脚本块中的Groovy代码替换了一般的构建步骤step()



在Jenkins管道中可以使用的可能步骤中,有一个名称为step,副标题为General Build step。https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/#step-一般构建步骤。我需要根据文件的内容反复调用此步骤。我已经创建了一个groovy脚本来读取文件并执行迭代,但我不确定如何在groovy中创建与我的step((等效的脚本。以下是我尝试执行的步骤的一般格式:

stage ('title') {
steps {
step([
$class: 'UCDeployPublisher',
siteName: 'literal string',
deploy: [
$class: 'com.urbancode.jenkins.plugins.ucdeploy.DeployHelper$DeployBlock',
param1: 'another literal string',
param2: 'yet another string'
]
])
}
}

我开发的脚本步骤如下:

steps {
script {
def content = readFile(file:'data.csv', encoding:'UTF-8');
def lines = content.split('n');
for (line in lines) {
// want to insert equivalent groovy code for the basic build step here
}
}
}

我想这里可能有一个微不足道的答案。我刚刚脱离了groovy/java世界的元素,我不知道如何继续。我做了大量的研究,看了詹金斯的源代码,看了插件,等等。我被卡住了!

检查以下内容,只需将UCDeployPublisher移动到一个新函数,然后从循环中调用它。

steps {
script {
def content = readFile(file:'data.csv', encoding:'UTF-8');
def lines = content.split('n');
for (line in lines) {
runUCD(line)
}
}
}
// Groovy function 
def runUCD(def n) {
stage ("title $n") {
steps {
step([
$class: 'UCDeployPublisher',
siteName: 'literal string',
deploy: [
$class: 'com.urbancode.jenkins.plugins.ucdeploy.DeployHelper$DeployBlock',
param1: 'another literal string',
param2: 'yet another string'
]
])
}
}
}

这显示了与我对已接受答案的评论相关的代码

pipeline {
stages {
stage ('loop') {
steps {
script {
... groovy to read/parse file and call runUCD
}
}
}
}
}

def runUCD(def param1, def param2) {
stage ("title $param1") {
step([
....
])
}
}

最新更新