Active Choice Parameter 在 Jenkinsfile 中的应用

Active Choice Parameter 在 Jenkinsfile 中的应用_第1张图片

插件地址:

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

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


目录

    • 场景
    • 一、插件安装
      • 1.1 安装
      • 1.2 配置
    • 二、应用案例
      • 2.1 直接配置
      • 2.2 Jenkinsfile 配置


场景

也许你可能有这样一个需求,在 Jenkins 构建时需要使用有单选、复选、及多值输入的场景,那你就可以使用 Active Choice 插件来实现。

一、插件安装

1.1 安装

image-20221022112938268

Active Choice Parameter 在 Jenkinsfile 中的应用_第2张图片

1.2 配置

1、添加选项参数

选择下图红框

Active Choice Parameter 在 Jenkinsfile 中的应用_第3张图片

2、编写 Groovy Script

Active Choice Parameter 在 Jenkinsfile 中的应用_第4张图片

return [
    'Sao Paulo',
    'Rio de Janeiro',
    'Parana',
    'Acre'
]

2、选择选择类型

Active Choice Parameter 在 Jenkinsfile 中的应用_第5张图片

3、效果展示

当你选择多选时,可通过下图实现多选

Active Choice Parameter 在 Jenkinsfile 中的应用_第6张图片

4、当选择不同的 user 时可进行动态更新

再次新增一个 Active Choice Reactive 控件,上面的 UI 控件会触发下面的选项参数并动态更新

此时选项参数选择如下图红框

Active Choice Parameter 在 Jenkinsfile 中的应用_第7张图片

配置:引用参数,实现自动更新。

Active Choice Parameter 在 Jenkinsfile 中的应用_第8张图片

if (User == "Sao Paulo") {
  return ["Barretos", "Sao Paulo", "Itu"]
} else if (User == "Rio de Janeiro") {
  return ["Rio de Janeiro", "Mangaratiba"]
} else if (User == "Parana") {
  return ["Curitiba", "Ponta Grossa"]
} else if (User == "Acre") {
  return ["Rio Branco", "Acrelandia"]
} else {
  return ["Unknown state"]
}

5、效果

选择 User 时,会根据配置规则刷新 PerformShell。

Active Choice Parameter 在 Jenkinsfile 中的应用_第9张图片

二、应用案例

实现功能:通过选择不同的构建工具来构建不同的项目。

2.1 直接配置

该方式不通过 Pipeline 脚本配置,直接进入你 Jenkins 的 Project 进行配置。

1、添加选项参数

选择下图红框

Active Choice Parameter 在 Jenkinsfile 中的应用_第10张图片

2、编写 Groovy Script

Active Choice Parameter 在 Jenkinsfile 中的应用_第11张图片

return[
  "maven",
  "ant",
  "gradle"
]

3、效果

Active Choice Parameter 在 Jenkinsfile 中的应用_第12张图片

4、当选择不同的 BuildType 时可进行动态更新

再次新增一个 Active Choice Reactive 控件,上面的 UI 控件会触发下面的选项参数并动态更新

此时选项参数选择如下图红框

Active Choice Parameter 在 Jenkinsfile 中的应用_第13张图片

5、配置

Active Choice Parameter 在 Jenkinsfile 中的应用_第14张图片

Groovy 脚本

if (BuildType == "maven") {
  return ["clean package"]
} else if (BuildType == "ant") {
  return ["ant -buildfile build.xml"]
} else if (BuildType == "gradle") {
  return ["gradle clean build"]
} else {
  return ["Unknown state"]
}

# 或下面的都一样的(任选其一即可)

if (BuildType.equals ("maven")) {
  return ["clean package"]
} else if (BuildType.equals ("ant")) {
  return ["ant -buildfile build.xml"]
} else if (BuildType.equals ("gradle")) {
  return ["gradle clean build"]
} else {
  return ["Unknown state"]
}

6、效果

选择 maven 类型时自动选择构建命令,而不需要手动点击 PerformShell 选项

Active Choice Parameter 在 Jenkinsfile 中的应用_第15张图片

2.2 Jenkinsfile 配置

2.1 部分是直接通过 web 界面设置,接下来我们可以通过 Pipeline 来自动化实现。附上一个参考模板样例:根据自身需求修改即可

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"
        } }
      }
  }
}

构建结果:

Active Choice Parameter 在 Jenkinsfile 中的应用_第16张图片

<点击跳转至开头>

你可能感兴趣的:(CICD,jenkins,运维)