- 使用环境变量
- 环境变量中的凭证信息
- 环境变量中的凭证信息
使用环境变量
环境变量可以像下面的示例设置为全局的,也可以是阶段(stage)级别的。如你所想,阶段(stage)级别的环境变量只能在定义变量的阶段(stage)使用。
Jenkinsfile (Declarative Pipeline)
pipeline {agent anyenvironment {DISABLE_AUTH = 'true'DB_ENGINE = 'sqlite'}stages {stage('Build') {steps {sh 'printenv'}}}}
Toggle Scripted Pipeline(Advanced)
Jenkinsfile (Scripted Pipeline)
node {withEnv(['DISABLE_AUTH=true','DB_ENGINE=sqlite']) {stage('Build') {sh 'printenv'}}}
这种在 Jenkinsfile 中定义环境变量的方法对于指令性的脚本定义非常有用方便,比如 Makefile 文件,可以在 Pipeline 中配置构建或者测试的环境,然后在 Jenkins 中运行。
环境变量的另一个常见用途是设置或者覆盖构建或测试脚本中的凭证。因为把凭证信息直接写入 Jenkinsfile 很显然是一个坏主意,Jenkins Pipeline 允许用户快速安全地访问在 Jenkinsfile 中预定义的凭证信息,并且无需知道它们的值。
环境变量中的凭证信息
更多信息参考用户手册中的凭证信息处理。
继续学习“记录测试和构建结果”
