Reputation: 964
I'm trying to extract the Jira story number from a branch name in Jenkins (1.617 on Ubuntu 12.04), so that my Maven deploy (of a web application) can use that Jira number to tag the application name. If there is no Jira story number, I would like the branch to get tagged with "bugfix".
E.g., I check in branch ABC-123_Some_Feature
. Jenkins picks up that there has been a checkin and builds the project. It then extracts the "ABC-123" part and deploys the application to http://my-dev-server/ABC-123
.
I have all of this working fine, except the regex. And specifically, the regex when running via Jenkins.
To vastly simplify the problem, on the Ubuntu 12 machine on which our Jenkins instance is hosted, via command line, I can do this:
xxx@yyy:/$ [[ abc_ABC-123_def =~ (ABC-[[:digit:]]*) ]] && echo "match" || echo "No match"
match
xxx@yyy:/$ [[ abc_XXX-ZZZ_def =~ (ABC-[[:digit:]]*) ]] && echo "match" || echo "No match"
No match
However, when I use this same script in an "Execute shell" build command in an otherwise empty Jenkins job, the "Console output" is
[EnvInject] - Loading node environment variables.
Building in workspace /var/lib/jenkins/jobs/TEST PROJECT/workspace
[workspace] $ /bin/sh -xe /tmp/hudson4251159992600854370.sh
/tmp/hudson4251159992600854370.sh: 2: /tmp/hudson4251159992600854370.sh: Syntax error: "(" unexpected
Build step 'Execute shell' marked build as failure
Finished: FAILURE
I assume the the unexpected opening bracket is the start of the regex string. However, I need it to match the group.
I've also tried wrapping the regex in double inverted commas (eg "(ABC-[[:digit:]]*)"
, with the irritating effect that the script does not fail, but it does produce an incorrect "No match" result.
FWIW what I want to do is this, if I can work out what is wrong with the regex script - assign the Jira number to a variable named branch
that is used later in a build script:
[[ echo "${GIT_BRANCH#*/feature/}" =~ (ABC-[[:digit:]]*) ]] && branch=echo "${BASH_REMATCH[1]}" || branch="bugfix"
Aside: I have "sort of" got this working via a different, similar script, however the if/else eludes me. I can get the variable branch
to be set to ABC-123
with this if it occurs in the branch name, but if not, the grep fails which in turn fails the build.
branch=$(echo "${GIT_BRANCH#*/feature/}" | grep -o 'ABC-[0-9]\+')
Finally, this question is similar to this one, but the Jenkins specific weirdness of that is unanswered.
Upvotes: 0
Views: 1874
Reputation: 964
Many thanks to choroba and John Mark Mitchell for pushing me in the right direction.
The solution that is working in my Jenkins "Execute shell script" build step is
!/bin/bash
# Name the build with the JIRA number taken from the branch name.
# NOTE: the first line (aka shebang) is required to force /bin/bash shell (instead of /bin/sh) which handles the regex below.
[[ ${GIT_BRANCH#*/feature/} =~ (ABC-[[:digit:]]*) ]] && branch="${BASH_REMATCH[1]}" || branch="bugfix"
echo "Branch is $branch"
echo feature_name=$branch > feature.prop
Upvotes: 1