Jenkins动态选择分支/tag
一.简介
一般选择分支构建,Git Parameter插件即可。这里是应用pipline的同时,可以选择分支进行构建。
Dynamic Parameter 可以动态的生成我们想要的一些参数,使用的语言是groovy。获取参数后,将变量传递到pipline中,用于脚本使用。
二.配置
1.在插件中找到 Dynamic Parameter Plug-in 进行安装
2.创建一个流水线项目
3.在参数化构建过程选择 Dynamic choice Parameter
git后面那段地址,就是项目的地址,不过只能获取一个项目的
def gettags = ("git ls-remote -h git@1.1.1.1:dd/xx.git").execute()
gettags.text.readLines().collect { it.split()[1].replaceAll('refs/heads/', '') }.unique()
4.编写pipline脚本,这里只写变化的一段
stage 'checkout'
dir('business_pay'){
git branch: release_branch, credentialsId: 'jenkins', url: 'http://1.1.1.1/dd/xx.git'
}
5.构建的时候,就会有选择提示了
三.配置tag
1.tag和上述配置一样,不过在Dynamic choice Parameter那里修改一下
def gettags = ("git ls-remote -t --refs git@1.1.1.1:dd/xx.git").execute()
gettags.text.readLines().collect { it.split()[1].replaceAll('refs/tags/', '') }.unique()
2.然后pipeline里进行下修改,用git命令切换分支
stage 'checkout'
dir('business_pay'){
git branch: 'master', credentialsId: 'jenkins', url: 'http://1.1.1.1/dd/xx.git'
sh "git checkout $release_tag"
}
四.其它方法
def branchType = BRANCH_NAME.tokenize('/').last().trim()
isReleaseBranch = branchType == 'tmo_ps_gp15' || branchType == 'master'
branch = isReleaseBranch ? branchType : BRANCH_NAME.trim()
originalBuildNumber = BUILD_NUMBER
revision = "1.0.${BUILD_NUMBER}"
def setDisplayName(){
if (BUILD_NUMBER != originalBuildNumber) {
currentBuild.displayName = "${revision}-${branch} (resume ${BUILD_NUMBER})"
} else if (isReleaseBranch){
currentBuild.displayName = "${revision}-${branch}"
}
}
BRANCH_NAME.tokenize('/').last().trim()
可以拿到你当前checkout下来是哪个branch, tokenize('/').last().trim() 意思就是以 / 切割 然后取最后一个元素并且去除该元素左右两边得空格,这样就可以拿到你checkout得branch
isReleaseBranch = branchType == 'tmo_ps_gp15' || branchType == 'master'
意思就是如果 你上面定义得branchType 等于tmo_ps_gp15 或者是master 分支得时候, isReleaseBranch 就等于呢个分支名且为true
branch = isReleaseBranch ? branchType : BRANCH_NAME.trim()
意思就是当isReleaseBranch为true得情况下,branch等于branchType ,如果为false就重新抓取你checkout得branch name
用于下面pipeline得判断而已
你可以根据你构建/部署情况而定
一个pipeline同一个代码可以作用于不同分支,不同分支得构建/部署不同得情况下,我都是在开头去判断得
后面你代码就可以直接 if(isReleaseBranch){xxxx}了
五.List Git Branches插件
插件List Git Branches可以根据选项获取远程仓库的分支或者tag,形成列表选项,除了在job进行图形配置,还可以通过pipeline进行配置,在job过百的情况下搭配共享库会很方便。
界面配置:
具体说明
Name:变量名词,用于传递到pipeline根据分支发布
Repository URL:远程仓库的地址,从这个仓库获取分支
Credentials:选择的凭证,根据这个来访问远程仓库
Parameter Type:获取的类型,可选TAG、分支、分支+TAG
Sort Mode:排序的方式
Tag Filter:过滤tag的正则,如果获取类型是分支,这个配置可以忽略
Branch Filter:过滤分支的正则
Default Value:如果获取不到默认的分支
Selected Value:不太清楚,选择NONE或者DEFALUT即可
List Size:这是显示的列表个数
效果:
pipeline方式:
pipeline {
agent any
parameters {
listGitBranches branchFilter: 'refs/heads/(.*)',
defaultValue: 'master',
name: 'branch_name',
type: 'PT_BRANCH',
remoteURL: 'http://10.0.15.1/xxx/xxx.git',
credentialsId: 'jenkins',
selectedValue: 'DEFAULT',
sortMode: 'ASCENDING'
}
stages {
stage('Example') {
steps {
git branch: "${params.branch_name}", credentialsId: 'jenkins', url: 'http://10.0.15.1/xxx/xxx.git'
}
}
}
}
最新文章
- 用实例讲解Spark Sreaming--转
- Date类型,Boolean类型,Number类型
- subeclipse 安装
- poll函数和串口设置
- wcf iis host 打开exe失败 不能显示界面
- iOS中常用的第三方
- 对Spring的一些理解
- UINavigationController 导航控制器
- 【转】如何查看linux版本 如何查看LINUX是多少位
- AFNetworking封装思路简析
- Learning How to Learn, Part 1
- 华为机试题【10】-求数字基root
- Game Engine Architecture 11
- If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
- js处理异步的几种方式
- 20165232 2017-2018-2《Java程序设计》课程总结
- 【MSSQL】How can i see what IP address made the request to SQL Server?
- openstack安装-计算节点-neutron服务安装
- 统计UPD丢包工具
- 【译】Surface中你也许不知道的五件事
热门文章
- Spring Cloud Gateway实战之四:内置predicate小结
- 菜鸡的Java笔记 第二十四 - java 接口的基本定义
- nginx 支持websocket
- 【Rancher相关问题】Rancher 2.5.8 及以下版本,提示Alert: Component controller-manager,scheduler is unhealthy.
- [gym102798F]Skeleton Dynamization
- [bzoj1109]堆积木
- 从零开始学Kotlin第五课
- k8s-数据持久化存储卷,nfs,pv/pvc
- Codeforces 348C - Subset Sums(根号分治)
- Codeforces 79D - Password(状压 dp+差分转化)