是否可以将变量从步骤传递到jenkinsfile中的脚本?



我有一个管道,我想传递一个数组列表,我从一个groovy方法到运行在Master Jenkins中的脚本。

stages {
stage('Get Tests Parameter') {
steps {
code = returnList()
script {
properties([
parameters([
[$class              : 'CascadeChoiceParameter',
choiceType          : 'PT_CHECKBOX',
description         : 'Select a choice',
defaultValue        : '',
filterLength        : 1,
filterable          : false,
name                : 'Tests',
referencedParameters: 'role',
script              : [$class        : 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox  : true,
script   : 'return ["ERROR"]'
],
script        : [
classpath: [],
sandbox  : false,
script   : code
]
]
]
])
])
}
}
}
}
...
def returnList() {
def stringList = []
def fileContent = readFile "/var/jenkins_home/example.txt"
for (line in fileContent.readLines()) {
stringList.add(line.split(",")[0] + ":selected");
}
return stringList
}

阶段运行在从属,所以我不能在脚本中执行方法returnList(),因为脚本运行在Master中。所以我试图得到returnListArrayList到一个变量,并在脚本部分使用该变量。这可能吗?

如果要在特定节点中执行特定步骤,则可以在阶段块中指定代理。因此,您可以做的是在初始阶段在主控上执行文件读取逻辑,然后在连续的阶段中使用它。查看下面的示例。

def code
pipeline {
agent none
stages {
stage('LoadParameters') {
agent { label 'master' }
steps {
scipt {
code = returnList()
}
}
}
stage('Get Tests Parameter') {
steps {
script {
properties([
parameters([
[$class              : 'CascadeChoiceParameter',
choiceType          : 'PT_CHECKBOX',
description         : 'Select a choice',
defaultValue        : '',
filterLength        : 1,
filterable          : false,
name                : 'Tests',
referencedParameters: 'role',
script              : [$class        : 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox  : true,
script   : 'return ["ERROR"]'
],
script        : [
classpath: [],
sandbox  : false,
script   : code
]
]
]
])
])
}
}
}
}
}
def returnList() {
def stringList = []
def fileContent = readFile "/var/jenkins_home/example.txt"
for (line in fileContent.readLines()) {
stringList.add(line.split(",")[0] + ":selected");
}
return stringList
}

最新更新