Jenkins 流水线语法 05 构建时变量 environment
env 构建时变量
之前讲的是全局变量,现在是构建时候自己定义所运行的一些变量
- 定义: 通过键值对(k-v)格式定义流水线在运行时的环境变量, 分为流水线级别和阶段级别。(可以使用enviorment语句块里面去写我们的变量,不需要加逗号分割,每行就是一个变量)
流水线级别环境变量参考:(全局变量,在pipeline下面定义的语句块,在这里面的变量在下面所有的阶段都可以去使用)
pipeline {
environment {
NAME = "zeyang"
VERSION = "1.1.10"
ENVTYPE = "DEV"
}
}
阶段级别环境变量参考:(局部变量,在某个阶段里面定义的变量,那么只能在某个阶段去使用)
pipeline {
...
...
stages {
stage("build"){
environment {
VERSION = "1.1.20"
}
steps {
script {
echo "${VERSION}"
}
}
}
}
}
全局变量
pipeline {
agent any
environment{
VERSION = "1.1.1"
ENV_TYPE = "DEV"
}
stages {
stage('Hello') {
steps {
script{
echo "The variable version is ${VERSION}"
echo "The variable env_type is ${ENV_TYPE}"
}
}
}
}
}
Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/pipeline-test
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
The variable version is 1.1.1
[Pipeline] echo
The variable env_type is DEV
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
这里是在stage里面定义了变量,也就是局部变量
pipeline {
agent any
environment{
VERSION = "1.1.1"
ENV_TYPE = "DEV"
}
stages {
stage('Hello') {
environment{
VERSION="1.0.0"
}
steps {
script{
echo "The variable version is ${VERSION}"
echo "The variable env_type is ${ENV_TYPE}"
}
}
}
}
}
Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/pipeline-test
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] withEnv
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] echo
The variable version is 1.0.0
[Pipeline] echo
The variable env_type is DEV
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
全局变量(env.JOB_NAME 这个是jenkins内置的全局变量,env.是自定义的全局环境变量,注意不要覆盖了Jenkins内置的全局变量)
pipeline {
agent any
environment{
VERSION = "1.1.1"
ENV_TYPE = "DEV"
}
stages {
stage('Hello') {
environment{
VERSION="1.0.0"
}
steps {
script{
echo "The variable version is ${VERSION}"
echo "The variable env_type is ${ENV_TYPE}"
echo "The job name is ${env.JOB_NAME}" //内置的环境变量
env.JOB_NAMEA="mytest-pipeline" //自定义的全局变量,也就是整个流水线可以去使用
echo "the variable env.JOB_NAMEA is ${env.JOB_NAMEA}"
}
}
}
}
}
Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/pipeline-test
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage (hide)
[Pipeline] { (Hello)
[Pipeline] withEnv
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] echo
The variable version is 1.0.0
[Pipeline] echo
The variable env_type is DEV
[Pipeline] echo
The job name is pipeline-test
[Pipeline] echo
the variable env.JOB_NAMEA is mytest-pipeline
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
目录 返回
首页