• 使用环境变量
    • 环境变量中的凭证信息

    使用环境变量

    环境变量可以像下面的示例设置为全局的,也可以是阶段(stage)级别的。如你所想,阶段(stage)级别的环境变量只能在定义变量的阶段(stage)使用。

    Jenkinsfile (Declarative Pipeline)

    1. pipeline {
    2. agent any
    3. environment {
    4. DISABLE_AUTH = 'true'
    5. DB_ENGINE = 'sqlite'
    6. }
    7. stages {
    8. stage('Build') {
    9. steps {
    10. sh 'printenv'
    11. }
    12. }
    13. }
    14. }

    Toggle Scripted Pipeline(Advanced)

    Jenkinsfile (Scripted Pipeline)

    1. node {
    2. withEnv(['DISABLE_AUTH=true',
    3. 'DB_ENGINE=sqlite']) {
    4. stage('Build') {
    5. sh 'printenv'
    6. }
    7. }
    8. }

    这种在 Jenkinsfile 中定义环境变量的方法对于指令性的脚本定义非常有用方便,比如 Makefile 文件,可以在 Pipeline 中配置构建或者测试的环境,然后在 Jenkins 中运行。

    环境变量的另一个常见用途是设置或者覆盖构建或测试脚本中的凭证。因为把凭证信息直接写入 Jenkinsfile 很显然是一个坏主意,Jenkins Pipeline 允许用户快速安全地访问在 Jenkinsfile 中预定义的凭证信息,并且无需知道它们的值。

    环境变量中的凭证信息

    更多信息参考用户手册中的凭证信息处理。

    继续学习“记录测试和构建结果”