jenkins pipline脚本 获取git分支

必须先禁用脚本安全性,参考文章:https://www.cnblogs.com/xiao987334176/p/18365397

点击Manage Jenkins-->Script Console

输入以下脚本:

复制代码
// 定义Git命令
def cmd = "git ls-remote --heads https://oauth2:xxxxxx@xx.gitlab.com/xx.git"
  
// 执行命令并获取输出
def proc = cmd.execute()
proc.waitFor() // 等待命令执行完成
  
// 从执行结果中获取标准输出文本
def text = proc.text
//println text
  
// 将输出文本按行分割,并提取分支名
def branches = text.readLines().collect { line ->
  //println line
  def parts = line.tokenize("\t")
  //println parts
  def branch = parts.last().replaceAll("refs/heads/", "")
  //println branch
}
  
// 输出分支列表
//println branches
  
// 如果需要转换为List数据结构,可以使用toList()方法
def branchList = branches.toList()
  
// 输出List
println branchList

注意:oauth2是固定的,后面的xxxx是授权的token

执行有输出分支,就表示成功了

相关推荐
222you20 小时前
git的命令
git
K_i13421 小时前
OSI七层模型:从原理到实战
运维·服务器·网络
jixunwulian21 小时前
5G+AIoT智赋,AI电力加密边缘网关智慧电网数字化运维解决方案
运维·人工智能·5g
Coolbike21 小时前
Git工作流
git
小小小糖果人1 天前
Linux云计算基础篇(25)-DNS配置
linux·运维·云计算
TIANE-Kimmy1 天前
VS code定时任务设置(mac os)
linux·运维·服务器
panshiyangmaye1 天前
RHCSA作业1
linux·运维·服务器
一张假钞1 天前
Windows下Jenkins服务未自动重启问题解决
jenkins
Narutolxy1 天前
从混合部署到高可用:在内网环境下搭建 GitLab-Jenkins-OpenResty的完整实战复盘20251014
gitlab·jenkins·openresty