Jenkins 환경 변수
List environment variables
Using env-vars.html
${JENKINS_HOST}/env-vars.html
Using shell command
pipeline {
agent any
stages {
stage("Env Variables") {
steps {
sh "printenv" | sort
}
}
}
}
Reading environment variables
pipeline {
agent any
stages {
stage("Env Variables") {
steps {
echo "The build number is ${env.BUILD_NUMBER}"
echo "You can also use \${BUILD_NUMBER} -> ${BUILD_NUMBER}"
sh 'echo "I can access $BUILD_NUMBER in shell command as well."'
}
}
}
}
Setting environment variables
pipeline {
agent any
environment {
FOO = "bar"
}
stages {
stage("Env Variables") {
environment {
NAME = "Alan"
}
steps {
echo "FOO = ${env.FOO}"
echo "NAME = ${env.NAME}"
script {
env.TEST_VARIABLE = "some test value"
}
echo "TEST_VARIABLE = ${env.TEST_VARIABLE}"
withEnv(["ANOTHER_ENV_VAR=here is some value"]) {
echo "ANOTHER_ENV_VAR = ${env.ANOTHER_ENV_VAR}"
}
}
}
}
}
Overriding environment variables
pipeline {
agent any
environment {
FOO = "bar"
NAME = "Joe"
}
stages {
stage("Env Variables") {
environment {
NAME = "Alan" // overrides pipeline level NAME env variable
BUILD_NUMBER = "2" // overrides the default BUILD_NUMBER
}
steps {
echo "FOO = ${env.FOO}" // prints "FOO = bar"
echo "NAME = ${env.NAME}" // prints "NAME = Alan"
echo "BUILD_NUMBER = ${env.BUILD_NUMBER}" // prints "BUILD_NUMBER = 2"
script {
env.SOMETHING = "1" // creates env.SOMETHING variable
}
}
}
stage("Override Variables") {
steps {
script {
env.FOO = "IT DOES NOT WORK!" // it can't override env.FOO declared at the pipeline (or stage) level
env.SOMETHING = "2" // it can override env variable created imperatively
}
echo "FOO = ${env.FOO}" // prints "FOO = bar"
echo "SOMETHING = ${env.SOMETHING}" // prints "SOMETHING = 2"
withEnv(["FOO=foobar"]) { // it can override any env variable
echo "FOO = ${env.FOO}" // prints "FOO = foobar"
}
withEnv(["BUILD_NUMBER=1"]) {
echo "BUILD_NUMBER = ${env.BUILD_NUMBER}" // prints "BUILD_NUMBER = 1"
}
}
}
}
}
Storing Boolean values in environment variables
모든 변수는 String 으로 저장된다.
pipeline {
agent any
environment {
IS_BOOLEAN = false
}
stages {
stage("Env Variables") {
steps {
// one way
script {
if (env.IS_BOOLEAN) {
echo "You can see this message, because \"false\" String evaluates to Boolean.TRUE value"
}
if (env.IS_BOOLEAN.toBoolean() == false) {
echo "You can see this message, because \"false\".toBoolean() returns Boolean.FALSE value"
}
}
// second way
when {
expression {
env.IS_BOOLEAN.toBoolean() == true
}
}
// third way
when {
environment name: "IS_BOOLEAN", value: "true" // syntax checking
}
}
}
}
}
Capturing the output from other steps
pipeline {
agent any
environment {
LS = "${sh(script:'ls -lah', returnStdout: true).trim()}"
}
stages {
stage("Env Variables") {
steps {
echo "LS = ${env.LS}"
}
}
}
}
pipeline {
agent any
stages {
stage("Env Variables") {
steps {
script {
env.LS = sh(script:'ls -lah', returnStdout: true).trim()
echo "LS = ${env.LS}"
// or if you access env variable in the shell command
sh 'echo $LS'
}
}
}
}
}
Dealing with null in the env variable
pipeline {
agent any
environment {
SOME_VAL = "${someFunc() ?: ''}"
}
stages {
stage("Env Variables") {
steps {
echo "Some value is ${env.SOME_VAL} of type class ${env.SOME_VAL?.class}"
}
}
}
}
def someFunc() {
return null
}
Reference
https://www.youtube.com/watch?v=KwQDxwZRZiE
https://e.printstacktrace.blog/jenkins-pipeline-environment-variables-the-definitive-guide/
https://github.com/wololock?tab=repositories&q=jenkin&type=&language=