Jenkins動態參數

parameters

能不能在構建時候傳入一些參數進去呢?

pipeline {
   agent any
   // 動態參數,看這裏!!!!
   parameters {
       // Boolean值
        booleanParam(defaultValue: false,description: 'test run?', name: 'testRun')
        // 下拉列表,默認值是第一個
        choice(choices: 'Windows-1\nLinux-2', description:'Which platform?', name: 'platform')
        //file(description: 'Select the file to upload', name: 'sample')
        // 多行文本
        text(defaultValue: 'No message', description:'Enter your message', name: 'userMsg')
        // 密碼
        password(defaultValue: "userpass1", description:'User password?', name: 'pass')
        // 字符串
        string(defaultValue: "Linux",description: 'What platform?', name: 'platform-noduplicate')
    }
   stages {
      stage('Build') {         
         steps {
            // Get some code from a GitHub repository
            git 'https://github.com/oneslideicywater/common.git'
            // 查看設置的參數
            echo "$params.testRun"
            echo "$params.platform"
            echo "$params.userMsg"
            echo "$params"
         }
      }
   }
}

你設置了上面的pipeline.parameters中設置構建參數之後,每次構建可以在裏面輸入參數覆蓋掉默認值。
在這裏插入圖片描述

小bug

你添加新的構建參數之後,運行一次之後,需要再重試構建一次纔可以看到上面的那些參數。

environment

另外,environment變量也可以定義運行時參數,不過這個一般保留的都是靜態配置。通過env.xxx來訪問。

pipeline {
   agent any
   tools {
      maven 'maven'
   }
    parameters {
        booleanParam(defaultValue: false,description: 'test run?', name: 'testRun')
        choice(choices: 'Windows-1\nLinux-2', description:'Which platform?', name: 'platform')
        //file(description: 'Select the file to upload', name: 'sample')
        text(defaultValue: 'No message', description:'Enter your message', name: 'userMsg')
        password(defaultValue: "userpass1", description:'User password?', name: 'pass')
        string(defaultValue: "Linux",description: 'What platform?', name: 'platform-noduplicate')
    }
    environment{
     // 靜態參數,看這裏!!!!
      SAMPLE_SERVER="http://10.0.0.1:8080"
      SAMPLE_API="$SAMPLE_SERVER/api"
    }
   stages {
      stage('Build') {              
         steps {
            // Get some code from a GitHub repository
            git 'https://github.com/oneslideicywater/common.git'
            echo "$params.testRun"
            echo "$params.platform"
            echo "$params.userMsg"
            echo "$params"
             // 打印靜態參數
            echo "------------------------below are env---------------------"
            echo "$env.SAMPLE_API"
         }
      }
   }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章