Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the displayName of a Jenkins Multibranch Pipeline Job

I've put all my Jenkins logic in a structured pipeline script (aka Jenkinsfile).

If something goes wrong, i m sending mails. For the subject i want to use the displayName of the job and not the jobs id env.JOB_NAME (as they are driven by access control patterns and not readability).

With a normal pipeline job i could use currentBuild.rawBuild.project.displayName but for multibranch pipelines this is just the branch name.

Or is there a even better way to get the userfriendly name, then traversing the rawBuild?

like image 441
dag Avatar asked Apr 26 '17 15:04

dag


People also ask

How do I get the full project name in Jenkins?

Jenkins sets some environment variables such as JOB_NAME (see here) for more details on the variables set. You can then access these in ant via ${env. JOB_NAME} .

What is the difference between pipeline and Multibranch pipeline in Jenkins?

Jenkins Pipeline Vs. Multibranch Pipeline. A multibranch pipeline is meant for building multiple branches from a repository and deploy to multiple environments if required. A pipeline job supports both pipeline steps to be added in Jenkins configuration and form SCM.


1 Answers

For now i found no convinient public api, so this seems to be the way to go:

String getDisplayName(currentBuild) {
    def project = currentBuild.rawBuild.project

    // for multibranch pipelines
    if (project.parent instanceof WorkflowMultiBranchProject) {
        return "${project.parent.displayName} (${project.displayName})"
    } else {
        // for all other projects
        return project.displayName
    }
}
like image 198
dag Avatar answered Nov 15 '22 07:11

dag