Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
...
like image 887
Daniel Juravski Avatar asked Nov 04 '19 13:11

Daniel Juravski


People also ask

How do you declare a variable in Jenkinsfile?

Environment variables can be defined using NAME = VALUE syntax. To access the variable value you can use these three methods $env.NAME , $NAME or ${NAME} There are no differences between these methods. Here is a complete pipeline sample with environment variables example.

How do you transfer workspace and environment variable in a pipeline to the next job?

Under Build Environment check Set environment variables through a file. give the path of that file here. If the environment variable is created in the first job then again you can save all the environment variable in a file and browse it using the above method. Install this plugin and go to job configuration paeg.


1 Answers

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.

like image 159
Szymon Stepniak Avatar answered Oct 14 '22 19:10

Szymon Stepniak