Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-68875

pod envVars do not override environment variable defined before the pod creation

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not A Defect
    • Icon: Critical Critical
    • kubernetes-plugin
    • None
    • Jenkins 2.346.1
      kubernetes:3651.v908e7db_10d06

      please consider the following test pipeline

      
      void runInPod(String testMyEnvValue, Closure closure){
      
          String dockerRegistry = "<your-registry-here>"
          String workingDir = "/home/jenkins/agent"
          String userID = "1000"
         String gID = "1000"
         String uniqueLabel = "test-env-virables-${UUID.randomUUID().toString()}"
         String buildDockerImage = "${dockerRegistry}/ubuntu:20.04"
         String jnlpDockerImage = "${dockerRegistry}/jenkins/inbound-agent:4.11-1-alpine-jdk11"
         List imagePullSecrets = ["<your-pull-secret-here>"]
      
          steps.podTemplate(
              label: uniqueLabel,
              containers: [
                  steps.containerTemplate(
                      args: "",
                      command: "",
                      image: jnlpDockerImage,
                      name: "jnlp",
                      ttyEnabled: false,
                      workingDir: workingDir
                  ),
                  steps.containerTemplate(
                      args: "99d",
                      command: "sleep",
                      image: buildDockerImage,
                      name: "build",
                      resourceRequestCpu: "1",
                      resourceLimitCpu: "1",
                      resourceRequestMemory: "1Gi",
                      resourceLimitMemory: "1Gi",
                      ttyEnabled: false,
                      workingDir: workingDir
                  ),
              ],
              // Setting environment variables for the pod
              envVars: [
                  steps.envVar(key: 'MY_ENV', value: "${testMyEnvValue}")
              ],
              runAsUser: userID,
              runAsGroup: gID,
              nodeUsageMode: "EXCLUSIVE",
              nodeSelector: "beta.kubernetes.io/os=linux",
              imagePullSecrets: imagePullSecrets,
              workspaceVolume:
                  steps.emptyDirWorkspaceVolume(memory: false)
          ){
              node(uniqueLabel) {
                  closure()
              }
          }
      }
      
      stage("nested pods"){
          runInPod("1"){
              sh("echo MY_ENV == \$MY_ENV") // expected to get 1 -- passed
      
              runInPod("2"){
                  sh("echo MY_ENV == \$MY_ENV") // expected to get 2 -- passed 
              }
          }
      }
      
      stage("pod should override env value"){
      
          withEnv(["MY_ENV=0"]){
              runInPod("1"){
                  sh("echo MY_ENV == \$MY_ENV") // expected to get 1 -- failed got 0
              }
          }
      }
      
      
      stage("wnv value defined first  + nested pods"){
          env.MY_ENV="00"
          runInPod("1"){
              sh("echo MY_ENV == \$MY_ENV") // expected to get 1 -- failed got 00
      
              runInPod("2"){
                  sh("echo MY_ENV == \$MY_ENV") // expected to get 2 -- failed got 00
              }
          }
      }
      

      the envVars set in the pod definition cannot override the values that were set before the pod creation ( set using withEnv() or env.<> = <> or even global jenkins properties )

            Unassigned Unassigned
            ysmaoui Yacine
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: