[Jenkins] Adding approval with pipeline script

To prevent production change in controllable manner, some organization require to have some manual approval in the process. To Implement in Jenkins, either use pipeline command or script.In this demo, it will set the boolean to mark user approve to deploy.

stage('Publish') {
            steps {
                script {
                    if(env.BRANCH_NAME=='master') {
                        def isContinue=input(
                                message: "Continue deploy to Production Environment?",
                                parameters: [
                                 [$class: 'BooleanParameterDefinition', defaultValue: true, description: '', name: 'Checked means you acknowledge and approve with this deployment']    
                                ])
                        echo "isContinue=${isContinue}"
                        if(isContinue) {
                            echo "deploy continue"
                        } else {
                            currentBuild.result = 'FAILURE'
                            echo "User untick and not agree on it."
                        }
                    } else { 
                        echo "no need to approve"
                    }
                }
            }
        }

For parameters, there are variants options but I use simple one as demo. Details can refer to reference (1).

Also, for currentBuild.result, there are 3 status: SUCCESS, FAILURE and UNSTABLE, unstable mean it has problem during execute but still success.

Reference

  1. Pipeline: Input Step, Jenkins,
    https://www.jenkins.io/doc/pipeline/steps/pipeline-input-step/
About C.H. Ling 260 Articles
a .net / Java developer from Hong Kong and currently located in United Kingdom. Thanks for Google because it solve many technical problems so I build this blog as return. Besides coding and trying advance technology, hiking and traveling is other favorite to me, so I will write down something what I see and what I feel during it. Happy reading!!!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.