Prerequisite
必须先在 Jenkins 上安装:
- Git Plugin
- Git Parameter Plugin
配置
如果没使用 Jenkins Pipeline
可以直接看 插件文档 指引,为了方便方便阅读,搬运至此
效果如下:
如果使用 Jenkins Pipeline
这里,插件文档就不适用了,因为,一般来说我们会直接在 Jenkins 上直接配置 SCM,如仓库地址、用户凭据
所以我们的脚本应该直接复用 SCM 的配置,正确脚本应该是:
properties([
parameters([
// 添加参数设置
gitParameter(branch: '',
branchFilter: 'origin/(.*)',
defaultValue: 'master', // default value 必填
description: '',
name: 'REVISION', // 变量名,因为我们可能支持不同类型,不仅仅是 branch,revision 名字更适合
quickFilterEnabled: false,
selectedValue: 'NONE',
sortMode: 'NONE',
tagFilter: '*',
type: 'PT_BRANCH_TAG'), // 其他类型 PT_TAG 列出 tag,PT_BRANCH 列出分支,PT_REVISION 列出所有 commit,PT_PULL_REQUEST 列出 PR
...
...
])
])
def myRepo = checkout([$class : 'GitSCM',
branches : [[name: "${params.REVISION}"]], // 传入分支
doGenerateSubmoduleConfigurations: false,
extensions : [],
gitTool : 'Default',
submoduleCfg : [],
userRemoteConfigs : scm.userRemoteConfigs // !重要,复用我们在 Jenkins 上 SCM 配置,如凭据、仓库地址
])
关键在于,把 scm.userRemoteConfigs
传给 checkout
步骤
更多高级配置详见插件文档