简体   繁体   中英

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. 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. Is this doable, and if so, how?

The repository is available in the config.xml of the job.

http://[jenkins server]/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. So when you call project.getSCM() you have to then cast it to the type of SCM that you actually have (GitSCM, SubversionSCM, etc.).

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. Simply looping through the ModuleLocation[] and calling getSVNURL() gets me the information I want. I can then play around with the SVNURL as needed ( toString() gives me the full repository path).

In your case, for Git, the source code shows that there is a method called getRepositories() , which returns a List<RemoteConfig> . Look through the RemoteConfig source code to see which method gets you the information you need. (I believe getURIs() will do the trick).

I managed to get URL of SVN repository for a particular job using following code:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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