blob: 22ca53d9a62f79171917afdacb0839c072e21d21 [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/**
Filip Pytloun49d66302017-03-06 10:26:22 +010051 * Get remote URL
52 *
Filip Pytloun3e4a1c92017-03-06 11:36:51 +010053 * @param name Name of remote (default any)
Filip Pytloun49d66302017-03-06 10:26:22 +010054 * @param type Type (fetch or push, default fetch)
55 */
Filip Pytloun3e4a1c92017-03-06 11:36:51 +010056def getGitRemote(name = '', type = 'fetch') {
Filip Pytloun49d66302017-03-06 10:26:22 +010057 gitRemote = sh (
Filip Pytloun3e4a1c92017-03-06 11:36:51 +010058 script: "git remote -v | grep '${name}' | grep ${type} | awk '{print \$2}' | head -1",
Filip Pytloun49d66302017-03-06 10:26:22 +010059 returnStdout: true
60 ).trim()
61 return gitRemote
62}
63
64/**
Ales Komarekfb7cbcb2017-02-24 14:02:03 +010065 * Change actual working branch of repo
66 *
67 * @param path Path to the git repository
68 * @param branch Branch desired to switch to
69 */
70def changeGitBranch(path, branch) {
71 dir(path) {
72 git_cmd = sh (
73 script: "git checkout -b ${branch}",
74 returnStdout: true
75 ).trim()
76 }
77 return git_cmd
78}
79
80/**
81 * Commit changes to the git repo
82 *
83 * @param path Path to the git repository
84 * @param message A commit message
85 */
86def commitGitChanges(path, message) {
87 dir(path) {
88 sh(
89 script: 'git add -A',
90 returnStdout: true
91 ).trim()
92 git_cmd = sh(
93 script: "git commit -m '${message}'",
94 returnStdout: true
95 ).trim()
96 }
97 return git_cmd
98}
99
100
101/**
102 * Push git changes to remote repo
103 *
104 * @param path Path to the git repository
105 * @param branch Branch on the remote git repository
106 * @param remote Name of the remote repository
107 */
108def pushGitChanges(path, branch = 'master', remote = 'origin') {
109 dir(path) {
110 git_cmd = sh(
111 script: "git push ${remote} ${branch}",
112 returnStdout: true
113 ).trim()
114 }
115 return git_cmd
116}
117
Sergey Kolekonovba203982016-12-21 18:32:17 +0400118/**
Filip Pytloun49d66302017-03-06 10:26:22 +0100119 * Mirror git repository, merge target changes (downstream) on top of source
120 * (upstream) and push target or both if pushSource is true
121 *
122 * @param sourceUrl Source git repository
123 * @param targetUrl Target git repository
124 * @param credentialsId Credentials id to use for accessing source/target
125 * repositories
126 * @param branches List or comma-separated string of branches to sync
127 * @param followTags Mirror tags
128 * @param pushSource Push back into source branch, resulting in 2-way sync
129 * @param pushSourceTags Push target tags into source or skip pushing tags
130 * @param gitEmail Email for creation of merge commits
131 * @param gitName Name for creation of merge commits
Sergey Kolekonovba203982016-12-21 18:32:17 +0400132 */
Filip Pytloun49d66302017-03-06 10:26:22 +0100133def mirrorGit(sourceUrl, targetUrl, credentialsId, branches, followTags = false, pushSource = false, pushSourceTags = false, gitEmail = 'jenkins@localhost', gitName = 'Jenkins') {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400134 if (branches instanceof String) {
135 branches = branches.tokenize(',')
136 }
Sergey Kolekonovba203982016-12-21 18:32:17 +0400137
Filip Pytloun49d66302017-03-06 10:26:22 +0100138 def ssh = new com.mirantis.mk.Ssh()
139 ssh.prepareSshAgentKey(credentialsId)
140 ssh.ensureKnownHosts(targetUrl)
141 sh "git config user.email '${gitEmail}'"
142 sh "git config user.name '${gitName}'"
143
144 sh "git remote | grep target || git remote add target ${TARGET_URL}"
145 ssh.agentSh "git remote update --prune"
146
Sergey Kolekonovba203982016-12-21 18:32:17 +0400147 for (i=0; i < branches.size; i++) {
148 branch = branches[i]
149 sh "git branch | grep ${branch} || git checkout -b ${branch} origin/${branch}"
150 sh "git branch | grep ${branch} && git checkout ${branch} && git reset --hard origin/${branch}"
151
Sergey Kolekonovba203982016-12-21 18:32:17 +0400152 sh "git ls-tree target/${branch} && git merge --no-edit --ff target/${branch} || echo 'Target repository is empty, skipping merge'"
153 followTagsArg = followTags ? "--follow-tags" : ""
Filip Pytloun49d66302017-03-06 10:26:22 +0100154 ssh.agentSh "git push ${followTagsArg} target HEAD:${branch}"
155
156 if (pushSource == true) {
157 followTagsArg = followTags && pushSourceTags ? "--follow-tags" : ""
Filip Pytlounb36387e2017-03-06 11:39:19 +0100158 ssh.agentSh "git push ${followTagsArg} origin HEAD:${branch}"
Filip Pytloun49d66302017-03-06 10:26:22 +0100159 }
160 }
161
162 if (followTags == true) {
163 ssh.agentSh "git push target --tags"
164
165 if (pushSourceTags == true) {
166 ssh.agentSh "git push origin --tags"
167 }
Sergey Kolekonovba203982016-12-21 18:32:17 +0400168 }
169}