简体   繁体   中英

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. I am using Jenkins freestyle Job. The approach I have taken is as below:-

  1. Create a Global environment variable to store Incident Number and set a default value.
  2. Check the state of the incident in an upstream job and pass the state to the downstream job.
  3. In the downstream job, if the incident is still open, do nothing. If it is Closed/Resolved, create another Incident with some defined descriptions.
  4. Update the global environment variable with the current Incident no. and store it permanently.
  5. Check the state of the updated Incident Number(by accessing the global Environment Variable) and then follow the same process during subsequent builds.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM