Search code examples
jenkins-pipelinejenkins-build-flow

How to apply approval on Jenkins Pipeline through email


I have created an Build Pipeline in Jenkins as below.

Jenkins Build on Maven

Configuration Detail of Build Pipeline as follows. 1: Pic 1 Pic 2 Pic 3 Pic 4 Pic 5 Pic 6

I have successfully configure the Email on Jenkins at pic 5 and it is working fine. I can receive email when build pass or fail.

How can i configure Manual Approval on my build, so that email can be generated and Admin can use the Jenkin's console link in the email to login and Approve the Build.

How can i do this.


Solution

  • node
    
    
    {def jobName = currentBuild.fullDisplayName
    def mailToRecipients = '[email protected]'
    def useremail = '[email protected]'
    
    stage("Build")
    {
        echo "Build"
    }
    stage("Test")
    {
        def userAborted = false
        emailext body: ''' 
        Dear Admin, <br>
        Please go to console output of ${BUILD_URL}input to approve or Reject.<br>
        ''',
        mimeType: 'text/html',
        subject: "[Jenkins] ${jobName} Build Approval Request",
        from: "${useremail}",
        to: "${mailToRecipients}",
        recipientProviders: [[$class: 'CulpritsRecipientProvider']]
        
        try {
            userInput = input submitter: 'Administrator', message: 'Do you approve?'
            }
            catch (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e)
            {
                cause = e.causes.get(0)
                echo "Abroted by" + cause.getUser().toString()
                    userAbroted = true
                echo "System aborted, but looks like timeout period did'nt complete. Aborting."
            }
            if (userAborted) {
                currentBuild.result = 'ABORTED'
            }
            else {
                echo "Testing"
            }
    }
    stage('Deploy')
    {
        echo "Deploy"
    }}
    

    Thanks to Sir, Anand Video