在 jenkins 管道中对不稳定和成功执行 post 中的脚本

Execute a script in post on both unstable and success in a jenkins pipeline

我的管道看起来像这样:

pipeline{
...
    post {
        always {
            archiveArtifacts artifacts: 'artifacts/**/*'
            script {
...
            }
            rtp stableText: '${FILE:artifacts/summary.html}', parserName: 'HTML'
        }
        success {
            script {
...
            }
        }
    }
}

我希望成功执行的脚本在不稳定时也能执行,我该如何实现? 有没有办法指定success or unstable {? 或者有没有办法声明在其他地方采取的行动,并在成功和不稳定的标签中“调用”它?

你也可以按照下面的方式做

def commonPostSteps() {
    echo "Hello World"
    script {
        def x =10
        print x + 20
    }
}
pipeline {
    agent any;
    stages {
        stage('one') {
            steps {
                echo "${env.STAGE_NAME}"
            }
        }
    }
    post {
        always {
            echo "post always"
        }
        success {
            commonPostSteps()
        }
        unstable {
            commonPostSteps()
        }
    }
}