在使用Pipeline项目时 一般都是参数化构建工作,在Jenkins的构建需要使用参数类型有复选框,单选按钮,多选值等输入的情景。
转到→管理Jenkins→选择管理插件→选择可用选项卡,然后搜索主动选择插件。安装并重新启动Jenkins,以正确安装插件。我的已经安装好,因此在“已安装”标签中列出。
主动选择参数
使用Groovy脚本或Scriptler目录中的脚本为生成参数动态生成值选项列表。参数可以动态更新,呈现为组合框,复选框,单选按钮或丰富的HTML UI窗口小部件。
按住Ctrl 就可以多选了。
主动选择反应参数
当作业中UI控件的值发生更改时,可以动态更新(主动选择和响应参考参数) 这里可以使用IF进行条件判断,输出相关的值。
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"
- } }
- }
- }
- }
【编辑推荐】