How do I set env variable to currentBuild property in declarative pipeline environment.

1,404 views
Skip to first unread message

JG

unread,
May 10, 2017, 11:20:45 AM5/10/17
to Jenkins Users
I'm trying to set an env variable globally in my pipeline {} with a concatenation of build details like this

#!groovy
pipeline {
    agent {
        label "linux"
    }
      parameters {
        string(name: "releaseName", defaultValue: "RM_TEST", description: "Ivy release spec")
    }
    tools {
        jdk 'jdk1.8.0_102'
        maven 'maven-3.2.5'
    }
    environment {
        BUILD_REVISION = "${params.releaseName}.${env.BRANCH_NAME}.${currentBuild.startTimeInMillis}.${env.BUILD_ID}"
        RELEASE_NAME = "${params.releaseName}"
    }
    stages {
        stage("dummy") {
            steps {
                sh "echo \"${params.releaseName}.${env.BRANCH_NAME}.${currentBuild.startTimeInMillis}.${env.BUILD_ID}\""
                sh "env | sort"
            }
        }
    }
}

and it seems to be silently failing. The echo command prints the value as expected, but the env does not include a BUILD_REVISION variable (the RELEASE_NAME env variable is shown in the output of env. I also tried setting the BUILD_REVISION in an environment{} block within the stage, and also tried using a string property of currentBuild. Nothing seems to work.

It seems to work using withEnv 

steps {
    sh "echo \"${params.releaseName}.${env.BRANCH_NAME}.${currentBuild.startTimeInMillis}.${env.BUILD_ID}\""
    withEnv(["BUILD_REVISION=${params.releaseName}.${env.BRANCH_NAME}.${currentBuild.startTimeInMillis}.${env.BUILD_ID}"]) {
        sh "env | sort"
    }
}

But I'll need to use the variable in multiple stages so I'd prefer it to be in a global environment{} block.

Cuong Tran

unread,
May 10, 2017, 1:07:31 PM5/10/17
to Jenkins Users
Does it work with this?

withEnv(["BUILD_REVISION=${env.BUILD_REVISION}"]) {
        sh "env | sort"

JG

unread,
May 10, 2017, 1:42:11 PM5/10/17
to Jenkins Users
Unfortunately not.

[branch_jenkins-dummy_master-ZKZSOZRU2C4BR7MTP6RRGC4D2OPUWJONM6DQP6YFBP3UB5R3DCYQ] Running shell script
+ env
+ sort
BRANCH_NAME=master
BUILD_DISPLAY_NAME=#13
BUILD_ID=13
BUILD_NUMBER=13
BUILD_REVISION=null
BUILD_TAG=jenkins-testing-multibranch-jenkins-dummy-master-13
...

It's specifically the inclusion of ${currentBuild.property} in the variable declaration within the environment{} block that leaves the variable null.

Kevin Burnett

unread,
May 10, 2017, 11:31:23 PM5/10/17
to Jenkins Users
This is a way to accomplish what you're trying to do:

#!groovy

// assign to this variable in the first stage (see below), and then you can use it throughout your pipeline
// not quite as clean as using the environment block, but it works. the problem seems to be that
// the environment block is "compiled" at the same time as the parameters block, etc, so the things
// that you want to use to set up an environment variable (params, other env vars) are not yet available.
def buildRevision = "UNKNOWN"


pipeline
{
    agent
{
        label
"linux"
   
}
    parameters
{
       
string(name: "releaseName", defaultValue: "RM_TEST", description: "Ivy release spec")
   
}
    tools
{
        jdk
'jdk1.8.0_102'
        maven
'maven-3.2.5'
   
}

    stages
{
        stage
("dummy") {
            steps
{

                script
{
                    buildRevision
= "${params.releaseName}.${env.BRANCH_NAME}.${currentBuild.startTimeInMillis}.${env.BUILD_ID}"
               
}  
                echo
"buildRevision: ${buildRevision}"
           
}
       
}
   
}
}

JG

unread,
May 11, 2017, 4:46:20 PM5/11/17
to Jenkins Users
Thanks! This is working for me. However contrary to your comment about parameters and environment, I am able to set a variable in environment block referencing the value of a parameter. It's only when I included the ${currentBuild} reference that it wouldn't set the variable.

Dan Tran

unread,
May 11, 2017, 6:10:24 PM5/11/17
to Jenkins Users
I am on the same boat, need to set an env var during built to be picked up by emailExt jelly template. Would this work?  agent set the env, master sends mail with env token replacement?

Thanks

-Dan
Reply all
Reply to author
Forward
0 new messages