效果,,点击build with Parameters 就会出现右边的当前仓库的所有的分支,默认最多显示5个,可以修改配置,修改显示的最大分支数量。如果分支太多,可以通过右边的过滤框输入过滤。
- 安装git params插件
- 搜索git parameter 插件 ,我这里是已经安装成功的
- 使用pipeline构建时动态获取分支名
在pipeline script里面添加获取分支的脚本:
pipeline {agent any// 参数配置,只有在里配置了,git parameter插件才会生效parameters {// 注意,这里是一行gitParameter branch: '',branchFilter: '.*', defaultValue: 'main',description: '发布选项: 请选择GIT分支', name: 'git_branch', quickFilterEnabled: true, selectedValue: 'TOP',sortMode: 'DESCENDING_SMART',tagFilter: '*', type: 'PT_BRANCH', listSize:'20', requiredParameter:true}stages {stage('Build') {steps {checkout scmGit(branches: [[name: '${BRANCH}']], extensions: [], userRemoteConfigs: [[credentialsId: '8be22b7b-709e-4f7e-93c4-c996e6fe250d', url: 'http://124.71.140.30:7070/xinyue/student-video-course.git']]) }}}
}
上面脚本中,credentialsId是访问git仓库的凭证id,可以在jenkins的凭证管理中添加凭证,会生成一个唯一的id:
点击全局,进入到凭证列表,右上角可以添加新的凭证
另外需要注意的是,保存好配置之后并不会立刻显示出来,需要先手动build一次,不管是成功还是失败,只要git parameters 参数那配置有没有问题,就会出现分支列表了。