03.04 Jenkins使用Active Choice Parameter參數化流水線


Jenkins使用Active Choice Parameter參數化流水線


Jenkins使用Active Choice Parameter參數化流水線

在使用Pipeline項目時 一般都是參數化構建工作,在Jenkins的構建需要使用參數類型有複選框,單選按鈕,多選值等輸入的情景。

項目地址:https://github.com/jenkinsci/active-choices-plugin

插件地址:https://plugins.jenkins.io/uno-choice/

轉到→管理Jenkins→選擇管理插件→選擇可用選項卡,然後搜索主動選擇插件。安裝並重新啟動Jenkins,以正確安裝插件。我的已經安裝好,因此在“已安裝”標籤中列出。


Jenkins使用Active Choice Parameter參數化流水線

主動選擇參數

使用Groovy腳本或Scriptler目錄中的腳本為生成參數動態生成值選項列表。參數可以動態更新,呈現為組合框,複選框,單選按鈕或豐富的HTML UI窗口小部件。


Jenkins使用Active Choice Parameter參數化流水線

按住Ctrl 就可以多選了。


Jenkins使用Active Choice Parameter參數化流水線


主動選擇反應參數

當作業中UI控件的值發生更改時,可以動態更新(主動選擇和響應參考參數) 這裡可以使用IF進行條件判斷,輸出相關的值。


Jenkins使用Active Choice Parameter參數化流水線


在JenkinsFile中定義

<code>

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',
/> $class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: false,
/> 'return[\\'Could not get Env\\']'
],
/> classpath: [],
sandbox: false,
/> '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',
/> $class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: false,
/> 'return[\\'Could not get Environment from Env Param\\']'
],
/> classpath: [],
sandbox: false,
/> ''' 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 {
/> 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"
} }
}
}
}/<code>


Jenkins使用Active Choice Parameter參數化流水線




Jenkins使用Active Choice Parameter參數化流水線


Jenkins使用Active Choice Parameter參數化流水線


分享到:


相關文章: