Jenkins 用戶文檔(使用環境變量)

使用環境變量

環境變量能夠全局設置,以下面的示例或每一個階段,正如你所料,每一個階段設置環境變量意味着它們僅適用於定義它們的階段。node

Jenkinsfile (Declarative Pipeline)
pipeline {
    agent any

    environment {
        DISABLE_AUTH = 'true'
        DB_ENGINE    = 'sqlite'
    }

    stages {
        stage('Build') {
            steps {
                sh 'printenv'
            }
        }
    }
}

腳本管道(高級):sql

Jenkinsfile (Scripted Pipeline)
node {
    withEnv(['DISABLE_AUTH=true',
             'DB_ENGINE=sqlite']) {
        stage('Build') {
            sh 'printenv'
        }
    }
}

這種從Jenkinsfile中定義環境變量的方法對於指示腳本(如Makefile)以不一樣的方式配置構建或測試以在Jenkins中運行它們很是有用。segmentfault

環境變量的另外一個常見用途是在構建或測試腳本中設置或覆蓋「虛擬」憑據,由於將憑據直接放入Jenkinsfile(顯然)是一個壞主意,Jenkins管道容許用戶快速安全地訪問Jenkinsfile中的預約義憑據,而無需知道其值。安全


上一篇:定義執行環境

下一篇:記錄測試結果和工件

相關文章
相關標籤/搜索