How can I make Jenkins CI with Git trigger on pushes to master?

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.


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.


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


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.