脚本化管道中的 Jenkins 批准阶段

Jenkins approval stage in scripted pipelines

我已经使用 jenkins groovy 将管道设置为代码堆栈。为此,我编写了一些共享库以扩展我的 CI/CD 功能并避免 copy/paste 我所有管道代码中的一些块代码。

所以我有一个 groovy 函数来在管道中添加批准阶段,我已经在 Jenskins 文件 中使用声明性管道成功测试了它,但是当我在我的脚本管道函数中尝试。

这是声明性 Jenkinsfile 中的代码块,您可以在下面的屏幕截图中看到它的工作原理。

   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'
            }
        }
    }

但是试图通过我的 groovy 功能即时添加批准,但它不起作用。

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 不断抛出如下异常:

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]

请问我错过了什么?我如何在 groovy vars 文件中编写我的批准函数,以便我可以像上面的声明性管道阶段那样获得预期的行为?

只需删除 script 块,在脚本管道或管道库全局中不需要(也不合法)var