[JIRA] (JENKINS-52555) Declarative pipeline functions cannot access defined environment variables from environment block

200 views
Skip to first unread message

brian.skarda@gmail.com (JIRA)

unread,
Jul 13, 2018, 5:11:01 PM7/13/18
to jenkinsc...@googlegroups.com
Brian Skarda created an issue
 
Jenkins / Bug JENKINS-52555
Declarative pipeline functions cannot access defined environment variables from environment block
Issue Type: Bug Bug
Assignee: Andrew Bayer
Components: pipeline-model-definition-plugin
Created: 2018-07-13 21:10
Environment: Jenkins version 2.89.3
pipeline-model-definition-plugin 1.2.7
Priority: Minor Minor
Reporter: Brian Skarda

Functions that are called from within the environment block do not have access to environment variables that are defined previously in the same environment block. Parameters work as expected and accessing the function outside of the environment block works as expected.

Example pipeline that exhibits the null environment variable function

#!/usr/bin/env groovy

def returnEnvVar() {
  env.VARIABLE
}

pipeline {
    environment {
      VARIABLE = "I'm set"
      COPY_VARIABLE = returnEnvVar() // this gets set to null
    }
    stages {
        stage('Print Environment') {
            steps {
                echo "${VARIABLE}"
                echo "${COPY_VARIABLE}" // this prints out null
            }
        }
    }
}
Add Comment Add Comment
 
This message was sent by Atlassian JIRA (v7.10.1#710002-sha1:6efc396)

andrew.bayer@gmail.com (JIRA)

unread,
Aug 9, 2018, 11:36:03 AM8/9/18
to jenkinsc...@googlegroups.com
Andrew Bayer commented on Bug JENKINS-52555
 
Re: Declarative pipeline functions cannot access defined environment variables from environment block

Yeah, that's expected - environment variable resolution is an annoyingly complicated thing, but the gist is that we resolve the values before any of them are added to env, and then add all of them to env together. You can reference one variable in another (i.e., in this case, COPY_VARIABLE = VARIABLE would work) because the resolution process knows to look for references to one of the variables in the value for another, but the referenced variable has to be referenced explicitly, not indirectly via a method.

brian.skarda@gmail.com (JIRA)

unread,
Aug 14, 2018, 10:20:01 AM8/14/18
to jenkinsc...@googlegroups.com

Ah, interesting. Thanks for the explanation Andrew. Is variable resolution documented anywhere or could I open a PR to add declarative pipeline variable resolution into one of the documentation sources?

io@zoto.io (JIRA)

unread,
Jan 16, 2020, 10:38:03 PM1/16/20
to jenkinsc...@googlegroups.com

This sounds somewhat related to an issue i am seeing.   Perhaps you can advise Andrew Bayer?

I want to call some shell commands to set env vars based on other env vars, and when they are cross-referenced, they appear to get re-evaluated.   Here is a basic example just using uuidgen - the use case is that the command setting SHARED_VALUE is not idempotent:

pipeline {
    agent any
    environment {        
        // SHARED_VALUE is re-evaluated each time it is referenced below
        SHARED_VALUE = sh(returnStdout: true, script: """uuidgen""").trim()        
        TEST_ONE = sh(returnStdout: true, script: """echo '${SHARED_VALUE}' """).trim()
        TEST_TWO = sh(returnStdout: true, script: """echo '${SHARED_VALUE}' """).trim()
        TEST_THR = sh(returnStdout: true, script: """echo '${SHARED_VALUE}' """).trim()    
    }    

    stages {        
        stage('Test') {            
            steps {                
                echo "JUST TESTING"                
                // each of these are different UUIDs
                sh '''                    
                    echo "ONE = $TEST_ONE"
                    echo "TWO = $TEST_TWO"
                    echo "THR = $TEST_THR"                
                '''               
             }
        }   
    }   
}

Is this expected behaviour?  i have not found a workaround, so have reverted to a bash script in the step instead.

This message was sent by Atlassian Jira (v7.13.6#713006-sha1:cc4451f)
Atlassian logo
Reply all
Reply to author
Forward
0 new messages