I'm using following way to evaluate a shell script and use its output as environment variable:
pipeline {
agent { ... }
stages {
stage('Test') {
environment {
TEST_SCRIPT = "${sh(script: 'echo "This should be evaluated only once"', returnStdout: true)}"
TEST_USAGE = "${env.TEST_SCRIPT} and ${env.TEST_SCRIPT}"
}
steps {
sh 'printenv | grep TEST | sort'
}
}
}
}
Works as expected and output "This should be evaluated only once" is written to env var TEST_SCRIPT
. But when I'm using env var TEST_SCRIPT
in other env var (e.g. TEST_USAGE
in example), the script is evaluated multiple times:
Is it possible to store resulting output in TEST_SCRIPT
instead of executing it multiple times?