Home > Software design >  Jenkins approval stage in scripted pipelines
Jenkins approval stage in scripted pipelines

Time:04-07

I've setup a pipeline as code stack using jenkins groovy. For that I've coded some shared libraries to extend my CI/CD capabilities and avoid copy/paste some block code within all my pipelines code.

So I have a groovy function to add approval stage in pipelines, which I've tested using declarative pipeline in a Jenskins File successfully, but which fails when I try in my scripted pipeline function.

Here is the block code in a the declarative Jenkinsfile which works as you can see in the screenshots below.

   stage('Approval') {
        // no agent, so executors are not used up when waiting for approvals
         when { changeset "vm-management/create-vm/**"}
        agent none
        steps {
            script {
                mail from: "$VM_EMAIL_FROM", to: "$VM_SUPPORT_EMAIL", subject: "APPROVAL REQUIRED FOR $JOB_NAME" , body: """Build $BUILD_NUMBER required an approval. Go to $BUILD_URL for more info."""
                def deploymentDelay = input id: 'Deploy', message: 'Deploy to production?', parameters: [choice(choices: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24'], description: 'Hours to delay deployment?', name: 'deploymentDelay')]
                sleep time: deploymentDelay.toInteger(), unit: 'HOURS'
            }
        }
    }

enter image description here enter image description here

But coming to try to add approval on the fly from my groovy function, it doesn't work.

def send(mail_from, mail_to, deploy_version) {
  // no agent, so executors are not used up when waiting for approvals
/*  when { 
    beforeAgent true
    anyOf {
      triggeredBy 'TimerTrigger'
    }
  }*/
  
  script {
      mail from: "${mail_from}", to: "${mail_to}", subject: "APPROVAL REQUIRED FOR $JOB_NAME" , body: """Build $BUILD_NUMBER from Deployment version ${deploy_version} required an approval. Go to $BUILD_URL for more info."""
      def deploymentDelay = input id: 'Deploy', message: 'Deploy to production?', parameters: [choice(choices: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24'], description: 'Hours to delay deployment?', name: 'deploymentDelay')]
          sleep time: deploymentDelay.toInteger(), unit: 'HOURS'
      }
 }

Jenkins keeps throwing exception like below :

groovy add manual approval using dshudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: script.call() is applicable for argument types: (org.jenkinsci.plugins.workflow.cps.CpsClosure2) values: [org.jenkinsci.plugins.workflow.cps.CpsClosure2@6097c139]

What Am I missing there please? How can I write my approval function in the groovy vars file so that I could have the expected behaviour like with the declarative pipeline stage above?

CodePudding user response:

Just remove the script block, it's not needed (and isn't legal) in a scripted pipeline or a pipeline library global var

  • Related