blob: 0624c40f9efeed4a1013416ffecdce23c3854f4c [file] [log] [blame]
Alexander Evseevbdf97c92018-08-09 17:21:37 +02001#!groovy
2
3/**
4 * (Re-)Create git branches
5 *
6 * @param GIT_REPO_LIST List of repositories to handle
7 * Multiline text: '<name> <url> <src_obj>' (full format)
8 * or: '<url>' (assuming src_obj=='SUBS_SOURCE_REF')
9 * @param GIT_CREDENTIALS Credentials ID to use for the ALL given repositories
10 * @param BRANCH New branch name
11 * @param SOURCE_REVISION Source object (commit/tag/branch) to apply to all repos
12 * having empty src_obj or src_obj=='SUBS_SOURCE_REF'
13 *
14 * @see <a href="https://mirantis.jira.com/browse/PROD-17759">PROD-17759</a>
15 */
16
17// Get job environment to use as a map to get values with defaults
18Map jobEnv = env.getEnvironment().findAll { k, v -> v }
19
20// Prepare job parameters
21ArrayList gitRepoList = jobEnv.get('GIT_REPO_LIST', '').readLines()
22String gitBranchNew = jobEnv.get('BRANCH')
23String srcObj = jobEnv.get('SOURCE_REVISION', 'master')
24String gitCredentialsId = jobEnv.get('GIT_CREDENTIALS')
25
26// Check if new branch name is given
27if (! gitBranchNew) {
28 error ('No new branch name is given')
29}
30
31/**
32 * Returns local path for the given URL constructed from hostname and repository
33 *
34 * @param repoUrl git repository URL
35 * @return string representing local relative patch
36 */
37String getRepoLocalPath(String repoUrl) {
38 // Regex to split git repository URLs
39 String re = '^(?:(?<proto>[a-z]+)://)?(?:(?<creds>[^@]+)@)?(?<host>[^:/]+)(?::(?<port>[0-9]+)/|[:/])(?<repo>.+)$'
40
41 java.util.regex.Matcher urlMatcher = repoUrl =~ re
42 if (urlMatcher.matches()) {
43 return new File(
44 urlMatcher.group('host'),
45 urlMatcher.group('repo').replaceAll(/\.git$/,'')
46 ).toString()
47 } else {
48 return ''
49 }
50}
51
52// Variables to use as repo parameters
53String gitRepoName
54String gitRepoUrl
55String gitSrcObj
56
57// Store current commit SHA
58String gitCommit
59
60node() {
61 for (gitRepo in gitRepoList) {
62 (gitRepoName, gitRepoUrl, gitSrcObj) = gitRepo.trim().tokenize(' ')
63
64 if (gitRepoName.startsWith('#')){
65 echo ("Skipping repo '${gitRepo}'")
66 continue
67 }
68
69 if (! gitRepoUrl) {
70 // The only token is the git repo url
71 gitRepoUrl = gitRepoName
72 gitRepoName = getRepoLocalPath(gitRepoUrl)
73 gitSrcObj = srcObj
74 } else if (! gitSrcObj) {
75 // Two tokens - can't decide is gitRepoName or gitSrcObj given
76 error ("Wrong repository string format: '${gitRepo}'")
77 }
78
79 if (gitSrcObj.contains('SUBS_SOURCE_REF')) {
80 echo ("Replacing 'SUBS_SOURCE_REF' => ${SOURCE_REVISION}")
Alexander Evseev5064d622018-08-27 15:56:37 +020081 gitSrcObj = gitSrcObj.replace('SUBS_SOURCE_REF', srcObj)
Alexander Evseevbdf97c92018-08-09 17:21:37 +020082 }
83
84 // Remove preifix `origin/` from gitSrcObj
85 java.util.regex.Pattern reOrigin = ~'^origin/'
86 gitSrcObj = gitSrcObj - reOrigin
87
88 checkout([
89 $class: 'GitSCM',
90 branches: [
91 [name: 'FETCH_HEAD'],
92 ],
93 userRemoteConfigs: [
94 [url: gitRepoUrl, refspec: gitSrcObj, credentialsId: gitCredentialsId],
95 ],
96 extensions: [
97 [$class: 'PruneStaleBranch'],
98 [$class: 'RelativeTargetDirectory', relativeTargetDir: gitRepoName],
99 [$class: 'SubmoduleOption', disableSubmodules: true],
100 [$class: 'UserIdentity', name: 'MCP CI', email: 'ci+infra@mirantis.com'],
101 ],
102 ])
103
104 // Proceed branch creation
105 dir(gitRepoName) {
106 sshagent (credentials: [gitCredentialsId]) {
107 // FIXME: Ensure git has configured user and email
108 // See: https://issues.jenkins-ci.org/browse/JENKINS-46052
109 sh 'git config user.name "MCP CI"'
110 sh 'git config user.email "ci+infra@mirantis.com"'
111
112 // Update list of branches
Alexander Evseev28eb6bd2018-10-29 12:34:13 +0000113 sh 'git remote update origin --prune'
Alexander Evseevbdf97c92018-08-09 17:21:37 +0200114
115 // Ensure there is no branch or tag with gitBranchNew name
116 sh "git branch -d '${gitBranchNew}' && git push origin ':${gitBranchNew}' || :"
117 sh "git tag -d '${gitBranchNew}' && git push origin ':refs/tags/${gitBranchNew}' || :"
118
Alexander Evseevbdf97c92018-08-09 17:21:37 +0200119 // Create new branch
Alexander Evseev28eb6bd2018-10-29 12:34:13 +0000120 sh "git checkout -b '${gitBranchNew}' '${gitSrcObj}'" // Create new local branch
Alexander Evseevb1029262018-10-08 17:18:13 +0200121 sh "git push origin '${gitBranchNew}'" // ... push new branch
Alexander Evseevbdf97c92018-08-09 17:21:37 +0200122 }
123 }
124 }
125}