'Importing map variable to Jenkinsfile environment stage

My project has many common variables for many other projects, so I use Jenkins Shared Library and created a vars/my_vars.groovy file where I defined my variables and return Map of them:

class my_vars {
    static Map varMap = [:]
    static def loadVars (Map config) {
        varMap.var1 = "val1"
        varMap.var2 = "val2"
        // Many more variables ...

        return varMap
    }
}

I load the Shared Library in my Jenkinsfile, and call the function in the environment bullet, as I want those variables to be as environment variables .

Jenkinsfile:

pipeline {

    environment {
        // initialize common vars
        common_vars = my_vars.loadVars()
    } // environment

    stages {
        stage('Some Stage') {
            // ...
        }
    }

    post {
        always { 
            script {
                // Print environment variables
                sh "env"
            } // script
        } // always
    } // post

} // pipeline

The thing is that the environment bullet gets KEY=VALUE pairs, thus my common_vars map is loaded like a String value (I can see that on sh "env").

...
vars=[var1:val1, var2:val2]
...

What is the correct way to declare those values as an environment variables? My target to get this:

...
var1=val1
var2=val2
...


Solution 1:[1]

Pipeline's environment variables store only String values. That is why when you assign a map to env.common_vars variables it stores map.toString() equivalent.

If you want to rewrite key-values from a map to the environment variables, you can iterate the variables map and assign each k-v pair to something like env."$k" = v. You can do that by calling a class method inside the environment block - that way you can be sure that the environment variables are assigned no matter which stage your pipeline gets restarted from. Consider the following example:

class MyVars {
    private Map config = [
        var1: "val1",
        var2: "val2"
    ]

    String initializeEnvironmentVariables(final Script script) {
        config.each { k,v ->
            script.env."$k" = v
        }

        return "Initialization of env variables completed!"
    }
}

pipeline {
    agent any

    environment {
        INITIALIZE_ENV_VARIABLES_FROM_MAP = "${new MyVars().initializeEnvironmentVariables(this)}"
    }

    stages {
        stage("Some stage") {
            steps {
                echo "env.var1 = ${env.var1}"
            }
        }
    }

    post {
        always {
            script {
                sh 'printenv | grep "var[0-9]\\+"'
            }
        }
    }
}

In this example, we use MyVars class to store some global config map (it can be a part of a shared library, here, for simplicity, it is a part of the Jenkinsfile). We use INITIALIZE_ENV_VARIABLES_FROM_MAP environment variable assignment to call MyVars.initializeEnvironmentVariables(this) method that can access env from the script parameter. Calling this method from inside environment block has one significant benefit - it guarantees that environment variables will be initialized even if you restart the pipeline from any stage.

And here is the output of this exemplary pipeline:

Running on Jenkins in /home/wololock/.jenkins/workspace/pipeline-env-map
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Some stage)
[Pipeline] echo
env.var1 = val1
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ grep 'var[0-9]\+'
+ printenv
var1=val1
var2=val2
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

As you can see we it sets env.var1 and env.var2 from the map encapsulated in MyVars class. Both variables can be accessed inside the pipeline step, script block or even inside the shell environment variables.

Solution 2:[2]

As far as I know there is no easy way to do this in declarative pipeline (e.g. in the environment directive. Instead, what you can do is to setup the environment outside of the declarative definition, like this:

my_vars.loadVars().each { key, value ->
    env[key] = value
}
// Followed by your pipelines definition:
pipeline {
    stages {
        stage('Some Stage') {
            // ...
        }
    }
    // ...
} // pipeline

As an full example:

class my_vars {
    static Map varMap = [:]
    static def loadVars (Map config) {
        varMap.var1 = "val1"
        varMap.var2 = "val2"
        // Many more variables ...

        return varMap
    }
}

my_vars.loadVars().each { key, value ->
    env[key] = value
}

pipeline {
    agent any
    stages {
        stage("Some stage") {
            steps {
                echo "env.var1 = ${env.var1}"
            }
        }
    }
}

Which outputs the following when built:

Started by user xxx
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on yyy in /zzz
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Some stage)
[Pipeline] echo
env.var1 = val1
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Edit; If your class (my_vars) is located in a shared library (MySharedLibrary):

library 'MySharedLibrary' // Will load vars/my_vars.groovy
my_vars.loadVars().each { key, value ->
    env[key] = value
}

pipeline {
    agent any
    stages {
        stage("Some stage") {
            steps {
                echo "env.var1 = ${env.var1}"
            }
        }
    }
}

Solution 3:[3]

You don't have to return a map of your environment variables from your shared library. You can simply set them in a shared library method, the method will run in the same container as your pipeline.

In you shared library vars/ directory:

def setVars() {
    env.var1 = "var1"
    env.var2 = "var2"
    env.var3 = "var3"
}

In your pipeline:

pipeline {
    agent any
    stages {
        stage("Setup") {
            steps {
                script {
                    imported_shared_lib.setVars()
                }
            }
        }
    }
}

Others mentioned the need to preserve the environment variables even if you restart the pipeline from a certain stage. In my experiments, the variables are preserved using this method, even if the setVars() method is not called in the environment{} block.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Szymon Stepniak
Solution 2
Solution 3 Kuranes