blob: 1564fec2ff5eb73850ad9ed31696aca605c87486 [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
Jakub Josef7dccebe2017-03-06 18:08:32 +010016 * @param poll Enable git polling (default true)
17 * @param timeout Set checkout timeout (default 10)
Jakub Josef61f29e62017-03-08 16:42:06 +010018 * @param depth Git depth param (default 0 means no depth)
Sergey Kolekonovba203982016-12-21 18:32:17 +040019 */
Jakub Josef61f29e62017-03-08 16:42:06 +010020def checkoutGitRepository(path, url, branch, credentialsId = null, poll = true, timeout = 10, depth = 0){
Sergey Kolekonovba203982016-12-21 18:32:17 +040021 dir(path) {
Jakub Josef6fa8cb12017-03-06 18:20:08 +010022 checkout(
23 changelog:true,
24 poll: poll,
25 scm: [
26 $class: 'GitSCM',
27 branches: [[name: "*/${branch}"]],
28 doGenerateSubmoduleConfigurations: false,
29 extensions: [
Jakub Josef61f29e62017-03-08 16:42:06 +010030 [$class: 'CheckoutOption', timeout: timeout],
Jakub Josef1589f9a2017-03-08 17:41:21 +010031 [$class: 'CloneOption', depth: depth, noTags: false, reference: '', shallow: depth > 0, timeout: timeout]],
Jakub Josef6fa8cb12017-03-06 18:20:08 +010032 submoduleCfg: [],
33 userRemoteConfigs: [[url: url, credentialsId: credentialsId]]]
34 )
Sergey Kolekonovba203982016-12-21 18:32:17 +040035 sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
36 }
37}
38
39/**
40 * Parse HEAD of current directory and return commit hash
41 */
42def getGitCommit() {
43 git_commit = sh (
44 script: 'git rev-parse HEAD',
45 returnStdout: true
46 ).trim()
47 return git_commit
48}
49
50/**
Ales Komarekfb7cbcb2017-02-24 14:02:03 +010051 * Change actual working branch of repo
52 *
53 * @param path Path to the git repository
54 * @param branch Branch desired to switch to
55 */
56def changeGitBranch(path, branch) {
57 dir(path) {
58 git_cmd = sh (
Leontii Istominb4f4ae12018-02-27 20:25:43 +010059 script: "git checkout ${branch}",
Ales Komarekfb7cbcb2017-02-24 14:02:03 +010060 returnStdout: true
61 ).trim()
62 }
63 return git_cmd
64}
65
66/**
Ales Komarekc3a8b972017-03-24 13:57:25 +010067 * Get remote URL
68 *
69 * @param name Name of remote (default any)
70 * @param type Type (fetch or push, default fetch)
71 */
72def getGitRemote(name = '', type = 'fetch') {
73 gitRemote = sh (
74 script: "git remote -v | grep '${name}' | grep ${type} | awk '{print \$2}' | head -1",
75 returnStdout: true
76 ).trim()
77 return gitRemote
78}
79
80/**
81 * Create new working branch for repo
82 *
83 * @param path Path to the git repository
84 * @param branch Branch desired to switch to
85 */
86def createGitBranch(path, branch) {
87 def git_cmd
88 dir(path) {
89 git_cmd = sh (
90 script: "git checkout -b ${branch}",
91 returnStdout: true
92 ).trim()
93 }
94 return git_cmd
95}
96
97/**
Ales Komarekfb7cbcb2017-02-24 14:02:03 +010098 * Commit changes to the git repo
99 *
100 * @param path Path to the git repository
101 * @param message A commit message
Denis Egorenkof4c45512019-03-04 15:53:36 +0400102 * @param global Use global config
Ales Komarekfb7cbcb2017-02-24 14:02:03 +0100103 */
Denis Egorenkof4c45512019-03-04 15:53:36 +0400104def commitGitChanges(path, message, gitEmail='jenkins@localhost', gitName='jenkins-slave', global=false) {
Ales Komarekc3a8b972017-03-24 13:57:25 +0100105 def git_cmd
Denis Egorenkof4c45512019-03-04 15:53:36 +0400106 def global_arg = ''
107 if (global) {
108 global_arg = '--global'
109 }
Ales Komarekfb7cbcb2017-02-24 14:02:03 +0100110 dir(path) {
Denis Egorenkof4c45512019-03-04 15:53:36 +0400111 sh "git config ${global_arg} user.email '${gitEmail}'"
112 sh "git config ${global_arg} user.name '${gitName}'"
Tomáš Kukráldf7bebc2017-03-27 15:12:43 +0200113
Ales Komarekfb7cbcb2017-02-24 14:02:03 +0100114 sh(
115 script: 'git add -A',
116 returnStdout: true
117 ).trim()
118 git_cmd = sh(
119 script: "git commit -m '${message}'",
120 returnStdout: true
121 ).trim()
122 }
123 return git_cmd
124}
125
126
127/**
128 * Push git changes to remote repo
129 *
Ales Komarekc3a8b972017-03-24 13:57:25 +0100130 * @param path Path to the local git repository
Ales Komarekfb7cbcb2017-02-24 14:02:03 +0100131 * @param branch Branch on the remote git repository
132 * @param remote Name of the remote repository
Ales Komarekc3a8b972017-03-24 13:57:25 +0100133 * @param credentialsId Credentials with write permissions
Ales Komarekfb7cbcb2017-02-24 14:02:03 +0100134 */
Ales Komarekc3a8b972017-03-24 13:57:25 +0100135def pushGitChanges(path, branch = 'master', remote = 'origin', credentialsId = null) {
136 def ssh = new com.mirantis.mk.Ssh()
Ales Komarekfb7cbcb2017-02-24 14:02:03 +0100137 dir(path) {
Ales Komarekc3a8b972017-03-24 13:57:25 +0100138 if (credentialsId == null) {
139 sh script: "git push ${remote} ${branch}"
140 }
141 else {
142 ssh.prepareSshAgentKey(credentialsId)
143 ssh.runSshAgentCommand("git push ${remote} ${branch}")
144 }
Ales Komarekfb7cbcb2017-02-24 14:02:03 +0100145 }
Ales Komarekfb7cbcb2017-02-24 14:02:03 +0100146}
147
Ales Komarekc3a8b972017-03-24 13:57:25 +0100148
Sergey Kolekonovba203982016-12-21 18:32:17 +0400149/**
Filip Pytloun49d66302017-03-06 10:26:22 +0100150 * Mirror git repository, merge target changes (downstream) on top of source
151 * (upstream) and push target or both if pushSource is true
152 *
153 * @param sourceUrl Source git repository
154 * @param targetUrl Target git repository
155 * @param credentialsId Credentials id to use for accessing source/target
156 * repositories
157 * @param branches List or comma-separated string of branches to sync
158 * @param followTags Mirror tags
159 * @param pushSource Push back into source branch, resulting in 2-way sync
160 * @param pushSourceTags Push target tags into source or skip pushing tags
161 * @param gitEmail Email for creation of merge commits
162 * @param gitName Name for creation of merge commits
Sergey Kolekonovba203982016-12-21 18:32:17 +0400163 */
Filip Pytloun49d66302017-03-06 10:26:22 +0100164def mirrorGit(sourceUrl, targetUrl, credentialsId, branches, followTags = false, pushSource = false, pushSourceTags = false, gitEmail = 'jenkins@localhost', gitName = 'Jenkins') {
Jakub Josef668dc2b2017-06-19 16:55:26 +0200165 def common = new com.mirantis.mk.Common()
166 def ssh = new com.mirantis.mk.Ssh()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400167 if (branches instanceof String) {
168 branches = branches.tokenize(',')
169 }
Sergey Kolekonovba203982016-12-21 18:32:17 +0400170
Filip Pytloun49d66302017-03-06 10:26:22 +0100171 ssh.prepareSshAgentKey(credentialsId)
172 ssh.ensureKnownHosts(targetUrl)
173 sh "git config user.email '${gitEmail}'"
174 sh "git config user.name '${gitName}'"
175
Jakub Josef1caa7ae2017-08-21 16:39:00 +0200176 def remoteExistence = sh(script: "git remote -v | grep ${TARGET_URL} | grep target", returnStatus: true)
177 if(remoteExistence != 0){
178 // silently try to remove target
179 sh(script:"git remote remove target", returnStatus: true)
180 sh("git remote add target ${TARGET_URL}")
181 }
Filip Pytloun49d66302017-03-06 10:26:22 +0100182 ssh.agentSh "git remote update --prune"
183
Sergey Kolekonovba203982016-12-21 18:32:17 +0400184 for (i=0; i < branches.size; i++) {
185 branch = branches[i]
Jakub Josef668dc2b2017-06-19 16:55:26 +0200186 sh "git branch | grep ${branch} || git checkout -b ${branch}"
187 def resetResult = sh(script: "git checkout ${branch} && git reset --hard origin/${branch}", returnStatus: true)
188 if(resetResult != 0){
189 common.warningMsg("Cannot reset to origin/${branch} for perform git mirror, trying to reset from target/${branch}")
190 resetResult = sh(script: "git checkout ${branch} && git reset --hard target/${branch}", returnStatus: true)
191 if(resetResult != 0){
192 throw new Exception("Cannot reset even to target/${branch}, git mirroring failed!")
193 }
194 }
Sergey Kolekonovba203982016-12-21 18:32:17 +0400195
Sergey Kolekonovba203982016-12-21 18:32:17 +0400196 sh "git ls-tree target/${branch} && git merge --no-edit --ff target/${branch} || echo 'Target repository is empty, skipping merge'"
197 followTagsArg = followTags ? "--follow-tags" : ""
Filip Pytloun49d66302017-03-06 10:26:22 +0100198 ssh.agentSh "git push ${followTagsArg} target HEAD:${branch}"
199
200 if (pushSource == true) {
201 followTagsArg = followTags && pushSourceTags ? "--follow-tags" : ""
Filip Pytlounb36387e2017-03-06 11:39:19 +0100202 ssh.agentSh "git push ${followTagsArg} origin HEAD:${branch}"
Filip Pytloun49d66302017-03-06 10:26:22 +0100203 }
204 }
Filip Pytloun49d66302017-03-06 10:26:22 +0100205 if (followTags == true) {
Richard Felkl082da3c2018-06-11 10:42:16 +0200206 ssh.agentSh "git push -f target --tags"
Filip Pytloun49d66302017-03-06 10:26:22 +0100207
208 if (pushSourceTags == true) {
Richard Felkl082da3c2018-06-11 10:42:16 +0200209 ssh.agentSh "git push -f origin --tags"
Filip Pytloun49d66302017-03-06 10:26:22 +0100210 }
Sergey Kolekonovba203982016-12-21 18:32:17 +0400211 }
Jakub Josefecf8b452017-04-20 13:34:29 +0200212 sh "git remote rm target"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400213}