Jenkins声明式管道中的条件环境变量 [英] Conditional environment variables in Jenkins Declarative Pipeline

查看:91
本文介绍了Jenkins声明式管道中的条件环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获得一个如下所示的声明性管道:

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

问题是,我希望能够基于任意条件来覆盖ENV1或ENV2的值.我目前的需求只是将其作为分支机构的基础,但我可以想象到更复杂的情况.

有没有理智的方法来实现这一目标?我在网上看到了一些类似的例子:

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

但是我相信这并没有设置实际的环境变量,而是设置了局部变量,该局部变量将覆盖以后对ENV1的调用.问题是,我需要由nodejs脚本读取这些环境变量,而这些则必须是真实的机器环境变量.

有什么方法可以在jenkinsfile中将环境变量设置为动态的吗?

解决方案

使用withEnv动态设置环境变量以用于管道的特定部分(例如,在运行节点脚本时).像这样(用您的节点脚本替换sh步骤的内容):

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
                    }
                }
            }
        }
    }
}

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?

解决方案

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
                    }
                }
            }
        }
    }
}

这篇关于Jenkins声明式管道中的条件环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆