blob: 4bfc3d788e5bd04dc7f1a6539acd26dce70cab6f [file] [log] [blame]
Sergey Kolekonovba203982016-12-21 18:32:17 +04001package com.mirantis.mk
2
3/**
4 *
5 * Git functions
6 *
7 */
8
9/**
10 * Checkout single git repository
11 *
12 * @param path Directory to checkout repository to
13 * @param url Source Git repository URL
14 * @param branch Source Git repository branch
15 * @param credentialsId Credentials ID to use for source Git
16 */
17def checkoutGitRepository(path, url, branch, credentialsId = null){
18 checkout([
19 $class: 'GitSCM',
20 branches: [[name: "*/${branch}"]],
21 doGenerateSubmoduleConfigurations: false,
22 extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: path]],
23 submoduleCfg: [],
24 userRemoteConfigs: [[url: url, credentialsId: credentialsId]]
25 ])
26 dir(path) {
27 sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
28 }
29}
30
31/**
32 * Parse HEAD of current directory and return commit hash
33 */
34def getGitCommit() {
35 git_commit = sh (
36 script: 'git rev-parse HEAD',
37 returnStdout: true
38 ).trim()
39 return git_commit
40}
41
42/**
Filip Pytloun49d66302017-03-06 10:26:22 +010043 * Get remote URL
44 *
45 * @param name Name of remote (default origin)
46 * @param type Type (fetch or push, default fetch)
47 */
48def getGitRemote(name = 'origin', type = 'fetch') {
49 gitRemote = sh (
50 script: "git remote | grep ${name} | grep ${type} | awk '{print \$2}'",
51 returnStdout: true
52 ).trim()
53 return gitRemote
54}
55
56/**
Ales Komarekfb7cbcb2017-02-24 14:02:03 +010057 * Change actual working branch of repo
58 *
59 * @param path Path to the git repository
60 * @param branch Branch desired to switch to
61 */
62def changeGitBranch(path, branch) {
63 dir(path) {
64 git_cmd = sh (
65 script: "git checkout -b ${branch}",
66 returnStdout: true
67 ).trim()
68 }
69 return git_cmd
70}
71
72/**
73 * Commit changes to the git repo
74 *
75 * @param path Path to the git repository
76 * @param message A commit message
77 */
78def commitGitChanges(path, message) {
79 dir(path) {
80 sh(
81 script: 'git add -A',
82 returnStdout: true
83 ).trim()
84 git_cmd = sh(
85 script: "git commit -m '${message}'",
86 returnStdout: true
87 ).trim()
88 }
89 return git_cmd
90}
91
92
93/**
94 * Push git changes to remote repo
95 *
96 * @param path Path to the git repository
97 * @param branch Branch on the remote git repository
98 * @param remote Name of the remote repository
99 */
100def pushGitChanges(path, branch = 'master', remote = 'origin') {
101 dir(path) {
102 git_cmd = sh(
103 script: "git push ${remote} ${branch}",
104 returnStdout: true
105 ).trim()
106 }
107 return git_cmd
108}
109
110
111/**
Sergey Kolekonovba203982016-12-21 18:32:17 +0400112 * Checkout git repositories in parallel
113 *
114 * @param path Directory to checkout to
115 * @param url Git repository url
116 * @param branch Git repository branch
117 * @param credentialsId Credentials ID to use
118 * @param poll Poll automatically
119 * @param clean Clean status
120 */
121def checkoutGitParallel(path, url, branch, credentialsId = null, poll = true, clean = true) {
122 return {
123 print "Checking out ${url}, branch ${branch} into ${path}"
124 dir(path) {
125 git url: url,
126 branch: branch,
127 credentialsId: credentialsId,
128 poll: poll,
129 clean: clean
130 }
131 }
132}
133
134/**
Filip Pytloun49d66302017-03-06 10:26:22 +0100135 * Mirror git repository, merge target changes (downstream) on top of source
136 * (upstream) and push target or both if pushSource is true
137 *
138 * @param sourceUrl Source git repository
139 * @param targetUrl Target git repository
140 * @param credentialsId Credentials id to use for accessing source/target
141 * repositories
142 * @param branches List or comma-separated string of branches to sync
143 * @param followTags Mirror tags
144 * @param pushSource Push back into source branch, resulting in 2-way sync
145 * @param pushSourceTags Push target tags into source or skip pushing tags
146 * @param gitEmail Email for creation of merge commits
147 * @param gitName Name for creation of merge commits
Sergey Kolekonovba203982016-12-21 18:32:17 +0400148 */
Filip Pytloun49d66302017-03-06 10:26:22 +0100149def mirrorGit(sourceUrl, targetUrl, credentialsId, branches, followTags = false, pushSource = false, pushSourceTags = false, gitEmail = 'jenkins@localhost', gitName = 'Jenkins') {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400150 if (branches instanceof String) {
151 branches = branches.tokenize(',')
152 }
Sergey Kolekonovba203982016-12-21 18:32:17 +0400153
Filip Pytloun49d66302017-03-06 10:26:22 +0100154 def ssh = new com.mirantis.mk.Ssh()
155 ssh.prepareSshAgentKey(credentialsId)
156 ssh.ensureKnownHosts(targetUrl)
157 sh "git config user.email '${gitEmail}'"
158 sh "git config user.name '${gitName}'"
159
160 sh "git remote | grep target || git remote add target ${TARGET_URL}"
161 ssh.agentSh "git remote update --prune"
162
Sergey Kolekonovba203982016-12-21 18:32:17 +0400163 for (i=0; i < branches.size; i++) {
164 branch = branches[i]
165 sh "git branch | grep ${branch} || git checkout -b ${branch} origin/${branch}"
166 sh "git branch | grep ${branch} && git checkout ${branch} && git reset --hard origin/${branch}"
167
Sergey Kolekonovba203982016-12-21 18:32:17 +0400168 sh "git ls-tree target/${branch} && git merge --no-edit --ff target/${branch} || echo 'Target repository is empty, skipping merge'"
169 followTagsArg = followTags ? "--follow-tags" : ""
Filip Pytloun49d66302017-03-06 10:26:22 +0100170 ssh.agentSh "git push ${followTagsArg} target HEAD:${branch}"
171
172 if (pushSource == true) {
173 followTagsArg = followTags && pushSourceTags ? "--follow-tags" : ""
174 agentSh "git push ${followTagsArg} origin HEAD:${branch}"
175 }
176 }
177
178 if (followTags == true) {
179 ssh.agentSh "git push target --tags"
180
181 if (pushSourceTags == true) {
182 ssh.agentSh "git push origin --tags"
183 }
Sergey Kolekonovba203982016-12-21 18:32:17 +0400184 }
185}