Jenkins Environment Parameters with groovy

I try to figure out how I can update the global Environment Parameter inside my Pipeline to use with all stages, all agents an all steps. It is hard to set this Environment at one place without to redefine them in each stage. As sample:

pipeline {
    agent { label 'linux'}
    environment {
        CC1="JonDoe" 
        CC2="DevOps"
        CC4=""
    }
    stages {
        stage('Example1') {
            environment {
                CC3="Compnay"
                CC1="Grandma"
                CC4="ALT"
            }
            steps {
                sh '''
                   #print Grandma
                    echo $CC1
                   #print DevOps
                    echo $CC2
                   #print Compnay
                    echo $CC3
                   #print  Alt
                    echo $CC4
                    printenv
                '''
            }
        }
        stage('Example2') {
            steps {
                sh '''
                    #Again JonDoe
                    echo $CC1
                    #Keep DevOps
                    echo $CC2
                    #Empty.not inside printenv
                    echo $CC3 
                    #Empty.not inside printenv
                    echo $CC4
                    printenv
                '''
            }
        }   
        
    }
}

So the global nevironment variable is just overwritten temporary.
This is annoying. So I started to research and found:

import hudson.EnvVars;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.slaves.NodeProperty;
import hudson.slaves.NodePropertyDescriptor;
import hudson.util.DescribableList;
import jenkins.model.Jenkins;
public createGlobalEnvironmentVariables(String key, String value){

       Jenkins instance = Jenkins.getInstance();
 
       DescribableList<NodeProperty<?>, NodePropertyDescriptor> globalNodeProperties = instance.getGlobalNodeProperties();
       List<EnvironmentVariablesNodeProperty> envVarsNodePropertyList = globalNodeProperties.getAll(EnvironmentVariablesNodeProperty.class);
 
       EnvironmentVariablesNodeProperty newEnvVarsNodeProperty = null;
       EnvVars envVars = null;
 
       if ( envVarsNodePropertyList == null || envVarsNodePropertyList.size() == 0 ) {
           newEnvVarsNodeProperty = new hudson.slaves.EnvironmentVariablesNodeProperty();
           globalNodeProperties.add(newEnvVarsNodeProperty);
           envVars = newEnvVarsNodeProperty.getEnvVars();           
       } else {
           //We do have a envVars List           
           envVars = envVarsNodePropertyList.get(0).getEnvVars();
       }
       envVars.put(key, value)

       instance.save()
}

I use with
createGlobalEnvironmentVariables("INGRESS","mydomain.local")
It works like a charm, the disadvantage is this Environment variable will keep in Jenkins because they are Jenkins global.
Anybody knows a similar method to set just the pipeline global environment? Like CC1=“JonDoe” to keep this in the whole pipeline? I think there have to be a groovy script but everything I tried out was not usefull. So I hope I can get a expert tipp here.

I think there have to be something because I also can set Parameters successful with Groovy while Pipeline is running.

I’m not sure why you would want to do that flow. Environment blocks inside a stage define variables (combined with top level/earlier levels) for that stage. Once you leave that stage, then they pop the scope. I’m not sure what scenario makes sense for you to change the global ones inside the declaration of a stage.

That being said, you can totally modify env variables very easily and done quite commonly, but not inside environment {}

script {
  env.FOO = "new value"
}

You need a script block for the assignment. I’m not sure what the behavior would be if you also have an environment {} block inside that scope, since I think it creates a new env for that scope. (Environment {} is declarative syntax for withEnv)

1 Like

I strongly need for this pipeline “global” values. I set them inside the first “Environment”. This is in my mind the part to define VAriables for the whole pipeline.
But I also have a prepare stage, where I fill this variable block. Everytime I try to refill the global block there is still the same value “JonDoe”, but never “JaneDoe” as sample.
The variable I defined inside this global block are needed inside 8 stages and it will be a overhead to define the same block in every stage again.