Jenkins 管道 sh 显示 name/label

Jenkins Pipeline sh display name/label

Jenkins 2 Pipeline 插件有一个有用的功能,允许快速概览管道阶段和步骤状态,包括日志输出。

但是,如果您使用 "Shell script" (sh) 步骤,似乎没有办法用有用的名称来标记该脚本,因此显示仅显示一长串 "Shell Script"(如下图所示)。

如何指定一个有用的名称,或者如何使用其他步骤来实现相同的效果?

2019 年 2 月更新

根据, it is now possible to assign an optional label to the sh step, starting from v2.28, and for those who can't upgrade yet, there's also a workaround. Please check 了解详情和意见!


上一版本(悬停即可查看):

据我所知,这目前是不可能的。在詹金斯 追踪器,有一个 Name or alias Shell Script Step (sh) 问题 这类似于你的情况:

The sh step adds a "Shell Script" step in the Pipeline. However, there could be multiple such steps including steps from various plugins (e.g., Docker), which makes it hard to distinguish the steps. We should perhaps add an optional parameter to sh to specify a name or alias which would then appear in the pipeline steps. e.g., the following can be the step for npm which would show as "Shell script: npm" in the pipeline view.

sh cmd:"npm install", name: "npm"
但是,它作为旧 允许阶段的副本而关闭 作为最近已修复的标记块运行,并且 似乎包含在 pipeline-stage-step-plugin 的 v2.2 中(参见 更新日志).

似乎阶段现在可以嵌套,它们将出现在 查看 table,但我认为这不是您要查找的内容。

嗯,危急时刻需要危急措施。如果你会用Blue Ocean,你可以使用单行并行步骤。

        parallel(
            "This is my step name" : {
                sh 'env'
            }
        )

我也在尝试同样的事情,但在不同的上下文中,我的团队不希望多个 sh log window over log UI,所以我确实尝试在一个中使用多个 UNIX 命令线 例如 jenkinsPipeline.sh "echo \"PATH: $PATH\";java -version;echo PROJ DIR = $projectDirectory;env > env.txt;cat env.txt;ls && cd $projectDirectory && gradle --refresh-dependencies clean assemble" 它适用于 Jenkins 工作中的 Jenkins 管道脚本。 但是如果我使用共享库来扩展管道和相同的策略,那么它就不起作用或者像往常一样为 sh log in UI.

创建多个 windows

试试这个,一个很好的解决方法

import org.jenkinsci.plugins.workflow.cps.CpsThread
import org.jenkinsci.plugins.workflow.actions.LabelAction


    def test() {
    def xyz = "Prints PWD"
    try {
        sh script: 'pwd'
    }
    finally {
        CpsThread.current().head.get().addAction(new LabelAction("Shell script ${xyz} "))
    }
}

它并不完美,但我通常认为添加一个描述以下 bat 或 sh 步骤试图完成的回显步骤就足够了。没见过的人应该能很快看出来。

echo "Testing with Ping"
bat "ping www.whosebug.com"
echo "Getting IPs"
bat "nslookup www.whosebug.com"

根据 sandy 的出色回答,我创建了一个小脚本包装器,将 sh 步骤封装在 try/finally 块中。

基本用法:

wrapper.script script: 'echo the invisible script', returnStdout: true, stepName: "description #1"

将显示 "description #1" 而不是通用文本。

完整的源代码和安装说明在这里 https://github.com/ael-computas/jenkins-script-wrapper

可以很容易地作为库安装在您的 jenkins 服务器上。

版本 2.28+ "Pipeline Nodes and Processes Plugin" has gained the label option for the sh step now with JENKINS-55410:

label (optional)

Label to be displayed in the pipeline step view and blue ocean details for the step instead of the step type. So the view is more meaningful and domain specific instead of technical.

  • Type: String

例如:

sh script: "echo foo", label: "my step"

如果您还不能升级,另一种选择是使用 Labelled Pipeline Steps plugin

sh "echo foo", label: "my step"

不适合我,

必须是:

sh script: "echo foo", label: "my step"