208
votes

I'm trying to set up Jenkins-ci for a project using GitHub. I've already set up Jenkins with the appropriate plugins. I want Jenkins to run build scripts only whenever someone on the project pushes to master. So far I've been able to set it up so that a build will be triggered anytime anyone pushes to anywhere, but that is too broad. I've done this with post-receive service hooks on Git.

I've read the Jenkins wiki, and a couple of tutorials, but this particular detail is missing... is it something to do with polling maybe? Or should work be done on the Git side, so that Git only triggers Jenkins when master is changed?

14
The original author of Jenkins, Kohsuke Kawaguchi, describes how to do push-notification from the repository using the Git plugin Git plugin 1.1.14. See kohsuke.org/2011/12/01/…GeraldScott

14 Answers

192
votes

As already noted by gezzed in his comment, meanwhile there is a good solution (described in Polling must die: triggering Jenkins builds from a Git hook):

  • Set the Jenkins job's build trigger to Poll SCM, but do not specify a schedule.

  • Create a GitHub post-receive trigger to notify the URL

    http://yourserver/jenkins/git/notifyCommit?url=<URL of the Git repository>?token=<get token from git to build remotely>
    
  • This will trigger all builds that poll the specified Git repository.

  • However, polling actually checks whether anything has been pushed to the used branch.

It works perfectly.

34
votes

As of version 0.5, the GitHub plugin for Jenkins can trigger a build when a change is pushed to GitHub.

9
votes

Instead of triggering builds remotely, change your Jenkins project configuration to trigger builds by polling.

Jenkins can poll based on a fixed internal, or by a URL. The latter is what you want to skip builds if there are not changes for that branch. The exact details are in the documentation. Essentially you just need to check the "Poll SCM" option, leave the schedule section blank, and set a remote URL to hit JENKINS_URL/job/name/polling.

One gotcha if you have a secured Jenkins environment is unlike /build, the /polling URL requires authentication. The instructions here have details. For example, I have a GitHub Post-Receive hook going to username:apiToken@JENKIS_URL/job/name/polling.

8
votes

For GitLab, use these steps:

  1. Go to your project's settings → Web hooks
  2. Enter the "Build Now" URL from your Jenkins project as a Push Event URL:

    http://server.com/jenkins/job/project_name/build?delay=0sec for example

  3. Click Add Web Hook and then test hook

Then any time you commit to the repository, the web hook is triggered and a build is created. Be sure to set your Jenkins workspace to delete workspace before each build so you get a fresh copy of the new code.

4
votes

Not related to Git, but below I will help with the Jenkins job configuration in detail with Mercurial. It may help others with a similar problem.

  1. Install the URL Trigger Plugin
  2. Go to the job configuration page and select Poll SCM option. Set the value to * * * * *
  3. Check the option: [URLTrigger] - Poll with a URL. Now you can select some options like modification date change, URL content, etc.
  4. In the options, select URL content change, select first option – Monitor change of content
  5. Save the changes.

Now, trigger some change to the Mercurial repository by some test check-ins.

See that the Jenkins job now runs by detecting the SCM changes. When the build is run due to Mercurial changes, then, you will see text Started by an SCM change. Else, the user who manually started it.

3
votes

I hope this helps: How to trigger a Jenkins build on Git commit

It's just a matter of using curl to trigger a Jenkins job using the Git hooks provided by Git.

The command curl http://localhost:8080/job/someJob/build?delay=0sec can run a Jenkins job, where someJob is the name of the Jenkins job.

Search for the "hooks" folder in your hidden .git folder. Rename the "post-commit.sample" file to "post-commit". Open it with Notepad, remove the ": Nothing" line and paste the above command into it.

That's it. Whenever you do a commit, Git will trigger the post-commit commands defined in the file.

3
votes

Continuous Integration with Jenkins, after code is pushed to repository from Git command/ GUI:

  1. Create a job in Jenkins with only job name and select type of the project freestyle. Click OK. The next page doesn't add anything - just click Save.
  2. Go to the your local Git repository where you have the source code and navigate to the .git/hooks folder.
  3. The hooks folder contains the few files. Check for the "post-commit". If not present, create a file, "post-commit" without a file extension:

    C:\work\test\\.git\hooks\post-commit
    
  4. Edit the "post-commit" file with the below command. Make sure it is present in your local source code hooks folder.

    curl -u userName:apiToken -X POST http://localhost:8080/jenkins/job/jobName/build?token=apiToken
    

    Example:

    curl -u admin:f1c55b3a07bb2b69b9dd549e96898384 -X POST http://localhost:8080/jenkins/job/Gitcommittest/build?token=f1c55b3a07bb2b69b9dd549e96898384
    

    5.

    userName: Jenkins user name

    jobName: Job name of the build

    apiToken: To get your API token, go to your Jenkins user page (top right in the interface). It is available in the "Configure" menu on the left of the page: "Show API token"

  5. Make changes in your source code and commit the code to repository.

  6. Your job, http://localhost:8080/jenkins/job/Gitcommittest/, should be building.

3
votes

You need to specify the branch. By default it listens to anything. See the blog post Hudson: Git and Maven plugins.

2
votes

Generic Webhook Trigger Plugin can be configured with filters to achieve this.

When configured with

  • A variable named ref and expression $.ref.
  • A filter with text $ref and filter expression like ^refs/heads/master$.

Then that job will trigger for every push to master. No polling.

You probably want more values from the webhook to actually perform the build. Just add more variables, with JSONPath, to pick what you need.

There are some use cases here: https://github.com/jenkinsci/generic-webhook-trigger-plugin/tree/master/src/test/resources/org/jenkinsci/plugins/gwt/bdd

1
votes

In my current organization, we don't do this in master but do do it on both develop and release/ branches (we are using Git Flow), in order to generate snapshot builds.

As we are using a multi branch pipeline, we do this in the Jenkinsfile with the when{} syntax...

stage {
    when { 
        expression { 
            branch 'develop'
        }
    }
}

This is detailed in this blog post: https://jenkins.io/blog/2017/01/19/converting-conditional-to-pipeline/#longer-pipeline

1
votes

Above answers are correct but i am addressing to them who are newbie here for their simplicity

especially for setting build trigger for pipeline:

Consider you have two Github branches: 1.master, 2.dev, and Jenkinsfile (where pipeline script is written) and other files are available on each branch

Configure new Pipeline project (for dev branch)

##1.Code integration with git-plugin and cron based approach Prerequisite git plugin should be installed and configure it with your name and email

  1. General section.Check checkbox - 'This project is parameterized’ and add Name-SBRANCH Default Value-'refs/remotes/origin/dev'
  2. Build triggers section" Check checkbox - 'Poll SCM' and schedule as per need for checking commits e.g. '*/1 * * * *' to check every minute
  3. Pipeline definition section.Select - Pipeline script from SCM—> select git—> addRepository URL—>add git credentials—> choose advanced—> add Name- origin, RefSpec- '+refs/heads/dev:refs/remotes/origin/dev'(dev is github branch )—> Branches to build - ${SBRANCH} (Parameter name from ref 1st point)—> Script Path—> Jenkinsfile —> Uncheck Lightweightcheckout
  4. Apply—> save

##2.Code integration: github-plugin and webhook approach Prerequisite Github plugin should be installed and Github server should be configured, connection should be tested if not consider following configuration

Configure Github plugin with account on Jenkins

GitHub section Add Github server if not present API URL: https://api.github.com Credentials: Add secret text (Click add button: select type secret text) with value Personal Access Token (Generate it from your Github accounts—> settings—> developer setting—> personal access token—> add token—> check scopes—> copy the token) Test Connection—> Check whether it is connected to your Github account or not Check checkbox with Manage Hooks In advance sub-section just select previous credential for 'shared secret'

Add webhook if not added to your repository by

  1. Go to Github Repository setting —> add webhook—> add URL
    http://Public_IP:Jenkins_PORT/github-webhook/
  2. Or if you don't have Public_IP use ngrok. Install, authenticate, get public IP from command ./ngrok http 80(use your jenkins_port) then add webhook —> add URL http://Ngrok_IP/github-webhook/
  3. Test it by delivering payload from webhook page and check whether you get 200 status or not.

If you have Github Pull requests plugin configure it also with published Jenkins URL.

  1. General section.Check checkbox - 'Github project' add project URL -(github link ending with '.git/')
  2. General section.Check checkbox - 'This project is parameterized' and add Name-SBRANCH Default Value-'refs/remotes/origin/dev'
  3. Build triggers.section.Check checkbox - 'GitHub hook trigger for GITScm polling'
  4. Pipeline def'n section: Select - Pipeline script from SCM—> select git—> addRepository URL—> add git credentials—>choose advanced —>add Name- origin, RefSpec- '+refs/heads/dev:refs/remotes/origin/dev' (dev is github branch ) —> Branches to build - ${SBRANCH} (Parameter name from ref 1.st point)—> Script Path—> Jenkinsfile—> Uncheck Lightweightcheckout
  5. Apply—> save
0
votes

Use the pull request builder plugin: https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+builder+plugin

It's really straightforward. You can then setup GitHub webhooks to trigger builds.

0
votes

My solution for a local git server: go to your local git server hook directory, ignore the existing update.sample and create a new file literally named as "update", such as:

gituser@me:~/project.git/hooks$ pwd
/home/gituser/project.git/hooks
gituser@me:~/project.git/hooks$ cat update
#!/bin/sh
echo "XXX from  update file"
curl -u admin:11f778f9f2c4d1e237d60f479974e3dae9 -X POST http://localhost:8080/job/job4_pullsrc_buildcontainer/build?token=11f778f9f2c4d1e237d60f479974e3dae9

exit 0
gituser@me:~/project.git/hooks$ 

The echo statement will be displayed under your git push result, token can be taken from your jenkins job configuration, browse to find it. If the file "update" is not called, try some other files with the same name without extension "sample".

That's all you need

0
votes
  1. Manage Jenkins/ configure system /GitHub Servers

  2. On jenkins job / git credentials and Branch Specifier (give the branch you want to look for pushes)

enter image description here

  1. Webhook on github