Triggering Jenkins builds from a git hook
I use Jenkins CI to build a number projects as part of a continuous integration system. I wanted to set things up so that, after each commit to a git repository containing one of these projects, the new code would be checked out and rebuilt by Jenkins (as well as collecting metrics and performing various other tasks). It turns out there’s a very easy way to do this using Git hooks and Jenkins remote build triggers.
Start by setting up a remote build trigger in a Jenkins project. This will enable a URL that, when fetched using an HTTP GET request, will cause Jenkins to build the project. To set things up, visit the Configure page for the project in Jenkins and, under Build Triggers, make sure the Trigger builds remotely option is checked. You’ll need to specify an authentication token to pass as a query parameter. I usually use md5sum to generate an MD5 message digest since they’re random(-ish) and URL-friendly. Make note of the URL that is displayed; that’s what you’ll request using an HTTP client to trigger a build.
Jenkins is not configured by default to require users to log in. If that’s how your local instance is set up, go ahead and test that the build trigger URL you just set up is working using cURL (wget works too):
curl ${JENKINS_URL}/${PROJECT_PATH}/build?token=${TOKEN}
The exact URL you should use is the one you noted earlier when setting up the remote build trigger. If all goes well, Jenkins should show that the project is built shortly after the HTTP request is made.
If you require authentication for your Jenkins instance, you’ll need to pass the appropriate credentials in your HTTP request to the trigger URL. In my case, Jenkins is configured to use local accounts; I use a separate account just for git hooks that has been given a limited set of permissions (just enough to trigger a build) using Jenkins’ matrix permission scheme:
Overall > Read
Job > Read
Job > Build
The next step is to perform the same request from a hook in your git repository. Since I want this behaviour to occur each time I check code into a bare repository on a server, I use the post-receive hook. Put the cURL from above, with a couple of additions, in hooks/post-receive:
curl --silent --show-error --user ${USER}:${PASS} ${JENKINS_URL}/${PROJECT_PATH}/build?token=${TOKEN}
NB: You may need to rename the hook file file by removing the .sample extension if you haven’t done so before.
The output from the hook script is displayed by git when you perform an operation that causes the hook to be called. Add --silent and --show-error options to stop cURL from displaying a progress meter, but still output any error messages that a user might need to see.
The ${USER} and ${PASS} are the username and password for the Jenkins ‘git’ account you set up.
If all goes well, the next time you commit some changes in a local clone of your repository and push them to the remote repository, Jenkins will automagically rebuild the project.
#continuous integration #git #jenkins #coding #build