Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

My pipeline looks like that:

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

I'd like that the script which is executed on success, was executed also on unstable, how can I achieve that? Is there a way to specify success or unstable {? Or is there a way to declare the action to take somewhere else and "invoke" it in a success and in an unstable tags?

like image 814
ncarrier Avatar asked Nov 21 '25 20:11

ncarrier


1 Answers

you can also do like below

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()
        }
    }
}
like image 92
Samit Kumar Patel Avatar answered Nov 24 '25 21:11

Samit Kumar Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!