Reputation: 61
I'm using Jenkins ver. 2.150.1 and have some freestyle jobs and some pipeline jobs. In both job types I am using the emailext plugin, with template and pre-send scripts.
It seems that the build variable, which is available in the freestyle projects, is null in the pipeline projects.
The pre-send script is the following (just an example, my script is more complex):
msg.setSubject(msg.getSubject() + " [" + build.getUrl() + "]")
There is no problem with the msg variable. In the freestyle job, this script adds the build url to the mail subject. In the pipeline job, the following is given in the job console:
java.lang.NullPointerException: Cannot invoke method getUrl() on null object
The invocation of emailext in the pipeline job is:
emailext body: '${SCRIPT, template="groovy-html.custom.pipeline.sandbox.template"}',
presendScript: '${SCRIPT, template="presend.sandbox.groovy"}',
subject: '$DEFAULT_SUBJECT',
to: '[email protected]'
I would rather find a general solution to this problem (i.e. Access the build variable in a pipeline pre-send script), but would also appreciate any workarounds to my current needs: Access job name, job number, and workspace folder in a pipeline pre-send script.
Upvotes: 0
Views: 3121
Reputation: 61
I have finally found the answer -
Apparently for presend script in pipeline jobs, the build
object does not exist, and instead the run
object does. At the time I posted this question this was still undocumented!
Found the answer in this thread
Which got the author to update the description in the wiki:
- run - the build this message belongs to (may be used with FreeStyle or Pipeline jobs)
- build - the build this message belongs to (only use with FreeStyle jobs)
Upvotes: 1
Reputation: 520
You can access the build
in a script like this:
// findUrl.groovy
def call(script) {
println script.currentBuild.rawBuild.url
// or if you just need the build url
println script.env.BUILD_URL
}
and would call the script like this from the pipeline:
stage('Get build URL') {
steps {
findUrl this
}
}
The currentBuild
gives you a RunWrapper object and the rawBuild
a Run. Hope this helps.
Upvotes: 0