Load file with environment variables Jenkins Pipeline

From the comments to the accepted answer

Don't use global 'env' but use 'withEnv' construct, eg see: issue #9: don't set env vars with global env in top 10 best practices jenkins pipeline plugin

In the following example: VAR1 is a plain java string (no groovy variable expansion), VAR2 is a groovy string (so variable 'someGroovyVar' is expanded).

The passed script is a plain java string, so $VAR1 and $VAR2 are passed literally to the shell, and the echo's are accessing environment variables VAR1 and VAR2.

stage('build') {
    def someGroovyVar = 'Hello world'
    withEnv(['VAR1=VALUE ONE',
             "VAR2=${someGroovyVar}"
            ]) {
        def result = sh(script: 'echo $VAR1; echo $VAR2', returnStdout: true)
        echo result
    }
}

For secrets / passwords you can use credentials binding plugin

Example:

NOTE: CREDENTIALS_ID1 is a registered username/password secret on the Jenkins settings.

stage('Push') {
    withCredentials([usernamePassword(
                         credentialsId: 'CREDENTIALS_ID1',
                         passwordVariable: 'PASSWORD',
                         usernameVariable: 'USER')]) {
        echo "User name: $USER"
        echo "Password:  $PASSWORD"
    }
}

The jenkisn console log output hides the real values:

[Pipeline] echo
User name: ****
[Pipeline] echo
Password:  ****

Jenkins and credentials is a big issue, probably see: credentials plugin

For completeness: Most of the time, we need the secrets in environment variables, as we use them from shell scripts, so we combine the withCredentials and withEnv like follows:

stage('Push') {
    withCredentials([usernamePassword(
                         credentialsId: 'CREDENTIALS_ID1',
                         passwordVariable: 'PASSWORD',
                         usernameVariable: 'USER')]) {
        withEnv(["ENV_USERNAME=${USER}",
                 "ENV_PASSWORD=${PASSWORD}"
            ]) {
                def result = sh(script: 'echo $ENV_USERNAME', returnStdout: true)
                echo result

        }
    }
}

Another way to resolve this install 'Pipeline Utility Steps' plugin that provides us readProperties method ( for reference please go to the link https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#pipeline-utility-steps) Here in the example we can see that they are storing the keys into an array and using the keys to retrieve the value. But in that case the in production the problem will be like if we add any variable later into property file that variable needs to be added into the array of Jenkins file as well. To get rid of this tight coupling, we can write code in such a way so that the Jenkins build environment can get information automatically about all the existing keys which presents currently in the Property file. Here is an example for the reference

def loadEnvironmentVariables(path){
    def props = readProperties  file: path
    keys= props.keySet()
    for(key in keys) {
        value = props["${key}"]
        env."${key}" = "${value}"
    }
} 

And the client code looks like

path = '\\ABS_Output\\EnvVars\\pic_env_vars.properties'
loadEnvironmentVariables(path)

One way you could load environment variables from a file is to load a Groovy file.

For example:

  1. Let's say you have a groovy file in '$JENKINS_HOME/.envvars' called 'stacktest-staging.groovy'.
  2. Inside this file, you define 2 environment variables you want to load

    env.DB_URL="hello"
    env.DB_URL2="hello2"
    
  3. You can then load this in using

    load "$JENKINS_HOME/.envvars/stacktest-staging.groovy"
    
  4. Then you can use them in subsequent echo/shell steps.

For example, here is a short pipeline script:

node {
   load "$JENKINS_HOME/.envvars/stacktest-staging.groovy"
   echo "${env.DB_URL}"
   echo "${env.DB_URL2}"
}