Jenkins 流水线语法 05 构建时变量 environment
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 adminRunning in Durability level: MAX_SURVIVABILITY[Pipeline] Start of Pipeline[Pipeline] nodeRunning on Jenkins in /var/jenkins_home/workspace/pipeline-test[Pipeline] {[Pipeline] withEnv[Pipeline] {[Pipeline] stage[Pipeline] { (Hello)[Pipeline] script[Pipeline] {[Pipeline] echoThe variable version is 1.1.1[Pipeline] echoThe variable env_type is DEV[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] }[Pipeline] // withEnv[Pipeline] }[Pipeline] // node[Pipeline] End of PipelineFinished: 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 adminRunning in Durability level: MAX_SURVIVABILITY[Pipeline] Start of Pipeline[Pipeline] nodeRunning on Jenkins in /var/jenkins_home/workspace/pipeline-test[Pipeline] {[Pipeline] withEnv[Pipeline] {[Pipeline] stage[Pipeline] { (Hello)[Pipeline] withEnv[Pipeline] {[Pipeline] script[Pipeline] {[Pipeline] echoThe variable version is 1.0.0[Pipeline] echoThe variable env_type is DEV[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // withEnv[Pipeline] }[Pipeline] // stage[Pipeline] }[Pipeline] // withEnv[Pipeline] }[Pipeline] // node[Pipeline] End of PipelineFinished: 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 adminRunning in Durability level: MAX_SURVIVABILITY[Pipeline] Start of Pipeline[Pipeline] nodeRunning 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] echoThe variable version is 1.0.0[Pipeline] echoThe variable env_type is DEV[Pipeline] echoThe job name is pipeline-test[Pipeline] echothe 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 PipelineFinished: SUCCESS
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~