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

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

相关推荐
AC赳赳老秦40 分钟前
财务报销自动化:用 OpenClaw 自动识别发票信息、填写报销单、校验报销规则,减少手工操作
运维·网络·eclipse·github·visual studio·deepseek·openclaw
zhangfeng11331 小时前
适合 5人以内小团队的Git 工作流 + Code Review + 自动化部署方案 FastAdmin +linunx服务器宝塔系统 外包项目 —
服务器·git·自动化·php·代码复审
倔强的石头1062 小时前
【Linux 指南】文件系统系列(一):磁盘底层原理 —— 从物理结构到 CHS与LBA 寻址全解析
linux·运维·服务器
陶然同学3 小时前
【Linux及Shell】VMware&Ubuntu&Xshell安装
linux·运维·xshell8·xftp8
小乐笙3 小时前
笔记:纯真 IP 库使用
运维·网络·tcp/ip
咖喱o3 小时前
DHCP
linux·运维·服务器·网络
IMPYLH4 小时前
Linux 的 touch 命令
linux·运维·服务器·bash
uestcwhc4 小时前
服务器定时发送邮件设置
运维·服务器
雷工笔记4 小时前
MES 系统设备管理模块详细设计方案
大数据·运维·网络