Reputation: 973
I am trying to set up Jenkins with a Git project so that:
It will build from branches matching a pattern (origin/master, origin/feature/*, origin/hotfix/*, etc.) whenever changes are pushed to the central repository
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?
Upvotes: 7
Views: 2406
Reputation: 31151
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.
Upvotes: 2
Reputation: 2993
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.
Upvotes: 1