使用变量作为 Jenkins 输入消息文本

Using variable as Jenkins input message text

我想在脚本管道内的输入步骤中使用变量作为消息。

stage("Manual Approval"){
        sh """
        ls -la
        versionNumber=`grep -wE -A 2 '"package": "example0"'`
        ancestorVersion=`grep -wE -A 2 '"package": "example"'`
        """
        timeout(time: 120, unit: 'MINUTES') {
            input message: "Do you want to build ver. ${versionNumber} having ver. ${ancestorVersion} as an ancestor?", submitter: 'user1'
        }
    }

在最新版本的管道中sh步骤允许将输出保存在一个变量中,如下:

script {
    INFO_SYSTEM = sh (
        script: 'uname -a',
        returnStdout: true
    ).trim()
    echo "Value: ${INFO_SYSTEM}"
}

输出:

Running on Jenkins in /var/jenkins_home/workspace/testing
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Testing)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ uname -a
[Pipeline] echo
Value: Linux d7d184735414 4.14.225-121.357.amzn1.x86_64 #1 SMP Mon Mar 15 23:52:05 UTC 2021 x86_64 GNU/Linux
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

因此,也许您可​​以更改执行此任务的方法,让每个变量在单个命令中执行并检索输出,如下所示:

stage("Manual Approval"){
    VERSION_NUMBER = sh (
            script: 'grep -wE -A 2 '"package": "example0"'',
            returnStdout: true
        ).trim()

    ANCESTOR_VERSION = sh (
            script: 'grep -wE -A 2 '"package": "example"'',
            returnStdout: true
        ).trim()

    timeout(time: 120, unit: 'MINUTES') {
            input message: "Do you want to build ver. ${VERSION_NUMBER} having ver. ${ANCESTOR_VERSION} as an ancestor?", submitter: 'user1'
        }
}