Gui13 Gui13 - 3 months ago 59
Groovy Question

Jenkins multibranch: change Job description from Groovy

My multibranch project in Jenkins works fine, but I would like to modify the job description, which is by default "Full project name: xxxx/", to something more meaningful.

I can easily change the build description by using the

currentBuild
variable that is available when my
Jenkinfile
is executed, but I can't figure how to modify the parent job description.

My use case is that each branch of my repository has an associated container that is updated with the latest build when it is finished. So each of the jobs in my multibranch project has its container and web URI, that I would like to put into the job description.

Is this possible?

Answer

How about using the Jenkins Api to update the current job's description? Something like:

To update the top multibranch project description:

def jobName = "${env.JOB_NAME}"
Jenkins.instance.getItem(jobName.split('/')[0]).description = "hi"

To update a specific branch project's description in the multibranch project:

def jobName = "${env.JOB_NAME}"
Jenkins.instance.getItem(jobName.split('/')[0]).getItem("${env.BRANCH_NAME}").description = "hi"

Note: You will need to approve several functions via "Manage Jenkins" -> "In process Script Approval"