Skip to main content

[SOLVED] How to Set Up Jenkins CI with Git to Trigger on Master Pushes? - jenkins

[SOLVED] A Simple Guide to Setting Up Jenkins CI with Git for Master Push Triggers

In this chapter, we will look into how to set up Jenkins Continuous Integration (CI). We want Jenkins to automatically start builds when we push changes to the master branch of a Git repository. Jenkins is a strong automation server. It helps developers build, test, and deploy their applications easily. When we connect Jenkins with Git, we can make our work smoother. This means every change to the master branch gets tested and checked through automatic builds. This guide will show you the steps to do this connection well.

In this guide, we will cover these parts:

  • Part 1 - Install Jenkins and Needed Plugins
  • Part 2 - Set Up Git Repository in Jenkins
  • Part 3 - Create Jenkins Pipeline or Freestyle Project
  • Part 4 - Set Up Webhook in Git Repository
  • Part 5 - Test the Setup with a Push to Master
  • Part 6 - Check Build Status and Logs
  • Frequently Asked Questions

By following this guide, we will understand how to set up Jenkins CI with Git to trigger on master pushes. This will make our development process better. If you want to learn more about Jenkins settings, you can check our article on how to configure Git post-commit hooks. Also, if we face any problems, we can look at our help resources, like how to fix Jenkins CI pipeline issues.

Part 1 - Install Jenkins and Required Plugins

We need to set up Jenkins CI with Git. This helps us trigger builds when we push to the master branch. First, we will install Jenkins and the plugins we need.

  1. Install Jenkins:

    • We can download the latest Jenkins WAR file from the official Jenkins website.

    • Then we run Jenkins with this command:

      java -jar jenkins.war
    • Now we can access Jenkins at http://localhost:8080.

  2. Unlock Jenkins:

    • To unlock Jenkins, we need the initial admin password. We can find it by running:

      cat /var/lib/jenkins/secrets/initialAdminPassword
    • We enter this password to unlock Jenkins.

  3. Install Recommended Plugins:

    • In the setup wizard, we choose “Install suggested plugins”.
    • This will install important plugins like Git and Pipeline plugins.
  4. Install Git Plugin:

    • We go to Manage Jenkins > Manage Plugins.
    • In the “Available” tab, we search for “Git Plugin”.
    • We check the box and click “Install without restart”.
  5. Install Pipeline Plugin:

    • Next, we search for “Pipeline” in the “Available” tab.
    • We check the box and click “Install without restart”.

We should make sure Jenkins and the needed plugins are installed before we configure our Git repository. This is the base for connecting Jenkins CI with Git. It will help us trigger builds on pushes to the master branch. For more details, we can check this Git post-commit configuration article.

Part 2 - Configure Git Repository in Jenkins

To set up a Git repository in Jenkins for CI/CD, we can follow these simple steps.

  1. Access Jenkins Dashboard: First, we need to open our Jenkins in a web browser.

  2. Create a New Job: Next, we click on “New Item” in the Jenkins dashboard.

  3. Select Job Type: We give a name for our job. Then, we choose “Freestyle project” or “Pipeline” depending on what we need. After that, we click “OK”.

  4. Configure Source Code Management:

    • On the job configuration page, we scroll down to “Source Code Management”.

    • We select “Git”.

    • Now, we enter our Git repository URL. For example:

      https://github.com/username/repo.git
    • If our repository is private, we need to add credentials. We do this by clicking “Add” next to the Credentials dropdown. We choose the right credential type like Username with password or SSH key.

  5. Specify Branch: In the “Branches to build” box, we enter:

    */master

    This makes sure the job listens for changes to the master branch.

  6. Advanced Settings (if needed):

    • We can click on “Advanced” to set more options like “Git LFS”, “Refspec”, or “Additional Behaviours”.
    • For example, if we want to track tags, we can add “Check out to specific local branch” and give the branch name.
  7. Save Configuration: We click “Save” at the bottom of the page to save our settings.

  8. Webhook Configuration: We should set up a webhook in our Git repository (like GitHub or GitLab). It helps to trigger Jenkins builds when there are pushes to the master branch. We can find more details in Part 4 - Configure Webhook in Git Repository.

For more information on how to configure Git in Jenkins, we can check this link: how to configure Git post-commit.

Now, our Jenkins CI setup is ready. It will pull changes from the Git repository whenever we push to the master branch.

Part 3 - Set Up Jenkins Pipeline or Freestyle Project

To set up a Jenkins Pipeline or a Freestyle project that runs builds when we push to the master branch, we can follow these steps.

Jenkins Pipeline

  1. Create a new Pipeline Job:

    • We open Jenkins.
    • We click on New Item.
    • We type a name for our job and select Pipeline, then click OK.
  2. Configure the Pipeline:

    • In the pipeline settings, we scroll to the Pipeline section.
    • We choose Pipeline script and write our pipeline code. Here is a simple example:
    pipeline {
        agent any
        stages {
            stage('Checkout') {
                steps {
                    checkout scm
                }
            }
            stage('Build') {
                steps {
                    sh 'make' // Replace with your build command
                }
            }
            stage('Test') {
                steps {
                    sh 'make test' // Replace with your test command
                }
            }
        }
    }
  3. Save the Pipeline: We click Save to finish creating the pipeline.

Freestyle Project

  1. Create a new Freestyle Project:

    • We open Jenkins.
    • We click on New Item.
    • We type a name for our job and select Freestyle project, then click OK.
  2. Configure the Freestyle Project:

    • In the project settings, we scroll to the Source Code Management section.
    • We select Git and enter our repository URL. For example:
    https://github.com/username/repo.git
    • Under Branches to build, we write */master to trigger on master pushes.
  3. Add Build Steps:

    • In the Build section, we add build steps as we need (for example, Execute shell or Invoke Ant).
  4. Save the Freestyle Project: We click Save to finish creating the project.

Triggering on Master Pushes

For both Pipeline and Freestyle projects, we must set up Git webhooks in our repository to trigger builds on pushes. This setup lets Jenkins listen for changes in the Git repository and start a build automatically.

For more details on setting up Git post-commit hooks, we can check this guide.

By following these steps, we can set up a Jenkins Pipeline or a Freestyle project that triggers on master pushes. This helps us improve our Continuous Integration workflow.

Part 4 - Configure Webhook in Git Repository

To set up a webhook in our Git repository that triggers Jenkins CI when we push to the master branch, we can follow these steps:

  1. Access Repository Settings:

    • First, we go to our Git repository on platforms like GitHub, GitLab, or Bitbucket.
    • Next, we find the settings of our repository.
  2. Add a Webhook:

    • We look for the “Webhooks” section and click on “Add webhook”.
  3. Configure Webhook URL:

    • In the “Payload URL” box, we enter our Jenkins server URL. We add /github-webhook/ for GitHub or the right endpoint for our Git platform. For example:

      http://your-jenkins-server.com/github-webhook/
  4. Set Content Type:

    • We choose application/json as the content type.
  5. Select Triggers:

    • For GitHub, we pick “Just the push event” to make sure it triggers only on master pushes.
    • For GitLab, we can choose “Merge Request Events” and “Push Events”.
  6. Secret (Optional):

    • We can set a secret token for security. We also need to set this in Jenkins.
  7. Save the Webhook:

    • We click on “Add webhook” or “Save” to create the webhook.
  8. Verify Webhook:

    • After saving, we can test the webhook by pushing a commit to the master branch. We check the webhook delivery status in our Git repository settings to see if it was successful.

We need to make sure Jenkins is set up to handle incoming webhook requests correctly. For more details on how to configure Jenkins, we can check how to set up Jenkins CI with Git.

Part 5 - Test the Setup with a Push to Master

We need to check if our Jenkins Continuous Integration (CI) setup triggers builds when we push to the master branch. Here are the steps to do this:

  1. Make a Change in Your Repository:

    • Open your local repository and change a file or add a new file.
    • Commit the changes with a message that shows the update.
    git add .
    git commit -m "Testing Jenkins CI trigger on master"
  2. Push to Master Branch:

    • Push the changes to the master branch of your remote repository.
    git push origin master
  3. Verify Jenkins Build Trigger:

    • Log in to your Jenkins dashboard.
    • Go to the job we set up for the repository.
    • Check the “Build History” section. We want to see if a new build was triggered by the push.
  4. Monitor Build Execution:

    • Click on the build number to see the console output and logs.
    • Make sure the build runs successfully and does not have errors.

If the build does not start, we should check the webhook settings in our Git repository. We also need to make sure Jenkins has the right permissions to get push events. For more details on how to set up webhooks, see Part 4 - Configure Webhook in Git Repository.

We also need to ensure Jenkins is running and can be reached from the internet. This is important if we are using a cloud-based Git service. If there are issues, check the Jenkins system logs for more info.

By following these steps, we can test our Jenkins CI setup to see if it works when we push to master. This helps us confirm that our integration pipeline is working as we want.

Part 6 - Monitor Build Status and Logs

We can monitor the build status and logs in Jenkins CI after setting up Git integration to trigger on master pushes. Here are the steps:

  1. Access Jenkins Dashboard: Open your web browser and type the Jenkins URL to go to your Jenkins.

  2. View Job Status:

    • On the main dashboard, find your job (pipeline or freestyle).
    • The job status shows with colors:
      • Blue means successful builds
      • Red means failed builds
      • Yellow means unstable builds
  3. Check Build History:

    • Click on your job name to see its details.
    • Look for the “Build History” section on the left side. Click on any build number to get more info.
  4. Access Build Logs:

    • After you choose a specific build, click on “Console Output” to see the full build log.
    • The logs show all output from the build process, including any errors and warnings.
  5. Configure Email Notifications (Optional):

    • If you want to get notifications about build status changes, go to Job Configuration.
    • In the “Post-build Actions” section, add the Email Notification action. Set the recipients and change the message if you want.
  6. Use Jenkins Monitoring Plugins:

    • You can install plugins like Build Monitor Plugin or Green Balls Plugin to see build statuses better.
    • Install plugins by going to Manage Jenkins > Manage Plugins > Available tab.
  7. Integrate with External Monitoring Tools:

    • For better monitoring, you can connect Jenkins with tools like Prometheus or Grafana for live metrics and visuals.

By doing these steps, we can easily monitor our Jenkins CI build status and logs. This helps us know quickly about any problems during the CI process that starts from Git pushes. For more info on Jenkins CI issues, you can check this guide.

Frequently Asked Questions

1. How do we set up a Jenkins CI pipeline to trigger builds on Git master pushes?

To set up a Jenkins CI pipeline that triggers on Git master pushes, we need to configure a webhook in our Git repository. This webhook sends a POST request to Jenkins when we make changes in the master branch. For more details, look at our article on how to configure Git post-commit and make sure Jenkins is ready to get these notifications.

2. What plugins are essential for Jenkins CI with Git integration?

For good Git integration in Jenkins CI, we should install the Git plugin and the GitHub plugin. These plugins help us pull code from our Git repository and trigger builds when we push to the master branch. If we want to schedule jobs in Jenkins, we can check our guide on how can I schedule jobs in Jenkins for more info.

3. How can we monitor Jenkins build status and logs?

We can monitor Jenkins build status and logs right from the Jenkins dashboard. Each build gives us console output and status indicators. For more help with troubleshooting, we can read our article on how to fix Jenkins CI pipeline issues to keep our CI/CD process running well.

4. What should we do if our Jenkins pipeline is not triggering on Git pushes?

If our Jenkins pipeline is not triggering on Git pushes, we should first check the webhook settings in our Git repository. We need to ensure it points to the right Jenkins URL. Also, we should make sure Jenkins is reachable from our Git host. If we have permission issues, we can look at our article on how to fix permission denied errors in Jenkins for tips.

5. How can we ensure our Jenkins setup is secure when integrating with Git?

To keep our Jenkins setup safe while working with Git, we should use HTTPS for our Git URLs and set up authentication tokens for webhooks. We also need to limit access to our Jenkins instance and use role-based access control. For more detailed security tips, we can check our article on how to choose between Hudson and Jenkins to help us make good choices about our CI/CD tools.

Comments