Reputation: 8016
What's the best (or any!) way to put the output of a command in a Jenkins build name?
I'd like to put the output of the git describe
command into the build name of a Jenkins job.
I've installed the build-name-setter
plugin with the hopes of setting the build name to something like:
${ENV, var="GIT_DESCRIBE"}
I just don't know how to set $GIT_DESCRIBE
before the name is set! I've tried using the EnvInject
plugin. The only way to dynamically set the environment variables is to use groovy script. This would work well, except my job is running on a remote slave and the groovy script is (apparently) running only on the master.
Upvotes: 7
Views: 3757
Reputation: 485
With build-name-setter plugin v1.6.7 one could do the following:
"Execute shell" build step, which does git describe >version.txt
"Update build name" build step, which takes build name from version.txt
.
Upvotes: 0
Reputation: 56
I tried following the steps outlined by Christopher Orr but my "Execute shell" script seemed to only run after the build had started. In my case GIT_DESCRIBE was never set/injected in time for the build to use it.
After some time researching I found a solution using the "Evaluated Groovy script" step as part of the Environment Injector Plugin. The Groovy script is evaluated pre-build. The main caveat there though is that the .groovy script is not run in the $WORKSPACE. What I ended up doing was executing a shell script located in my app ($WORKSPACE) from the .groovy script and returning its output as a map with GIT_DESCRIBE.
Evaluated Groovy script
def sout = new StringBuilder()
def serr = new StringBuilder()
def proc = "$WORKSPACE/git-describe.sh".execute()
proc.waitForProcessOutput(sout, serr)
def map = [GIT_DESCRIBE: sout.toString()]
return map
git-describe.sh
#! /bin/bash
# change working directory to the current script's directory
cd "${0%/*}"
echo `git describe`
From there you should be able to reference GIT_DESCRIBE in your "Build name" macro.
${ENV, var="GIT_DESCRIBE"}
Upvotes: 0
Reputation: 111555
If you need the "describe" data (i.e. you can't just use the existing $GIT_BRANCH
or $GIT_COMMIT
environment variables), you could add an "Execute shell step" with:
echo GIT_DESCRIBE=$(git describe) > git.properties
Then after that, add an EnvInject build step which injects properties from the git.properties
file.
Upvotes: 6