如何获取Jenkins多分支流水线作业的displayName

6
我已经将所有的Jenkins逻辑放在一个结构化的管道脚本中(也称为Jenkinsfile)。
如果出现问题,我会发送邮件。对于主题,我想使用作业的displayName而不是作业ID env.JOB_NAME(因为它们受访问控制模式驱动,而不是可读性)。
对于普通的管道作业,我可以使用currentBuild.rawBuild.project.displayName,但对于多分支管道来说,这只是分支名称。
或者有没有更好的方法来获取用户友好的名称,而不是遍历rawBuild?
2个回答

8

目前我没有找到方便的公共API,所以这似乎是最好的选择:

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

currentBuild 不是全局变量而是输入参数吗? - Blake Mitchell
2
你说得对,Blake。但是我将这段代码作为共享库提供的类中的静态函数使用,所以必须将“currentBuild”作为参数传递才能使用它。如果你在流水线中定义该函数,则不需要这样做。 - dag

0

我使用currentBuild.fullProjectName,它被设置为multibranch_pipeline_name/branch_namepipeline_name,具体取决于您是使用多分支管道还是普通管道。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接