50

I'm trying to get a declarative pipeline that looks like this:

pipeline {
    environment {
        ENV1 = 'default'
        ENV2 = 'default also'
    }
}

The catch is, I'd like to be able to override the values of ENV1 or ENV2 based on an arbitrary condition. My current need is just to base it off the branch but I could imagine more complicated conditions.

Is there any sane way to implement this? I've seen some examples online that do something like:

stages {
    stage('Set environment') {
        steps {
            script {
                ENV1 = 'new1'
            }
        }
    }
}

But I believe this isn't setting the actually environment variable, so much as it is setting a local variable which is overriding later calls to ENV1. The problem is, I need these environment variables read by a nodejs script, and those need to be real machine environment variables.

Is there any way to set environment variables to be dynamic in a jenkinsfile?

7 Answers 7

119

Maybe you can try Groovy's ternary-operator:

pipeline {
    agent any
    environment {
        ENV_NAME = "${env.BRANCH_NAME == "develop" ? "staging" : "production"}"
    }
}

or extract the conditional to a function:

 pipeline {
        agent any
        environment {
           ENV_NAME = getEnvName(env.BRANCH_NAME)
        }
    }

// ...

def getEnvName(branchName) {
    if("int".equals(branchName)) {
        return "int";
    } else if ("production".equals(branchName)) {
        return "prod";
    } else {
        return "dev";
    }
}

But, actually, you can do whatever you want using the Groovy syntax (features that are supported by Jenkins at least)

So the most flexible option would be to play with regex and branch names...So you can fully support Git Flow if that's the way you do it at VCS level.

Sign up to request clarification or add additional context in comments.

7 Comments

amazing, this works perfectly; my example is environment { branch = env.BRANCH_NAME ?: 'tester' } needs to be environment { "${branch = env.BRANCH_NAME ?: 'tester'}"
How can we use this solution when we have 3 conditions?
@Sumith08 - any logic should work within the closure in the example. It's a really neat trick to set env vars dynamically.
This doesn't work. First of all the quotes are wrong. After changing to single quotes inside the double quotes, Jenkins still blows up on this syntax.
The answer is updated according to your concerns. Thx
|
14

use withEnv to set environment variables dynamically for use in a certain part of your pipeline (when running your node script, for example). like this (replace the contents of an sh step with your node script):

pipeline {
    agent { label 'docker' }
    environment {
        ENV1 = 'default'
    }
    stages {
        stage('Set environment') {
            steps {
                sh "echo $ENV1" // prints default
                // override with hardcoded value
                withEnv(['ENV1=newvalue']) {
                    sh "echo $ENV1" // prints newvalue
                }
                // override with variable
                script {
                    def newEnv1 = 'new1'
                    withEnv(['ENV1=' + newEnv1]) {
                        sh "echo $ENV1" // prints new1
                    }
                }
            }
        }
    }
}

1 Comment

Ah yes, this does work. It's unfortunate that there is no way to do it for all stages, but this will have to do for now. Thanks!
11

Here is the correct syntax to conditionally set a variable in the environment section.

environment {
    MASTER_DEPLOY_ENV = "TEST" // Likely set as a pipeline parameter
    RELEASE_DEPLOY_ENV = "PROD" // Likely set as a pipeline parameter
    DEPLOY_ENV = "${env.BRANCH_NAME == 'master' ? env.MASTER_DEPLOY_ENV : env.RELEASE_DEPLOY_ENV}"
    CONFIG_ENV = "${env.BRANCH_NAME == 'master' ? 'MASTER' : 'RELEASE'}"
}

3 Comments

Can we do it in parameters{} section? I tried to do it but it does not like it at all.
@AbidKhan I have not used the parameters section, so I couldn't say.
The ternary-operator as assignment solution was already answered by Drasko years ago. What is the difference here if any?
2

I managed to get this working by explicitly calling shell in the environment section, like so:

UPDATE_SITE_REMOTE_SUFFIX = sh(returnStdout: true, script: "if [ \"$GIT_BRANCH\" == \"develop\" ]; then echo \"\"; else echo \"-$GIT_BRANCH\"; fi").trim()

however I know that my Jenkins is on nix, so it's probably not that portable

Comments

2

Here is a way to set the environment variables with high flexibility, using maps:

    stage("Environment_0") {
        steps {
            script {
                def MY_MAP = [ME: "ASSAFP", YOU: "YOUR_NAME", HE: "HIS_NAME"]
                env.var3 = "HE"
                env.my_env1 = env.null_var ? "not taken" : MY_MAP."${env.var3}"
                echo("env.my_env1: ${env.my_env1}")                 
           }
        }
    }

This way gives a wide variety of options, and if it is not enough, map-of-maps can be used to enlarge the span even more. Of course, the switching can be done by using input parameters, so the environment variables will be set according to the input parameters value.

Comments

-1
pipeline {
    agent none
    environment {
        ENV1 = 'default'
        ENV2 = 'default'
    }
    stages {
        stage('Preparation') {
            steps {
                script {
                    ENV1 = 'foo' // or variable
                    ENV2 = 'bar' // or variable
                }
                echo ENV1
                echo ENV2
            }
        }
        stage('Build') {
            steps {
                sh "echo ${ENV1} and ${ENV2}"
            }
        }
        // more stages...
    }
}

This method is more simple and looks better. Overridden environment variables will be applied to all other stages also.

2 Comments

Actually this doesn't overwrite the environment variable, but defines a new global-scope Groovy variable (I think) Try to echo ${env.ENV1} int the Build stage and you'll see that it still has the original value!
environment{} can be used at the stage level for that. However it seems that it needs to be repeated for every stage where it is needed.
-1

I tried to do it in a different way, but unfortunately it does not entirely work:

pipeline {
  agent any
  environment {
    TARGET = "${changeRequest() ? CHANGE_TARGET:BRANCH_NAME}"
  }

  stages {
    stage('setup') {
        steps {
            echo "target=${TARGET}"
            echo "${BRANCH_NAME}"
        }
    }
  }
}

Strangely enough this works for my pull request builds (changeRequest() returning true and TARGET becoming my target branch name) but it does not work for my CI builds (in which case the branch name is e.g. release/201808 but the resulting TARGET evaluating to null)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.