尝试从属性文件中读取和使用节点标签,如下所示,我hello/world.jenkins
这是我的 JenkinsFile 和hello/world
签入的文件,其中有一些属性,包括NODE_LABEL=my-server-name
,两个文件都在 git 中,我正在使用Pipeline script from SCM
定义和hello/world.jenkins
作为 Jenkins 管道配置中的脚本路径。
def scriptPath = currentBuild.rawBuild.parent.definition.scriptPath // hello/world.jenkins
String fileWithoutExt = scriptPath.take(scriptPath.lastIndexOf('.')) // hello/world
println "props_file=" + fileWithoutExt // prints correctly.
properties = readProperties file: "$fileWithoutExt" // here it fails, I could see hello/world file present in the workspace
echo "node: ${properties.NODE_LABEL}"
pipeline {
agent { label props1.NODE_LABEL }
...
stages {
...
}
}
我无法在stage
之外加载属性文件,还有其他方法可以读取属性文件的节点名称吗?
.log:
props_file=hello/world
[Pipeline] readProperties
[Pipeline] End of Pipeline
org.jenkinsci.plugins.workflow.steps.MissingContextVariableException: Required context class hudson.FilePath is missing
properties
不可见,因为它未声明为全局环境变量。请改为执行以下操作:
env.properties = readProperties file: "$fileWithoutExt"
这有效:
def scriptPath = currentBuild.rawBuild.parent.definition.scriptPath // hello/world.jenkins
String fileWithoutExt = scriptPath.take(scriptPath.lastIndexOf('.')) // hello/world
pipeline {
environment {
nodeProp = readProperties file: "${fileWithoutExt}"
nodeLabel = "$nodeProp.NODE_LABEL"
}
agent { label env.nodeLabel }
...