How to get the displayName of a Jenkins Multibranch Pipeline Job
Asked Answered
Y

2

6

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?

Yarrow answered 26/4, 2017 at 15:51 Comment(0)
Y
8

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
    }
}
Yarrow answered 28/4, 2017 at 8:52 Comment(2)
Isn't currentBuild a global rather than an input parameter?Disclaim
You are right Blake, but i use this code as static function in a class provided by a shared library so i had to pass the currentBuild as parameter in order to use it. If you define the function in your pipeline you don't need this.Yarrow
C
0

I use currentBuild.fullProjectName which is set to multibranch_pipeline_name/branch_name or pipeline_name depending if you are using a multibranch pipeline or normal pipeline.

Crawl answered 1/10, 2020 at 17:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.