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

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

相关推荐
likfishdn10 分钟前
Linux的文件与目录管理
linux·运维·服务器
2501_9032386521 分钟前
Git Bash:Windows下的强大命令行工具
windows·git·bash·个人开发
chian-ocean30 分钟前
Linux 文件缓冲区:高效数据访问的幕后推手
linux·运维·服务器
加油=^_^=32 分钟前
【Linux】进程优先级 | 进程调度(三)
linux·运维·服务器
元气满满的热码式1 小时前
Docker数据卷操作实战
linux·运维·docker·云原生·容器
北京耐用通信1 小时前
航空装配自动化神器Ethercat转profient网关搭配机器人精准控制
运维·机器人·自动化
吃汤圆的抹香鲸2 小时前
Rider 安装包 绿色版 Win/Mac/Linux 适合.NET和游戏开发者使用 2025全栈开发终极指南:从零配置到企业级实战
linux·运维·windows·sql·游戏·macos·.net
神明木佑2 小时前
datalist 是什么?以及作用是什么?
git
那片海还在吗4 小时前
使用 Git、Postman、Newman、Jenkins 与邮件构建自动化接口测试及通知流程
git·jenkins·postman
崔lc4 小时前
linux中安装部署Jenkins,成功构建springboot项目详细教程
运维·jenkins