In Jenkins1 we had a script to set environment variables, something like that :
//...imports
instance = Jenkins.getInstance()
globalNodeProperties = instance.getGlobalNodeProperties()
envVarsNodePropertyList = globalNodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)
newEnvVarsNodeProperty = null
envVars = null
if ( envVarsNodePropertyList == null || envVarsNodePropertyList.size() == 0 ) {
newEnvVarsNodeProperty = new hudson.slaves.EnvironmentVariablesNodeProperty();
globalNodeProperties.add(newEnvVarsNodeProperty);
envVars = newEnvVarsNodeProperty.getEnvVars();
envVars.put(NEW_VAR, "toto");
} else {
for (property in envVarsNodePropertyList) {
envVars = property.getEnvVars();
envVars.put("EXISTING_VAR","tata");
}
}
instance.save()
Today we're using a Jenkins2, with the jenkinsfile feature and I wanted to know how to do the same thing into a jenkinsfile?
does this would work, if yes does there is a better way ? :
def setEnvVar() {
script {
//all the script above
}
}
pipeline {
agent any
tools {
maven 'Maven3_6'
jdk 'Jdk8'
}
stages {
stage ('Initialize') {
steps {
setEnvVar()
}
}
...
EDIT : let's say I have an existing env variable, LAST_DEPLOY = "0.1" using :
environment {
LAST_DEPLOY = "0.2"
}
will only modify the variable for the current execution and somehow at the end it will be reset to "0.1", I want the modification to last for the next builds.