代码之家  ›  专栏  ›  技术社区  ›  kol23

在脚本化管道中发布等效内容?

  •  49
  • kol23  · 技术社区  · 8 年前

    与声明性管道相比,脚本管道中“post”的语法是什么? https://jenkins.io/doc/book/pipeline/syntax/#post

    2 回复  |  直到 8 年前
        1
  •  73
  •   Tamás Szelei    7 年前

    对于脚本化管道,所有内容都必须以编程方式编写,并且大部分工作都是在 finally 块:

    Jenkinsfile (脚本化管道):

    node {
        try {
            stage('Test') {
                sh 'echo "Fail!"; exit 1'
            }
            echo 'This will run only if successful'
        } catch (e) {
            echo 'This will run only if failed'
    
            // Since we're catching the exception in order to report on it,
            // we need to re-throw it, to ensure that the build is marked as failed
            throw e
        } finally {
            def currentResult = currentBuild.result ?: 'SUCCESS'
            if (currentResult == 'UNSTABLE') {
                echo 'This will run only if the run was marked as unstable'
            }
    
            def previousResult = currentBuild.getPreviousBuild()?.result
            if (previousResult != null && previousResult != currentResult) {
                echo 'This will run only if the state of the Pipeline has changed'
                echo 'For example, if the Pipeline was previously failing but is now successful'
            }
    
            echo 'This will always run'
        }
    }
    

    https://jenkins.io/doc/pipeline/tour/running-multiple-steps/#finishing-up

        2
  •  13
  •   smelm    4 年前

    您可以修改@jf2010解决方案,使其看起来更整洁(在我看来)

    try {
        pipeline()
    } catch (e) {
        postFailure(e)
    } finally {
        postAlways()
    }
    
    
    def pipeline(){
        stage('Test') {
            sh 'echo "Fail!"; exit 1'
        }
        println 'This will run only if successful'
    }
    
    def postFailure(e) {
        println "Failed because of $e"
        println 'This will run only if failed'
    
    }
    
    def postAlways() {
        println 'This will always run'
    }