如何将方法实用程序传递和调用到 Jenkins 模板?
How to pass and invoke a method utility to Jenkins template?
我有这个模板:
def call(body) {
def pipelineParams= [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = pipelineParams
body()
pipeline {
agent any
....
stages {
stage('My stages') {
steps {
script {
pipelineParams.stagesParams.each { k, v ->
stage("$k") {
$v
}
}
}
}
}
}
post { ... }
}
}
然后我在管道中使用模板:
@Library('pipeline-library') _
pipelineTemplateBasic {
stagesParams = [
'First stage': sh "do something...",
'Second stage': myCustomCommand("foo","bar")
]
}
在 stagesParams
中,我传递了我的命令实例(sh
和 myCustomCommand
),它们在模板中 land
作为 $v
。我怎样才能执行它们?某种 InvokeMethod($v)
?
目前我收到此错误:
org.jenkinsci.plugins.workflow.steps.MissingContextVariableException: Required context class hudson.FilePath is missing
Perhaps you forgot to surround the code with a step that provides this, such as: node
使用node
的问题是它在parallel
:
这样的情况下不起作用
parallelStages = [:]
v.each { k2, v2 ->
parallelStages["$k2"] = {
// node {
stage("$k2") {
notifySlackStartStage()
$v2
checkLog()
}
// }
}
}
如果你想执行地图提供的sh
步骤,你需要将地图值存储为闭包,例如
@Library('pipeline-library') _
pipelineTemplateBasic {
stagesParams = [
'First stage': {
sh "do something..."
}
'Second stage': {
myCustomCommand("foo","bar")
}
]
}
然后在管道阶段的 script
部分,您将需要执行闭包,但还将委托和委托策略设置为工作流脚本,例如
script {
pipelineParams.stagesParams.each { k, v ->
stage("$k") {
v.resolveStrategy = Closure.DELEGATE_FIRST
v.delegate = this
v.call()
}
}
}
我有这个模板:
def call(body) {
def pipelineParams= [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = pipelineParams
body()
pipeline {
agent any
....
stages {
stage('My stages') {
steps {
script {
pipelineParams.stagesParams.each { k, v ->
stage("$k") {
$v
}
}
}
}
}
}
post { ... }
}
}
然后我在管道中使用模板:
@Library('pipeline-library') _
pipelineTemplateBasic {
stagesParams = [
'First stage': sh "do something...",
'Second stage': myCustomCommand("foo","bar")
]
}
在 stagesParams
中,我传递了我的命令实例(sh
和 myCustomCommand
),它们在模板中 land
作为 $v
。我怎样才能执行它们?某种 InvokeMethod($v)
?
目前我收到此错误:
org.jenkinsci.plugins.workflow.steps.MissingContextVariableException: Required context class hudson.FilePath is missing
Perhaps you forgot to surround the code with a step that provides this, such as: node
使用node
的问题是它在parallel
:
parallelStages = [:]
v.each { k2, v2 ->
parallelStages["$k2"] = {
// node {
stage("$k2") {
notifySlackStartStage()
$v2
checkLog()
}
// }
}
}
如果你想执行地图提供的sh
步骤,你需要将地图值存储为闭包,例如
@Library('pipeline-library') _
pipelineTemplateBasic {
stagesParams = [
'First stage': {
sh "do something..."
}
'Second stage': {
myCustomCommand("foo","bar")
}
]
}
然后在管道阶段的 script
部分,您将需要执行闭包,但还将委托和委托策略设置为工作流脚本,例如
script {
pipelineParams.stagesParams.each { k, v ->
stage("$k") {
v.resolveStrategy = Closure.DELEGATE_FIRST
v.delegate = this
v.call()
}
}
}