Bash (via Jenkins) regex if/else -
i'm trying extract jira story number branch name in jenkins (1.617 on ubuntu 12.04), maven deploy (of web application) can use jira number tag application name. if there no jira story number, branch tagged "bugfix".
e.g., check in branch abc-123_some_feature
. jenkins picks there has been checkin , builds project. extracts "abc-123" part , deploys application http://my-dev-server/abc-123
.
i have of working fine, except regex. , specifically, regex when running via jenkins.
to vastly simplify problem, on ubuntu 12 machine on our jenkins instance hosted, via command line, can 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 use same script in "execute shell" build command in otherwise empty jenkins job, "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 failure finished: failure
i assume the unexpected opening bracket start of regex string. however, need match group.
i've tried wrapping regex in double inverted commas (eg "(abc-[[:digit:]]*)"
, irritating effect script not fail, produce incorrect "no match" result.
fwiw want this, if can work out wrong regex script - assign jira number variable named branch
used later in build script:
[[ echo "${git_branch#*/feature/}" =~ (abc-[[:digit:]]*) ]] && branch=echo "${bash_rematch[1]}" || branch="bugfix"
aside: have "sort of" got working via different, similar script, if/else eludes me. can variable branch
set abc-123
if occurs in branch name, if not, grep fails in turn fails build.
branch=$(echo "${git_branch#*/feature/}" | grep -o 'abc-[0-9]\+')
finally, question similar this one, jenkins specific weirdness of unanswered.
many choroba , john mark mitchell pushing me in right direction.
the solution working in jenkins "execute shell script" build step is
!/bin/bash # name build jira number taken branch name. # note: first line (aka shebang) required force /bin/bash shell (instead of /bin/sh) handles regex below. [[ ${git_branch#*/feature/} =~ (abc-[[:digit:]]*) ]] && branch="${bash_rematch[1]}" || branch="bugfix" echo "branch $branch" echo feature_name=$branch > feature.prop
Comments
Post a Comment