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?

dag
  • 1,133
  • 12
  • 25

2 Answers2

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
    }
}
dag
  • 1,133
  • 12
  • 25
  • Isn't `currentBuild` a global rather than an input parameter? – Blake Mitchell May 18 '17 at 20:05
  • 2
    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. – dag May 23 '17 at 07:39
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.

Chris Maggiulli
  • 3,375
  • 26
  • 39
Eric
  • 1