I have a requirement where I want to create an Service Now Incident only when a previous similar incident is closed. I am using Jenkins freestyle Job. The approach I have taken is as below:-
Till now I have achieved till step 3 by using various plugins. 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. 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.
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):
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. 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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.