How to auto build a job in jenkins if there is any change in code on Github repository

+1 vote
I want Jenkins to automatically build a job as soon as there is a change in the code on github. With polling jenkins checks if there is a change in the code. I don't want that. If there is a change in the code jenkins should be notified about the change and then it should auto build. Is this possible?
Jun 20, 2019 in Jenkins by Neel
• 3,020 points
78,847 views

3 answers to this question.

0 votes

Yes this is possible. Follow these steps.

1. Open Jenkins dashboard. Click on manage jenkins

2. Click on Configure system and under github configuration click advanced tab.

3. Check 'Specify another hook url' for GitHub configuration. 

4. Now you will get a url in the textbox. Copy this url as it is required in the next steps.

5. Now open your github repository. Go to settings -> webhooks -> add webhooks.

6. Now paste the url from step 4 in the payload url section. Next click on just push the event Now you should be able to see the added webhook in the list of webhooks.

7. Now go to jenkins dashboard. Go to your project configuration. In the build triggers section select github hook trigger for git scm polling. Save the changes.

answered Jun 20, 2019 by Arvind
• 3,050 points
Very good, steps, but it does not work if we are running the jenkins on windows machine locally, it stats host localhost is not supported because it is not reachable over the public internet
I guess it will work for windows system. Did you try on windows?
+3 votes

To automatically run build on code commit, follow these steps:

  1. Configuring Jenkins: 
    Jenkins listens for POST requests at a Hook URL. We need to give this URL to the repository on GitHub. Then, whenever code is pushed to that repository, GitHub will send a POST request to the Hook URL and Jenkins will run the build.

    • To get the Hook URL of Jenkins, Open the Jenkins Dashboard.

    • Go to: Manage Jenkins > Configure System

    • Under GitHub Plugin Configuration, Click on ‘Advanced' tab. 

    • Check 'Specify another hook url' for GitHub configuration. 

    • A textbox will appear with a hook URL. This is the Hook URL at which Jenkins will listen for POST requests. Copy this URL and go to the next step.

  2. Configuring GitHub Repository: 
    We now have to provide the Hook URL we got from Jenkins in the previous step.

    • Open your repository on GitHub.

    • Click ‘Settings’ on the navigation bar on the right-hand side of the screen.

    • Click ‘Webhooks ’ on the navigation bar on the left-hand side of the screen.

    • Click ‘Add webhook’ to add the webhook. 

    • Paste the URL you copied in the previous step as the ‘Payload URL’.

    • You can select the events for which you want the Jenkins build to be triggered. We will select ‘Just the push event’ because we want to run the build when we push our code to the repository.

    • Click ‘Add webhook’. 

    • You should now see the webhook you just added in the list of Webhooks for that repository.

  3. Configuring Jenkins Project : We now have Jenkins configured to run builds automatically when code is pushed to central repositories. However, Jenkins doesn’t run all builds for all projects. To specify which project builds need to run, we have to modify the project configuration.

    • In Jenkins, go to the project configuration of the project for which you want to run an automated build.

    • In the ‘Build Triggers’ section, select 'Github hook trigger for GITScm Polling'. 

    • Save your project.

Jenkins will now run the build whenever you push/commit your code to the GitHub repository.

answered Jul 31, 2019 by Sirajul
• 59,230 points
0 votes

Hi,

Poll SCM periodically polls the SCM to check whether changes were made (i.e. new commits) and builds the project if new commits were pushed since the last build. Jenkins periodically calls GitHub and checks if there was any push to the repository. Then, it starts the build. 

answered Dec 15, 2020 by MD
• 95,460 points

Related Questions In Jenkins

+1 vote
4 answers

Is there a way to change the Jenkins build number while building a job?

If you have access to the script ...READ MORE

answered May 7, 2019 in Jenkins by Atul
29,771 views
0 votes
1 answer

is there a way how to finish in-progress slave builds if Jenkins master goes offline

Hi@, You can assign your Jenkins Job to ...READ MORE

answered Jun 30, 2020 in Jenkins by MD
• 95,460 points
3,715 views
+2 votes
4 answers
0 votes
2 answers

how to execute a shell script post build step in Jenkins?

You could use the Post Build Task Plugin ...READ MORE

answered Jul 31, 2019 in Jenkins by Sirajul
• 59,230 points
29,519 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,053 views
+2 votes
1 answer
0 votes
1 answer
+3 votes
2 answers

Is there a way to restrict permissions to a user per individual job in jenkins?

Yes, you can configure user permissions per ...READ MORE

answered May 28, 2019 in Jenkins by Sirajul
• 59,230 points

edited May 28, 2019 by Sirajul 112,460 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP