Search code examples
jenkinsjenkins-pipelinemultibranch-pipeline

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?


Solution

  • 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
        }
    }