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