SCM module: Add support for multiple Git remotes.

Configuring multiple Git remotes is necessary to allow Jenkins to
merge commits from a remote branch into a local branch. Note that
this is not the same as specifying multiple SCMs which are cloned
into separate directories.

Change-Id: Ifef2da85fa22a979570d4ea2b6f30d4bd37da116
This commit is contained in:
Martin Konrad 2014-03-12 16:00:46 -04:00
parent 06e68a97c9
commit 8a956bcc74
3 changed files with 81 additions and 10 deletions

View File

@ -44,6 +44,13 @@ def git(self, xml_parent, data):
:arg str credentials-id: ID of credentials to use to connect (optional)
:arg str refspec: refspec to fetch
:arg str name: name to fetch
:arg list(str) remotes: list of remotes to set up (optional, only needed if
multiple remotes need to be set up)
:Remote: * **url** (`string`) - url of remote repo
* **refspec** (`string`) - refspec to fetch (optional)
* **credentials-id** - ID of credentials to use to connect
(optional)
:arg list(str) branches: list of branch specifiers to build
:arg list(str) excluded-users: list of users to ignore revisions from
when polling for changes. (if polling is enabled)
@ -133,16 +140,27 @@ def git(self, xml_parent, data):
'scm', {'class': 'hudson.plugins.git.GitSCM'})
XML.SubElement(scm, 'configVersion').text = '2'
user = XML.SubElement(scm, 'userRemoteConfigs')
huser = XML.SubElement(user, 'hudson.plugins.git.UserRemoteConfig')
XML.SubElement(huser, 'name').text = data.get('name', 'origin')
if 'refspec' in data:
refspec = data['refspec']
else:
refspec = '+refs/heads/*:refs/remotes/origin/*'
XML.SubElement(huser, 'refspec').text = refspec
XML.SubElement(huser, 'url').text = data['url']
if 'credentials-id' in data:
XML.SubElement(huser, 'credentialsId').text = data['credentials-id']
if 'remotes' not in data:
data['remotes'] = [{data.get('name', 'origin'): data}]
for remoteData in data['remotes']:
huser = XML.SubElement(user, 'hudson.plugins.git.UserRemoteConfig')
remoteName = remoteData.keys()[0]
XML.SubElement(huser, 'name').text = remoteName
remoteParams = remoteData.values()[0]
if 'refspec' in remoteParams:
refspec = remoteParams['refspec']
else:
refspec = '+refs/heads/*:refs/remotes/' + remoteName + '/*'
XML.SubElement(huser, 'refspec').text = refspec
if 'url' in remoteParams:
remoteURL = remoteParams['url']
else:
raise JenkinsJobsException('Must specify a url for git remote \"' +
remoteName + '"')
XML.SubElement(huser, 'url').text = remoteURL
if 'credentials-id' in remoteParams:
credentialsId = remoteParams['credentials-id']
XML.SubElement(huser, 'credentialsId').text = credentialsId
xml_branches = XML.SubElement(scm, 'branches')
branches = data.get('branches', ['**'])
for branch in branches:

View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<scm class="hudson.plugins.git.GitSCM">
<configVersion>2</configVersion>
<userRemoteConfigs>
<hudson.plugins.git.UserRemoteConfig>
<name>internal</name>
<refspec>+refs/heads/*:refs/remotes/internal/*</refspec>
<url>ssh://git@internal.example.com/foobar.git</url>
<credentialsId>01234567-89ab-cdef-0123-456789abcdef</credentialsId>
</hudson.plugins.git.UserRemoteConfig>
<hudson.plugins.git.UserRemoteConfig>
<name>github</name>
<refspec>+refs/heads/*:refs/remotes/github/*</refspec>
<url>https://github.com/exampleuser/foobar.git</url>
</hudson.plugins.git.UserRemoteConfig>
</userRemoteConfigs>
<branches>
<hudson.plugins.git.BranchSpec>
<name>github/master</name>
</hudson.plugins.git.BranchSpec>
</branches>
<excludedUsers/>
<buildChooser class="hudson.plugins.git.util.DefaultBuildChooser"/>
<disableSubmodules>false</disableSubmodules>
<recursiveSubmodules>false</recursiveSubmodules>
<doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
<authorOrCommitter>false</authorOrCommitter>
<clean>false</clean>
<wipeOutWorkspace>true</wipeOutWorkspace>
<pruneBranches>false</pruneBranches>
<remotePoll>false</remotePoll>
<gitTool>Default</gitTool>
<submoduleCfg class="list"/>
<relativeTargetDir/>
<reference/>
<gitConfigName/>
<gitConfigEmail/>
<skipTag>false</skipTag>
<scmName/>
<useShallowClone>false</useShallowClone>
</scm>
</project>

View File

@ -0,0 +1,10 @@
scm:
- git:
remotes:
- internal:
url: ssh://git@internal.example.com/foobar.git
credentials-id: 01234567-89ab-cdef-0123-456789abcdef
- github:
url: https://github.com/exampleuser/foobar.git
branches:
- github/master