共计 4015 个字符,预计需要花费 11 分钟才能阅读完成。
Jenkins流水线中的环境变量(全局或局部)如何声明和使用?我们看一下一个最简单的示例,看看jenkins中默认的环境变量都有哪些?
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo 'Hello World'
// 使用sh执行命令来打印env
sh 'env'
// 若使用println打印env则会输出: org.jenkinsci.plugins.workflow.cps.EnvActionImpl@21a4932b
// 此时需要去系统配置中去做脚本批准,或者取消沙盒中运行
// println(env)
}
}
}
}
对应的输出如下
Started by user cicd-admin
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/pipeline研究/project-env
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] echo
Hello World
[Pipeline] sh
+ env
JENKINS_NODE_COOKIE=677c4ed8-17f1-4368-8a98-eec57048381e
BUILD_URL=http://jenkins.xadocker.cn:8080/job/pipeline%E7%A0%94%E7%A9%B6/job/project-env/3/
SHELL=/bin/false
HUDSON_SERVER_COOKIE=5a7b0b305d27d9bb
STAGE_NAME=Hello
BUILD_TAG=jenkins-pipeline研究-project-env-3
JOB_URL=http://jenkins.xadocker.cn:8080/job/pipeline%E7%A0%94%E7%A9%B6/job/project-env/
WORKSPACE=/var/lib/jenkins/workspace/pipeline研究/project-env
BUILD_TIMESTAMP_YESTERDAY=2019-05-18 17:10:20 CST
RUN_CHANGES_DISPLAY_URL=http://jenkins.xadocker.cn:8080/job/pipeline%E7%A0%94%E7%A9%B6/job/project-env/3/display/redirect?page=changes
USER=jenkins
RUN_ARTIFACTS_DISPLAY_URL=http://jenkins.xadocker.cn:8080/job/pipeline%E7%A0%94%E7%A9%B6/job/project-env/3/display/redirect?page=artifacts
JENKINS_HOME=/var/lib/jenkins
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
RUN_DISPLAY_URL=http://jenkins.xadocker.cn:8080/job/pipeline%E7%A0%94%E7%A9%B6/job/project-env/3/display/redirect
_=/usr/bin/env
PWD=/var/lib/jenkins/workspace/pipeline研究/project-env
HUDSON_URL=http://jenkins.xadocker.cn:8080/
LANG=en_US.UTF-8
JOB_NAME=pipeline研究/project-env
BUILD_DISPLAY_NAME=#3
BUILD_ID=3
JENKINS_URL=http://jenkins.xadocker.cn:8080/
NOTIFY_SOCKET=/run/systemd/notify
BUILD_TIMESTAMP=2019-05-19 17:10:20 CST
JOB_BASE_NAME=project-env
RUN_TESTS_DISPLAY_URL=http://jenkins.xadocker.cn:8080/job/pipeline%E7%A0%94%E7%A9%B6/job/project-env/3/display/redirect?page=tests
HOME=/var/lib/jenkins
SHLVL=2
CI=true
WORKSPACE_TMP=/var/lib/jenkins/workspace/pipeline研究/project-env@tmp
EXECUTOR_NUMBER=1
JENKINS_SERVER_COOKIE=durable-c9140363b0cd3dd0109f2432995e9494e4a474e38cddd085f21754416474d3f4
NODE_LABELS=arm built-in
LOGNAME=jenkins
HUDSON_HOME=/var/lib/jenkins
NODE_NAME=built-in
BUILD_NUMBER=3
JOB_DISPLAY_URL=http://jenkins.xadocker.cn:8080/job/pipeline%E7%A0%94%E7%A9%B6/job/project-env/display/redirect
HUDSON_COOKIE=c1b6e15b-4e84-4c4b-a664-80610c31e2dd
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
从输出中可以看到提供jenkins提供的变量有很多,而在流水线中又是如何调用的?其实也很简单
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo 'Hello World'
sh "env"
}
}
stage('World') {
steps {
sh "echo ${BUILD_NUMBER}"
sh "echo ${env.BUILD_NUMBER}"
}
}
}
}
如何声明一个变量呢?在jenkins中可以用environment来声明全局或局部,当environment块作为pipeline的全局声明时,则所有stage都可以用该块内的变量
pipeline {
agent any
environment {
name = 'xadocker'
}
stages {
stage('Hello') {
steps {
echo name
}
}
stage('World') {
steps {
echo name
}
}
}
}
若是environment块在stage中声明,那该块内的变量只生效于该stage范围内
pipeline {
agent any
environment {
name = 'xadocker'
}
stages {
stage('Hello') {
steps {
echo name
}
}
stage('World') {
environment {
name = 'xadxad'
}
steps {
echo name
}
}
stage('Hi') {
steps {
echo name
}
}
}
}
当然还存在其他方式定义变量,那就是使用def去定义,注意查看输出
def name = "abcdef"
pipeline {
agent any
environment {
name = 'xadocker'
}
stages {
stage('Hello') {
steps {
echo name
echo env.name
}
}
stage('World') {
environment {
name = 'xadxad'
}
steps {
echo name
echo env.name
}
}
stage('Hi') {
steps {
echo name
echo env.name
}
}
}
}
输出
Started by user cicd-admin
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/pipeline研究/project-env
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] echo
abcdef
[Pipeline] echo
xadocker
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (World)
[Pipeline] withEnv
[Pipeline] {
[Pipeline] echo
abcdef
[Pipeline] echo
xadxad
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Hi)
[Pipeline] echo
abcdef
[Pipeline] echo
xadocker
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
从上面输出可以看到变量的调用方式不同,在特定场景下输出可能相同,但也有可能不同。博主推荐使用
env.变量名
去调用environment块的变量。
正文完