如何在詹金斯管道中获取回购名称 [英] how to get repo name in Jenkins pipeline

查看:328
本文介绍了如何在詹金斯管道中获取回购名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用使用Groovy样式脚本的Jenkins脚本化管道,并创建了一个Jenkinsfile来描述管道.我需要使用与git repo name相同的文件夹名称创建工作区,然后在工作区文件夹中签出代码. 我的问题是,在执行checkout scm之前,是否有办法知道git repo名称或git repo url?

I'm using Jenkins Scripted Pipeline that uses Groovy style scripting, and created a Jenkinsfile to describe the pipeline. I need to create the workspace with the folder name same as git repo name, and then checkout the code in the workspace folder. My question is, before doing the checkout scm, is there a way to know the git repo name or the git repo url?

推荐答案

String determineRepoName() {
    return scm.getUserRemoteConfigs()[0].getUrl().tokenize('/')[3].split("\\.")[0]
}

这个相对丑陋的代码是我用来获取repoName的代码.关键是回购的URL存储在:

This relatively ugly code is what I use to get the repoName. The key is that the URL of the repo is stored in:

scm.getUserRemoteConfigs()[0] .getUrl()

scm.getUserRemoteConfigs()[0].getUrl()

从那里开始,您需要执行一些字符串操作以获得所需的内容.

from there you need to do some string ops to get what you want.

更新:

String determineRepoName() {
    return scm.getUserRemoteConfigs()[0].getUrl().tokenize('/').last().split("\\.")[0]
}

这也适用于具有更深层次结构的存储库( https://domain/project/subproject/repo 或ssh git repo,在开始时不包含两个//.

This works also for repositories with a deeper hierarchy (https://domain/project/subproject/repo or ssh git repo which does not contain the two // at the start.

这篇关于如何在詹金斯管道中获取回购名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆