7
votes

I am trying to set up Jenkins with a Git project so that:

  1. It will build from branches matching a pattern (origin/master, origin/feature/*, origin/hotfix/*, etc.) whenever changes are pushed to the central repository

  2. Developers and testers can trigger a build for any revision they want, specified as a build parameter that is a tag name, branch name or commit hash. The job has other parameters and we will occasionally want to create builds with something other than the default values.

I have got 1. working correctly by setting up a post-receive script on the Git server and adding multiple branch specifiers in Jenkins.

In order to also do 2., I added an extra build parameter GitRef and then added an extra branch specifier with $GitRef. Manually starting a build would then just keep building from the same commit/branch every time, whatever the parameter was set to. If I removed all the other branch specifiers, the manual builds would work as expected. But then the hook-triggered builds would only build from origin/master (the default value of $GitRef).

Is what I am trying to achieve even possible without creating a two jobs for every project? If so, what do I need to do to get it working?

2
you are interested in this topic yet?enrique-carbonell
I never did manage to get this working. We're just doing without manually triggered builds now. I had a look at the plugin code but I couldn't quite get my head around it. As far as I could tell, it didn't resolve the macros at the right point and that's why it didn't do what I wanted.derkyjadex
you need something like "Gitlab Merge Request Builder Plugin" (Ref: wiki.jenkins-ci.org/display/JENKINS/…)enrique-carbonell

2 Answers

2
votes

If you install the Git Parameters Plugin you can allow users to start a parameterized build using a specific commit ID, branch or tag.

You can then set the default value for your parameters as ** and by default, Jenkins will build the latest commit on branches.

1
votes

Instead of using $GitRef as another branch specifier, just use it as a String variable with no default value. Then, as the first step of your build phase in Jenkins, have a script that checks if that value is set:

#!/bin/bash
if [ -n $GitRef ]
then
  echo "Manually specified reference found. Building $GitRef"
  git checkout $GitRef
else  
  echo "No explicit branch specified, building $GIT_BRANCH" 
fi

At this point, run your build as per normal (e.g. Maven). The build will run whatever branch/tag/commit you wish to compile, and if it doesn't exist Jenkins should take care of the git checkout <bad_ref> by failing the build.