[英]How to add environment variables from the Code in Jenkins?
所以基本上程序需要一個以這種方式讀取的環境變量:
var a = java.lang.System.getenv('envVariable');
現在,當我使用 Jenkins 管道時,我正在尋找一種在管道中提供此環境變量的方法,但我沒有找到方法。 有沒有辦法通過 Jenkins 實現這一目標?
您應該使用environment
部分來定義環境變量。 scope 取決於environment
等級。 更多詳情請查看官方文檔: https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#using-environment-variables
例子:
pipeline {
agent {
label 'my_label'
}
// These are visible in all stages
environment {
MY_ENV_VAR = 'value_of_var'
DEBUG = 'true'
}
stages {
stage('Build') {
// These are visible only in the current stage (Build)
environment {
STAGE_ENV = 'value_stage_env'
}
steps {
echo "MY_ENV_VAR = ${MY_ENV_VAR}"
echo "DEBUG = ${DEBUG}"
echo "STAGE_ENV = ${STAGE_ENV}"
sh 'printenv'
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.