0

How do I access an existing environment variable SOME_VAR inside of the environment block?

I want to redefine it under a new name NEW_VAR?

pipeline {
    agent {
        label any
    }
    environment {
        SECRET = credentials('secret-credential') // works as expected
        NEW_VAR = ? // get from different env variable
    }
    stages {
        stage('Test') {
            steps {
                echo "SECRET=${env.SECRET}"
                echo "NEW_VAR=${env.NEW_VAR}"
            }
        }
    }
}

What I tried so far:

NEW_VAR = SOME_VAR        // fails build
NEW_VAR = env.SOME_VAR    // fails build
NEW_VAR = ${env.SOME_VAR} // fails build
NEW_VAR = env('SOME_VAR') // does not fail, but NEW_VAR is null
NEW_VAR = env(SOME_VAR)   // does not fail, but NEW_VAR is null
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
friederbluemle
  • 33,549
  • 14
  • 108
  • 109

1 Answers1

2

Use this:

environment {
    NEW_VAR = "${env.SOME_VAR}"
}

This way you're doing string interpolation, and then assigning string value to the new var.
Just tested, this should work.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62