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

发布于 2025-01-17 13:14:10 字数 2676 浏览 3 评论 0原文

我已经使用 jenkins groovy 设置了一个管道作为代码堆栈。为此,我编写了一些共享库来扩展我的 CI/CD 功能,并避免在我的所有管道代码中复制/粘贴一些块代码。

因此,我有一个在管道中添加审批阶段的常规函数​​,我已经在 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'
            }
        }
    }

输入图片此处描述在此处输入图像描述

但是尝试从我的常规功能中动态添加批准,它不起作用。

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

詹金斯不断抛出异常,如下所示:

groovy 使用 dshudson.remoting.ProxyException 添加手动批准:groovy.lang.MissingMethodException:没有方法签名:script.call() 适用于参数类型:(org.jenkinsci.plugins.workflow.cps.CpsClosure2) 值:[org.jenkinsci.plugins.workflow.cps.CpsClosure2@6097c139]

我在那里缺少什么 请?如何在 groovy vars 文件中编写我的批准函数,以便我可以获得与上面的声明性管道阶段类似的预期行为?

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 hereenter 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少钕鈤記 2025-01-24 13:14:10

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文