This post will explain how to setup a single job in Jenkins that performs releases when tags are pushed to any number of repositories. This means you can:
- Let developers perform releases without admin permissions in Jenkins.
- Let Jenkins build releases without giving Jenkins permission to push to Git.
- No Jenkinsfiles in the repositories, eliminate code duplication.
- Trace whoever made the release by looking at the Git log.
The code used in the post is here: https://github.com/tomasbjerre/bitbucket-server-jenkins-release-tags
I implement a flow like this:
- The user pushes a tag to Git
git tag 1.0
git push -u origin 1.0
- Git (in my case Bitbucket Server) invokes Jenkins with a webhook
- Jenkins will clone repository, checkout the commit, perform the release and email the user.
A single pipeline is created in Jenkins, completely generic and can handle any number of Git repositories. The webhook needs to be added to the Git repositories that should use the job.
A Jenkins pipeline job is created. Triggered by Generic Webhook Trigger. The pipeline can be found here: https://github.com/tomasbjerre/bitbucket-server-jenkins-release-tags/blob/master/Jenkinsfile
The build log may look like this: https://github.com/tomasbjerre/bitbucket-server-jenkins-release-tags/blob/master/build-log-sample.txt
This workflow is turned on for any repository by configuring the repository with a webhook that triggers on push events. In Bitbucket Server, this includes tag events.
When fiddling with it you may want to check the event log.
It may have some useful info in the response.
Here is a Youtube clip showing this flow: