歡迎關注我公眾號呀~「測試游記」「zx94_11」
環境變量
環境變量可以分為:
- Jenkins內置變量
- 自定義變量
Jenkins內置變量
Jenkins通過一個名為
env
的全局變量,將Jenkins內置環境變量暴露出來
${env.BUILD_NUMBER}$
$env.BUILD_NUMBER$
-
$BUILD_NUMBER$
,不推薦使用
查看全局變量:http://「Jenkins服務器地址」:「端口」/pipeline-syntax/globals
例如:http://127.0.0.1:8080/pipeline-syntax/globals
全局變量
幾個常用的變量
- BUILD_NUMBER:構建號
- BUILD_URL:構建頁面URL
使用sh ‘printenv’可以打印env變量的屬性值
pipeline{
agent any
stages{
stage('Example'){
steps{
script{
sh 'printenv'
}
}
}
}
}
printenv
自定義環境變量
定義兩個環境變量CC = 'clang'
,DEBUG_FLAGS = '-g'
pipeline{
agent any
environment {
CC = 'clang'
}
stages{
stage('Example'){
environment {
DEBUG_FLAGS = '-g'
}
steps{
script{
sh "${CC} ${DEBUG_FLAGS}"
sh "printenv"
}
}
}
}
}
自定義環境變量
??environment
在pipeline中定義,作用域為整個pipeline,在stage中定義,作用域只在該階段
為了避免命名沖突,建議在自定義變量前增加特別的前綴,例如「公司名」_BUILD_NUMBER
自定義全局變量
進入Mange Jenkins -> Configute System(系統設置)-> Global properties(全局屬性)
??勾選Enbironment variables(環境變量)復選框
系統設置
設置自定義全局變量
測試
通過一個簡單的echo來查看自定義環境變量是否生效
pipeline{
agent any
stages{
stage('Example'){
steps{
script{
echo "${env.g_name}"
}
}
}
}
}
從圖中可以看到,輸出的內容為剛定義的內容
測試結果