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

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

相关推荐
TTGGGFF1 天前
云端服务器使用指南:如何跨机传输较大文件(通过windows自带工具远程桌面连接 非常方便)
运维·服务器
躲在云朵里`1 天前
ElasticSearch复习指南:从零搭建一个商品搜索案例
运维·jenkins
yyy0002001 天前
压缩和归档 文件传输
linux·运维·服务器
STCNXPARM1 天前
深度剖析Linux内核无线子系统架构
linux·运维·系统架构·wifi·无线子系统
python_1361 天前
git常见冲突场景及解决办法
git
m0_694845571 天前
教你使用服务器如何搭建数据库
linux·运维·服务器·数据库·云计算
空灵之海1 天前
Ubuntu Server 22.04.5系统安装教程
linux·运维·ubuntu
arbboter1 天前
【自动化】深入浅出UIAutomationClient:C#桌面自动化实战指南
运维·c#·自动化·inspect·uiautomation·uia·桌面自动化
二进制_博客1 天前
给CentOS的虚拟机扩容
linux·运维·centos
千钰v1 天前
Tcpdump: The Basics Tcpdump 基础
linux·运维·网络·tcpdump·tryhackme