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

Inconsistent Environment Variable interpolation in Declarative k8s agent

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • kubernetes-plugin
    • None

      Hi - There is inconsistent behavior of environment variable interpolation with the k8s plugin and declarative pipeline.

       

      In this case, there is inconsistent handling of a single quote vs double quote.

      In scripted, only double quote works -

       

       

      podTemplate(label: 'mypod', containers: [
          containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-8-alpine', ttyEnabled: true, command: 'cat'),
        ]) {
      
          node('mypod') {
              stage('Get a Maven project') {
                  withEnv(["SOME_VAR=someval"]) {
      //prints
                      echo "double quote - ${SOME_VAR}"
      //prints literal SOMEVAR
                      echo 'single quote - single ${SOME_VAR}'
                  }
                  container('maven') {
                      stage('Build a Maven project') {
                       withEnv(["SOME_VAR=someval"]) {
      //same
                          echo "double quote -${SOME_VAR}" 
                          echo 'single quote - ${SOME_VAR}'
                      }
                      }
                  }
              }
          }
        }
      

       

      in declarative pipeline, we see that this is not as true -

       

      pipeline {
        agent {
          kubernetes {
            //cloud 'kubernetes'
            label 'mypod'
            containerTemplate {
              name 'maven'
              image 'maven:3.3.9-jdk-8-alpine'
              ttyEnabled true
              command 'cat'
            }
          }
        }
        
          environment {
              TEST_VAR = 'TEST_VALUE'
          }
      
          stages {
              stage('JNLP env') {
                  steps {
                      echo 'Environment variables in JNLP:'
                      //Doesn't Print
                      sh 'echo ${TEST_VAR}'
                      //prints
                      sh "echo ${TEST_VAR}"
                      //prints
                      sh "echo ${env.TEST_VAR}"
                  }
              }
              stage('Container env') {
                  steps {
                      container('maven') {
                          echo 'Environment variables in container:'
                          sh 'env'
                          //PRINTS - works without double quote
                          sh 'echo ${TEST_VAR}'
                      }
                  }
              }
          }
        }    

       

       

            Unassigned Unassigned
            seakip18 Matthew Ludlum
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: