jenkins pipeline active choices git分支
时间: 2023-08-19 21:05:33 浏览: 83
可以使用 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 构建。