Jenkins: Environment
Jenkins 环境变量就是通过 env 关键字暴露出来的全局变量,可以在 Jenkins 文件的任何位置使用
查看 Jenkins 系统内置环境变量
- ${JENKINS_URL}/env-vars.html
- through pipeline
pipeline { agent any stages { stage('Environment') { steps { sh 'printenv | sort' } } }

Shell 结果赋值给环境变量
实现这种方式很简单,只需要记住一个格式:sh(script: 'cmd', returnStdout:true)
environment { v1=11 } env.v5=5555555 def v6=666666 pipeline { agent any environment { v2=22 } options { ansiColor('xterm') } stages { stage('Hello') { environment { v7=77777777 LS="${sh(script:'ls -alh', returnStdout: true)}" } steps { // environment can't locate in steps echo '${env.BUILD_NUMBER}' echo "${env.BUILD_NUMBER} ${v6}" // can't access v4 echo "LS = ${env.LS}" // def b=8888 should use in script {} withEnv(['t=tttttttttttt', "tt=${JENKINS_URL}"]) { // can override any env variables sh ''' env | sort ''' } script { env.v3=33 env.v5=5555555555555555555555555 // can override env.v5 (declared imperatively) env.v7=77 // can't override env.v7 (declared declaratively) v2='ttttttt' v4=44 // can be access from other stages println "false".toBoolean() sh ''' echo "\033[7m$JOB_DISPLAY_URL\033[0m" echo ${!v*} printenv | sort ''' echo "stage Hello: v2=${v2} v4=${v4} env.v2=${env.v2} v6=${v6}" ansiColor("xterm") { sh """ echo "\033[32m$JOB_DISPLAY_URL\033[0m" """ echo "\u001B[31mI'm Red\u001B[0m Now not" } } echo "v4=${v4} ${JENKINS_HOME}" } } stage('Environment') { steps { sh ''' echo ${!v*} printenv | sort ''' echo "stage environment: v2=${v2} v4=${v4} env.v2=${env.v2} v6=${v6}" } } } }