Conditional environment variables in Jenkins Decla

2020-07-02 22:15发布

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?

6条回答
叼着烟拽天下
2楼-- · 2020-07-02 22:36

Maybe you can try something like this:

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

UPGRADE (2nd option)

 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.

查看更多
贪生不怕死
3楼-- · 2020-07-02 22:37

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

查看更多
可以哭但决不认输i
4楼-- · 2020-07-02 22:44

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)

查看更多
仙女界的扛把子
5楼-- · 2020-07-02 22:48

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
                    }
                }
            }
        }
    }
}
查看更多
男人必须洒脱
6楼-- · 2020-07-02 22:54
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.

查看更多
够拽才男人
7楼-- · 2020-07-02 22:56

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'}"
}
查看更多
登录 后发表回答