[英]How can I update a global Environment Variable using Groovy PostBuild in Jenkins permanently and use it in other subsequent builds?
I have a requirement where I want to create an Service Now Incident only when a previous similar incident is closed.我有一个要求,我只想在以前的类似事件关闭时才创建一个 Service Now 事件。 I am using Jenkins freestyle Job.
我正在使用 Jenkins 自由式工作。 The approach I have taken is as below:-
我采取的方法如下:-
Till now I have achieved till step 3 by using various plugins.到目前为止,我已经通过使用各种插件实现了第 3 步。 To achieve step 4, I am using groovy post-build plugin, so that I can update the global environment variable permanently, so that it is available in my next build, so that I can check the status of the incident before creating a new one.
为了实现第 4 步,我正在使用 groovy 构建后插件,这样我就可以永久更新全局环境变量,以便在我的下一个构建中可用,这样我就可以在创建新的之前检查事件的状态. I have come-up with a code, but it does not seem to update the variable permanently(Verified by printing the variable in a subsequent build).
我想出了一个代码,但它似乎并没有永久更新变量(通过在后续构建中打印变量来验证)。 The script also has other parts as per my requirement but SNOW_INC is the global variable that I want to update permanently, till the next build.
根据我的要求,该脚本还有其他部分,但 SNOW_INC 是我想要永久更新的全局变量,直到下一次构建。
import groovy.json.JsonSlurper
import hudson.model.*
def workspace = manager.build.getEnvVars()["WORKSPACE"]
def console = manager.listener.logger.&println
build = Thread.currentThread().executable
String jobName = build.project.getName()
job = Hudson.instance.getJob(jobName)
my_env_var = job.getLastBuild().getEnvironment()["SNOW_INC"]
console(my_env_var)
try{
def inputFile = new File(workspace, 'response.json')
if (inputFile != null){
JsonSlurper slurper = new JsonSlurper()
parsedJson = slurper.parse(inputFile)
String number= parsedJson.result.number
console("${number}")
System.setProperty("hudson.model.ParametersAction.safeParameters", "INC_NUMBER")
manager.build.addAction(
new ParametersAction(
new StringParameterValue("INC_NUMBER", "${number}")
)
)
manager.build.addAction(
new ParametersAction(
new StringParameterValue("SNOW_INC", "${number}")
)
)
console("Environment Variable set")
}
} catch (Exception e){
println("response.json file was not ")
}
Any help would be appreciated!任何帮助,将不胜感激!
Anyhow, found out the solution in few days.无论如何,几天后找到了解决方案。 So in order to modify the global environment variable that can be used across any jobs or any builds is as follows(this was used in Groovy Postbuild as mentioned):
因此,为了修改可在任何作业或任何构建中使用的全局环境变量,如下所示(如前所述,在 Groovy Postbuild 中使用):
nodes = Jenkins.getInstance().getGlobalNodeProperties()
nodes.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)
if ( nodes.size() != 1 ) {
console("error: unexpected number of environment variable containers: "
+ nodes.size()
+ " expected: 1")
} else {
envVars= nodes.get(0).getEnvVars()
envVars.put("SNOW_INC", "${number}")
Jenkins.getInstance().save()
}
So after any run where a new incident is being created, I am updating the environment variable "SNOW_INC" with the updated number.因此,在创建新事件的任何运行之后,我将使用更新后的编号更新环境变量“SNOW_INC”。 And then it is being used in another job to verify whether that incident is open or not according to the scenario.
然后在另一个工作中使用它来根据场景验证该事件是否开放。 Works like a charm!
奇迹般有效!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.