Scripted Pipeline 实现Declarative pipeline的类似post功能

众所周知Declarative pipeline中可以用post去实现pipeline为某个状态后执行某个动作,例如:

pipeline {
    agent {node 'master'}
    stages {
        stage('build'){
            steps {
                script {
                    echo "build"
                }
            }
        }
    }
    post {
        success {
            echo "success"
        }
    }
}

Declarative pipeline中没有post这一功能,如果要实现类似功能,可以这样去操作:

node('master'){
    try{
        stage('build'){
            echo "build step"
        }
    } catch (e) {
        echo 'This will run only if 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://stackoverflow.com/questions/48989238/post-equivalent-in-scripted-pipeline

发布了41 篇原创文章 · 获赞 14 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/liurizhou/article/details/91489840