使用Active Choice Parameter參數(shù)化構(gòu)建
在使用Pipeline項(xiàng)目時(shí) 一般都是參數(shù)化構(gòu)建工作,在Jenkins的構(gòu)建需要使用參數(shù)類型有復(fù)選框,單選按鈕,多選值等輸入的情景。

轉(zhuǎn)到→管理Jenkins→選擇管理插件→選擇可用選項(xiàng)卡,然后搜索主動(dòng)選擇插件。安裝并重新啟動(dòng)Jenkins,以正確安裝插件。我的已經(jīng)安裝好,因此在“已安裝”標(biāo)簽中列出。

主動(dòng)選擇參數(shù)
使用Groovy腳本或Scriptler目錄中的腳本為生成參數(shù)動(dòng)態(tài)生成值選項(xiàng)列表。參數(shù)可以動(dòng)態(tài)更新,呈現(xiàn)為組合框,復(fù)選框,單選按鈕或豐富的HTML UI窗口小部件。

按住Ctrl 就可以多選了。

主動(dòng)選擇反應(yīng)參數(shù)
當(dāng)作業(yè)中UI控件的值發(fā)生更改時(shí),可以動(dòng)態(tài)更新(主動(dòng)選擇和響應(yīng)參考參數(shù)) 這里可以使用IF進(jìn)行條件判斷,輸出相關(guān)的值。

Jenkinsfile
- properties([
- parameters([
- [$class: 'ChoiceParameter',
- choiceType: 'PT_SINGLE_SELECT',
- description: 'Select the Env Name from the Dropdown List',
- filterLength: 1,
- filterable: true,
- name: 'Env',
- randomName: 'choice-parameter-5631314439613978',
- script: [
- $class: 'GroovyScript',
- fallbackScript: [
- classpath: [],
- sandbox: false,
- script:
- 'return[\'Could not get Env\']'
- ],
- script: [
- classpath: [],
- sandbox: false,
- script:
- 'return["Dev","QA","Stage","Prod"]'
- ]
- ]
- ],
- [$class: 'CascadeChoiceParameter',
- choiceType: 'PT_SINGLE_SELECT',
- description: 'Select the Server from the Dropdown List',
- filterLength: 1,
- filterable: true,
- name: 'Server',
- randomName: 'choice-parameter-5631314456178619',
- referencedParameters: 'Env',
- script: [
- $class: 'GroovyScript',
- fallbackScript: [
- classpath: [],
- sandbox: false,
- script:
- 'return[\'Could not get Environment from Env Param\']'
- ],
- script: [
- classpath: [],
- sandbox: false,
- script:
- ''' if (Env.equals("Dev")){
- return["devaaa001","devaaa002","devbbb001","devbbb002","devccc001","devccc002"]
- }
- else if(Env.equals("QA")){
- return["qaaaa001","qabbb002","qaccc003"]
- }
- else if(Env.equals("Stage")){
- return["staaa001","stbbb002","stccc003"]
- }
- else if(Env.equals("Prod")){
- return["praaa001","prbbb002","prccc003"]
- }
- '''
- ]
- ]
- ]
- ])
- ])
- pipeline {
- environment {
- vari = ""
- }
- agent any
- stages {
- stage ("Example") {
- steps {
- script{
- echo 'Hello'
- echo "${params.Env}"
- echo "${params.Server}"
- if (params.Server.equals("Could not get Environment from Env Param")) {
- echo "Must be the first build after Pipeline deployment. Aborting the build"
- currentBuild.result = 'ABORTED'
- return
- }
- echo "Crossed param validation"
- } }
- }
- }
- }
【編輯推薦】