简体   繁体   English

从构建参数设置 Jenkins 管道阶段的环境变量

[英]Setting environment variable in Jenkins pipeline stage from build parameter

I would like to configure an environment variable for my Jenkins pipeline, but dynamically based on an input parameter to the build.我想为我的 Jenkins 管道配置一个环境变量,但动态地基于构建的输入参数。 I'm trying to configure my pipeline to set the KUBECONFIG environment variable for kubectl commands.我正在尝试配置我的管道以设置kubectl命令的 KUBECONFIG 环境变量。

My pipeline is as follows (slightly changed):我的管道如下(略有改动):

pipeline {

    parameters {
        choice(name: 'CLUSTER_NAME', choices: 'cluster1/cluster2')
    }

    stages {
        // Parallel stages since only one environment variable should be set, based on input
        stage ('Set environment variable') {
            parallel {
                stage ('Set cluster1') {
                    when {
                        expression {
                            params.CLUSTER_NAME == "cluster1"
                        }
                    }
                    environment {
                        KUBECONFIG = "~/kubeconf/cluster1.conf"
                    }
                    steps {
                        echo "Using KUBECONFIG: ${env.KUBECONFIG}"
                    }
                }

                stage ('Set cluster2') {
                    when {
                        expression {
                            params.CLUSTER_NAME == "cluster2"
                        }
                    }
                    environment {
                        KUBECONFIG = "~/kubeconf/cluster2.conf"
                    }
                    steps {
                        echo "Using KUBECONFIG: ${env.KUBECONFIG}"
                    }
                }
            }
        }

        stage ('Test env') {
            steps {
                sh "cat ${env.KUBECONFIG}"
            }
        }
    }
}

However, while the stage where I set the environment variable can print it, once I move to another stage I only get null.然而,虽然我设置环境变量的阶段可以打印它,但一旦我移动到另一个阶段,我只会得到 null。

Is there some way of sharing env variables between stages?有没有办法在阶段之间共享环境变量? Since I'd like to use the default KUBECONFIG command (and not specify a file/context in my kubectl commands), it would be much easier to find a way to dynamically set the env variable.由于我想使用默认的 KUBECONFIG 命令(而不是在我的kubectl命令中指定文件/上下文),因此找到一种动态设置 env 变量的方法会容易得多。

I've seen the EnvInject plugin mentioned, but was unable to get it working for a pipeline, and was struggling with the documentation.我已经看到提到的EnvInject插件,但无法使其适用于管道,并且在文档中苦苦挣扎。

I guess that with the environment{} you are setting the environment variable only for the stage where it runs - it is not affecting the context of environment of the pipeline itself.我猜想使用environment{}您只是为它运行的阶段设置环境变量 - 它不会影响管道本身的环境上下文。 Set environment variables like below to affect the main context.设置如下环境变量以影响主上下文。 Works for me.为我工作。

pipeline {

    agent any

    parameters {
        choice(name: 'CLUSTER_NAME', choices: 'cluster1\ncluster2')
    }

    stages {
        // Parallel stages since only one environment variable should be set, based on input
        stage ('Set environment variable') {
            parallel {
                stage ('Set cluster1') {
                    when {
                        expression {
                            params.CLUSTER_NAME == "cluster1"
                        }
                    }
                    steps {
                        script{
                            env.KUBECONFIG = "~/kubeconf/cluster1.conf"
                            echo "Using KUBECONFIG: ${env.KUBECONFIG}"    
                        }
                    }
                }

                stage ('Set cluster2') {
                    when {
                        expression {
                            params.CLUSTER_NAME == "cluster2"
                        }
                    }
                    steps {
                        script{
                            env.KUBECONFIG = "~/kubeconf/cluster2.conf"
                            echo "Using KUBECONFIG: ${env.KUBECONFIG}"
                        }
                    }
                }
            }
        }
        stage ('Test env') {
            steps {
                sh "cat ${env.KUBECONFIG}"
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM