我需要设置一个具有动态性的值build_ {build_number}(jenkins global}(的全局变量。如何在Jenkins Global Properties中进行设置?如何识别我指的构建号?
使用声明管道,您可以基于此其他环境变量(build_number(设置一个环境变量:
pipeline {
agent { label 'docker' }
environment {
MY_BUILD_IDENTIFIER = "build_${env.BUILD_NUMBER}"
}
stages {
stage('hot_stage') {
steps {
echo "MY_BUILD_IDENTIFIER: ${env.MY_BUILD_IDENTIFIER}"
}
}
}
}
产生这样的输出:
[Pipeline] echo
MY_BUILD_IDENTIFIER: build_153
这是一个示例脚本,如何更改全局环境变量:
nodes = Jenkins.instance.globalNodeProperties
nodes.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)
if ( nodes.size() != 1 ) {
println("error: unexpected number of environment variable containers: ${nodes.size()}, expected: 1")
} else {
envVars = nodes[0].envVars
envVars[args[0]] = args[1]
Jenkins.instance.save()
println("okay")
}
参考:https://gist.github.com/johnyzed/2AF71090419AF2B20C5A