jenkins中shell脚本中使用构建参数化Groovy变量的四种方式:
以字符变量为例:
流水线代码:
pipeline {
agent {
//label "${server}"
label "${28}"
}
stages {
stage('Hello') {
steps {
echo 'Hello World'
echo "${28}"
echo "${character_argument}"
echo "--------------------------"
// 这个报错
//echo ${character_argument}
// 这个可以输出变量
echo "${character_argument}"
//# 下面这三个不可以,输出的是${character_argument}本身
echo "\${character_argument}"
echo '${character_argument}'
echo '\${character_argument}'
echo "--------------------------"
}
}
stage('shell') {
steps{
script {
sh"""
pwd
# shell 脚本中引用 Groovy变量的四种方式
echo ${character_argument}
echo "${character_argument}"
echo "\${character_argument}"
echo '${character_argument}'
# 下面这个不可以,输出的是${character_argument}本身
echo '\${character_argument}'
"""
}
}
}
}
}
直接运行:
Console Output:
Started by user 昌显瑞
[Pipeline] Start of Pipeline
[Pipeline] node
Running on xxx.xxx.xx.xx in /home/jenkins/program/jenkins/workspace/xxxxxx_test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] echo
Hello World
[Pipeline] echo
28
[Pipeline] echo
default_argument
[Pipeline] echo
--------------------------
[Pipeline] echo
default_argument
[Pipeline] echo
${character_argument}
[Pipeline] echo
${character_argument}
[Pipeline] echo
${character_argument}
[Pipeline] echo
--------------------------
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (shell)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ pwd
/home/jenkins/program/jenkins/workspace/middle_nMedia_test
+ echo default_argument
default_argument
+ echo default_argument
default_argument
+ echo default_argument
default_argument
+ echo default_argument
default_argument
+ echo ${character_argument}
${character_argument}
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS