简体   繁体   English

在Jenkins构建插件中获取作业库URL

[英]Getting job repository URL in Jenkins build plugin

I am writing a build notification plugin for Jenkins. 我正在为詹金斯编写一个构建通知插件。 The SCM repository URL (SCM being git to begin with) contains useful information I'd want to get to in my code. SCM存储库URL(SCM是开头的git)包含我想在我的代码中获得的有用信息。 Being the beginner with the Jenkins API that I am, I am having trouble figuring out how I should go about retrieving the job's repository location. 作为我的Jenkins API的初学者,我无法弄清楚如何检索作业的存储库位置。 Is this doable, and if so, how? 这是可行的,如果是的话,怎么样?

The repository is available in the config.xml of the job. 存储库在作业的config.xml中可用。

http://[jenkins server]/job/[job]/config.xml http:// [jenkins服务器] / job / [job] /config.xml

Finally found the answer! 终于找到答案了! The method needed is not defined in the abstract SCM class, but by the classes which extend it. 所需的方法不是在抽象SCM类中定义的,而是由对其进行扩展的类定义的。 So when you call project.getSCM() you have to then cast it to the type of SCM that you actually have (GitSCM, SubversionSCM, etc.). 因此,当您调用project.getSCM() ,必须将其强制转换为实际拥有的SCM类型(GitSCM,SubversionSCM等)。

I am developing my plugin to work with Subversion, so I looked through the source code for the Subversion Plugin, and found the method getLocations which returns a ModuleLocation[] which contains information on all the SVN repositories in that project. 我正在开发我的插件以使用Subversion,所以我查看了Subversion插件的源代码 ,并找到了getLocations方法,它返回一个ModuleLocation[] ,其中包含有关该项目中所有SVN存储库的信息。 Simply looping through the ModuleLocation[] and calling getSVNURL() gets me the information I want. 只需循环访问ModuleLocation[]并调用getSVNURL()即可获得所需的信息。 I can then play around with the SVNURL as needed ( toString() gives me the full repository path). 然后,我可以根据需要使用SVNURLtoString()为我提供了完整的存储库路径)。

In your case, for Git, the source code shows that there is a method called getRepositories() , which returns a List<RemoteConfig> . 在您的情况下,对于Git, 源代码显示有一个名为getRepositories()的方法,它返回List<RemoteConfig> Look through the RemoteConfig source code to see which method gets you the information you need. 查看RemoteConfig 源代码 ,了解哪种方法可以获得所需的信息。 (I believe getURIs() will do the trick). (我相信getURIs()会做到这一点)。

I managed to get URL of SVN repository for a particular job using following code: 我设法使用以下代码获取特定工作的SVN存储库的URL:

String jobName = manager.build.project.getName()
def job = hudson.model.Hudson.instance.getItem(jobName)
def svnScm = job.scm
def svnLocation = svnScm.getLocations()
def svnURLstr = svnLocation[0].getSVNURL().toString()

Perhaps it will be useful to someone. 也许对某人有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM