AdSense

網頁

2024/4/8

Jenkins Pipeline 設定環境變數

Jenkins Pipeline設定環境變數的方式如下。


範例環境:

  • Jenkins 2.414.3


設定環境變數

例如在Pipeline的Pipeline script設定如下,使用environment指令(directive)設定環境變數APP_NAMEENV

pipeline {
    agent any
    
    environment {
        APP_NAME = 'go-demo'
        ENV = 'test'
    }
    
    stages {
        stage('Env info') {
            steps {
                echo "APP_NAME is '${APP_NAME}'"
                echo "ENV is '${ENV}'"   
            }
        }
    }
}


測試

點選Pipeline項目的[Build Now]開始建置,然後檢視[Console Output]如下:

Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/pipeline-1
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Env info)
[Pipeline] echo
APP_NAME is 'go-demo'
[Pipeline] echo
ENV is 'test'
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

沒有留言:

AdSense