Active Choices Parameter
时间: 2023-11-12 07:30:40 浏览: 47
Active Choices Parameter is a Jenkins plugin that allows a parameter in a Jenkins job to have dynamically generated choices at runtime. This means that the choices available to the user selecting the parameter are not static but can change based on some external factors.
For example, you could use Active Choices Parameter to generate a list of available environments or branches in a Git repository. The choices would be updated automatically whenever a new environment or branch is created or deleted.
This plugin provides great flexibility in defining job parameters and can greatly simplify job configuration and management.
相关问题
jenkins pipeline active choices git分支
可以使用 Jenkins Pipeline 的 Active Choices 插件来动态获取 Git 仓库中的分支列表,并在 Jenkins 构建中提供选择。下面是一个使用 Active Choices 插件和 Git 插件的示例代码:
```groovy
pipeline {
agent any
parameters {
// 定义参数,使用 Active Choices 插件提供选择
activeChoiceParam('BRANCH', 'git branch') {
// 使用 Groovy 脚本动态获取 Git 仓库中的分支列表
script {
def branches = []
sh 'git fetch --all'
sh 'git branch -r --no-abbrev | grep -v HEAD | while read branch; do echo ${branch#origin/}; done'.eachLine {
branches.add(it.trim())
}
return branches
}
}
}
stages {
stage('Build') {
steps {
// 使用选定的分支进行 Git 操作
git branch: params.BRANCH, url: 'https://github.com/username/repo.git'
sh 'maven clean install'
}
}
}
}
```
在该示例中,使用 Active Choices 插件提供了一个名为 `BRANCH` 的参数,脚本动态获取了 Git 仓库中的分支列表,并使用选定的分支进行了 Git 操作和 Maven 构建。
Jenkins中使用Active Choices Reactive Paramete参数化构建时如何点击某个选项实现post请求,但不刷新当前页面,有没有示例脚本
在 Jenkins 中使用 Active Choices Reactive Parameter 参数化构建时,也可以实现点击某个选项实现 post 请求,不刷新当前页面。具体实现步骤如下:
1. 安装 Active Choices 插件。
2. 在 Jenkins 项目中,添加一个 Active Choices 插件类型的参数。
3. 在参数配置中,选择类型为“单选框”。
4. 在“Choice Type”中选择“Active Choices Reactive Reference”。
5. 在“Referenced Parameters”中填写需要依赖的参数名称。
6. 在“Script”中编写 Groovy 脚本,实现点击选项时发送 post 请求的功能。具体实现方法可以参考 Active Choices 插件官方文档。
7. 在 Jenkins 项目构建时,选择相应的选项,即可触发 post 请求,实现对应的操作。
需要注意的是,为了不刷新当前页面,可以使用 JavaScript 发送 post 请求,并使用 AJAX 技术实现异步刷新。具体实现方法可以参考前面提到的 AJAX 技术。
以下是一个示例脚本,实现在选择某个选项时触发 post 请求,并将请求结果显示在 Jenkins 构建日志中。
```groovy
def url = 'http://example.com/api/endpoint'
def params = [:]
def my_choice = binding.variables.get('MY_CHOICE') // MY_CHOICE 是 Active Choices 参数中选择的选项
if (my_choice == 'option1') {
params['key'] = 'value1'
} else if (my_choice == 'option2') {
params['key'] = 'value2'
} else {
println('Invalid choice')
return
}
def response = post(url: url, requestBody: params, contentType: 'APPLICATION_JSON').getContent()
println(response)
```
在上面的示例脚本中,首先获取 MY_CHOICE 变量,然后根据选项的值发送对应的 post 请求,并将请求结果存储在 response 变量中。最后使用 println 命令将结果显示在 Jenkins 构建日志中。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)