脚本化管道中的 Jenkins 审批阶段
我已经使用 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'
}
}
}
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需删除
script
块,脚本化管道或管道库全局var
中不需要(也不合法)它Just remove the
script
block, it's not needed (and isn't legal) in a scripted pipeline or a pipeline library globalvar