0

I would like to configure an environment variable for my Jenkins pipeline, but dynamically based on an input parameter to the build. I'm trying to configure my pipeline to set the KUBECONFIG environment variable for kubectl commands.

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.

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.

I've seen the EnvInject plugin mentioned, but was unable to get it working for a pipeline, and was struggling with the documentation.

nandilov
  • 699
  • 8
  • 18
zodac
  • 285
  • 10
  • 26
  • Have you tried setting the `environment` block to a global scope and setting the value by interpolating the value in the params Map vis a vis `environment { KUBECONFIG = "~/kubeconf/${params.CLUSTER_NAME}" }`? – Matthew Schuchard May 29 '20 at 13:32
  • Yes, it was giving syntax errors. It didn't like the ${params.CLUSTER_NAME} in the environment block. – zodac May 29 '20 at 13:40
  • Ok, so the `env` map is available in the `parameters` block, but the `params` map is not available in the `environment` block. That makes sense for declarative DSL loading, but was worth a guess. What about `env.KUBECONFIG = "~/kubeconf/${params.CLUSTER_NAME}"` at a global scope, or at least a scope that allows global accesibility? – Matthew Schuchard May 29 '20 at 13:49

1 Answers1

1

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. 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}"
            }
        }
    }
}
nandilov
  • 699
  • 8
  • 18