【问题标题】:How To Run A Script As Jenkins Pipeline Post-Build Stage如何在 Jenkins 流水线构建后阶段运行脚本
【发布时间】:2021-06-30 17:30:49
【问题描述】:

是否可以使用作业 DSL 作为管道作业的构建后步骤执行 shell 脚本?

    post {
        success {
            sh """
            echo "Pipeline Works"
            """
            }
        failure {
            shell('''
            |echo "This job failed"
            |echo "And I am not sure why"
            '''.stripMargin().stripIndent()
            )
        }
    }

我能够执行一个单行程序,但理想情况下我想执行一个脚本。

我尝试过类似的方法

    publishers {
        postBuildScripts {
            steps {
                shell('echo Hello World')
            }
            onlyIfBuildSucceeds(false)
            onlyIfBuildFails()
        }
    }
}

但似乎发布者已被弃用。

【问题讨论】:

    标签: jenkins jenkins-pipeline jenkins-groovy jenkins-job-dsl post-build


    【解决方案1】:

    您最初的尝试很好,问题是在声明性管道中的字符串上调用.stripMargin().stripIndent() 是不可能的。要运行这样的 groovy 代码,您需要用 script 块包装它。
    请尝试以下操作:

    post {
        success {
            sh 'echo "Pipeline Works"'
        }
        failure {
            script {
                sh '''
                |echo "This job failed"
                |echo "And I am not sure why"
                '''.stripMargin().stripIndent()
            }
        }
    }
    

    【讨论】:

    • 不知道我是怎么错过的。非常感谢。这正是我所期望的。
    猜你喜欢
    • 1970-01-01
    • 2017-03-07
    • 2022-10-25
    • 2021-05-15
    • 2023-03-07
    • 2020-09-04
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多