blob: 423101694065e34e928beda01728e5ecdaff1197 [file] [log] [blame]
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +03001package com.mirantis.mcp
2
3/**
4 * Parse HEAD of current directory and return commit hash
5 */
6def getGitCommit() {
7 git_commit = sh(
8 script: 'git rev-parse HEAD',
9 returnStdout: true
10 ).trim()
11 return git_commit
12}
13
14/**
15 * Describe a commit using the most recent tag reachable from it
Sergey Kulanov85672722016-11-16 17:46:58 +020016 *
17 * @param useShort Boolean, which String format returns as result.
18 * false (Default): {gitTag}-{numCommits}-g{gitsha}
19 * true: {gitTag}-{numCommits}
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +030020 */
Sergey Kulanov85672722016-11-16 17:46:58 +020021def getGitDescribe(Boolean useShort = false) {
22 if (useShort) {
23 // original sed "s/-g[0-9a-f]\+$//g" should be escaped in groovy
24 git_commit = sh (
25 script: 'git describe --tags | sed "s/-g[0-9a-f]\\+$//g"',
26 returnStdout: true
27 ).trim()
28 } else {
29 git_commit = sh (
30 script: 'git describe --tags',
31 returnStdout: true
32 ).trim()
33 }
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +030034 return git_commit
35}
Denis Egorenko8c606552016-12-07 14:22:50 +040036
37/**
38 * Execute git clone+checkout stage for some project,
39 * through SSH
40 *
Denis Egorenkoe945ec92016-12-21 19:15:10 +040041 * @param config LinkedHashMap
42 * config includes next parameters:
azvyagintsev40d384e2017-02-06 15:11:05 +020043 * - credentialsId, id of user which should make checkout(Jenkins Credential)
Denis Egorenko8c606552016-12-07 14:22:50 +040044 * - branch, branch of project
azvyagintsev40d384e2017-02-06 15:11:05 +020045 * - host, gerrit-ci hostname(Also, could be in format username@host)
Denis Egorenko8c606552016-12-07 14:22:50 +040046 * - project, name of project
47 * - targetDir, target directory of cloned repo
48 * - withMerge, prevent detached mode in repo
Denis Egorenkoe945ec92016-12-21 19:15:10 +040049 * - withWipeOut, wipe repository and force clone
azvyagintsev40d384e2017-02-06 15:11:05 +020050 * - protocol, protocol for git connection(http\https\ssh\file\etc)
51 * - refspec, A refspec controls the remote refs to be retrieved and how they map to local refs.
52 * If left blank, it will default to the normal behaviour of git fetch, which retrieves all the branch heads
53 * as remotes/REPOSITORYNAME/BRANCHNAME. This default behaviour is OK for most cases.
Denis Egorenko8c606552016-12-07 14:22:50 +040054 *
55 * Usage example:
56 *
57 * def gitFunc = new com.mirantis.mcp.Git()
Denis Egorenkoe945ec92016-12-21 19:15:10 +040058 * gitFunc.gitSSHCheckout ([
azvyagintsev40d384e2017-02-06 15:11:05 +020059 * credentialsId : 'mcp-ci-gerrit',
60 * branch : 'mcp-0.1',
61 * host : 'user@ci.mcp-ci.local',
62 * project : 'project',
Denis Egorenkoe945ec92016-12-21 19:15:10 +040063 * ])
azvyagintsev40d384e2017-02-06 15:11:05 +020064 *
65 * Example for Anon http:
66 * def gitFunc = new com.mirantis.mcp.Git()
67 * gitFunc.gitHTTPCheckout ([
68 * branch : 'master',
69 * host : 'ci.mcp-ci.local',
70 * project : 'project',
71 * ])
72 *
Denis Egorenko8c606552016-12-07 14:22:50 +040073 */
azvyagintsev40d384e2017-02-06 15:11:05 +020074def gitCheckout(LinkedHashMap config) {
Denis Egorenkoe945ec92016-12-21 19:15:10 +040075 def merge = config.get('withMerge', false)
76 def wipe = config.get('withWipeOut', false)
77 def targetDir = config.get('targetDir', "./")
78 def port = config.get('port', "29418")
azvyagintsev40d384e2017-02-06 15:11:05 +020079 def credentialsId = config.get('credentialsId', '')
80 def protocol = config.get('protocol', 'ssh')
81 def refspec = config.get('refspec', null)
Dmitry Teselkin70ef7002020-10-19 20:03:21 +030082 String branch = config.get('branch', 'FETCH_HEAD')
83 Integer depth = config.get('depth', 0)
84 Integer timeout = config.get('timeout', 0)
Denis Egorenko8c606552016-12-07 14:22:50 +040085
86 // default parameters
87 def scmExtensions = [
88 [$class: 'CleanCheckout'],
89 [$class: 'RelativeTargetDirectory', relativeTargetDir: "${targetDir}"]
90 ]
91
92 // https://issues.jenkins-ci.org/browse/JENKINS-6856
93 if (merge) {
Dmitry Teselkin70ef7002020-10-19 20:03:21 +030094 scmExtensions.add([$class: 'LocalBranch', localBranch: "${branch}"])
Denis Egorenko8c606552016-12-07 14:22:50 +040095 }
96
Denis Egorenkobb1f8792016-12-21 13:13:24 +040097 // we need wipe workspace before checkout
98 if (wipe) {
99 scmExtensions.add([$class: 'WipeWorkspace'])
100 }
101
Dmitry Teselkin70ef7002020-10-19 20:03:21 +0300102 // optionally limit depth of checkout
103 if (depth) {
104 scmExtensions.add([$class: 'CloneOption', depth: "${depth}", shallow: 'true'])
105 }
106
107 // optionally set timeout
108 if (timeout) {
109 scmExtensions.add([$class: 'CloneOption', timeout: "${timeout}"])
110 }
111
Denis Egorenko8c606552016-12-07 14:22:50 +0400112 checkout(
113 scm: [
114 $class: 'GitSCM',
Dmitry Teselkin70ef7002020-10-19 20:03:21 +0300115 branches: [[name: "${branch}"]],
Denis Egorenko8c606552016-12-07 14:22:50 +0400116 extensions: scmExtensions,
117 userRemoteConfigs: [[
azvyagintsev40d384e2017-02-06 15:11:05 +0200118 credentialsId: credentialsId,
119 refspec: refspec,
Denis Egorenko8c606552016-12-07 14:22:50 +0400120 name: 'origin',
azvyagintsev40d384e2017-02-06 15:11:05 +0200121 url: "${protocol}://${config.host}:${port}/${config.project}.git"
Denis Egorenko8c606552016-12-07 14:22:50 +0400122 ]]
123 ]
124 )
125}
126
azvyagintsev40d384e2017-02-06 15:11:05 +0200127def gitSSHCheckout(LinkedHashMap config) {
128 config['protocol'] = config.get('protocol', 'ssh')
129 config['port'] = config.get('port', 29418)
130 gitCheckout(config)
131}
132
133def gitHTTPCheckout(LinkedHashMap config) {
134 config['protocol'] = config.get('protocol', 'http')
135 config['port'] = config.get('port', 80)
136 gitCheckout(config)
137}
138
Denis Egorenko8c606552016-12-07 14:22:50 +0400139/**
140 * Execute git clone and checkout stage from gerrit review
141 *
Denis Egorenkoe945ec92016-12-21 19:15:10 +0400142 * @param config LinkedHashMap
143 * config includes next parameters:
Denis Egorenko8c606552016-12-07 14:22:50 +0400144 * - credentialsId, id of user which should make checkout
145 * - withMerge, prevent detached mode in repo
146 * - withWipeOut, wipe repository and force clone
147 *
148 * Usage example:
Sergey Kulanov88054e62017-01-17 16:21:54 +0200149 * //anonymous gerrit checkout
150 * def gitFunc = new com.mirantis.mcp.Git()
151 * gitFunc.gerritPatchsetCheckout([
152 * withMerge : true
153 * ])
Denis Egorenko8c606552016-12-07 14:22:50 +0400154 *
155 * def gitFunc = new com.mirantis.mcp.Git()
Denis Egorenkoe945ec92016-12-21 19:15:10 +0400156 * gitFunc.gerritPatchsetCheckout([
Sergey Kulanov88054e62017-01-17 16:21:54 +0200157 * credentialsId : 'mcp-ci-gerrit',
Denis Egorenkoe945ec92016-12-21 19:15:10 +0400158 * withMerge : true
159 * ])
Denis Egorenko8c606552016-12-07 14:22:50 +0400160 */
Denis Egorenkoe945ec92016-12-21 19:15:10 +0400161def gerritPatchsetCheckout(LinkedHashMap config) {
162 def merge = config.get('withMerge', false)
163 def wipe = config.get('withWipeOut', false)
Sergey Kulanov88054e62017-01-17 16:21:54 +0200164 def credentials = config.get('credentialsId','')
Denis Egorenko8c606552016-12-07 14:22:50 +0400165
166 // default parameters
167 def scmExtensions = [
168 [$class: 'CleanCheckout'],
169 [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']]
170 ]
Sergey Kulanov88054e62017-01-17 16:21:54 +0200171 def scmUserRemoteConfigs = [
172 name: 'gerrit',
173 refspec: "${GERRIT_REFSPEC}"
174 ]
175
176 if (credentials == '') {
177 // then try to checkout in anonymous mode
178 scmUserRemoteConfigs.put('url',"https://${GERRIT_HOST}/${GERRIT_PROJECT}")
179 } else {
180 // else use ssh checkout
Artem Panchenko86e19c72017-01-31 13:09:42 +0200181 scmUserRemoteConfigs.put('url',"ssh://${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git")
Sergey Kulanov88054e62017-01-17 16:21:54 +0200182 scmUserRemoteConfigs.put('credentialsId',credentials)
183 }
184
Denis Egorenko8c606552016-12-07 14:22:50 +0400185 // if we need to "merge" code from patchset to GERRIT_BRANCH branch
186 if (merge) {
187 scmExtensions.add([$class: 'LocalBranch', localBranch: "${GERRIT_BRANCH}"])
188 }
189 // we need wipe workspace before checkout
190 if (wipe) {
191 scmExtensions.add([$class: 'WipeWorkspace'])
192 }
193
194 checkout(
195 scm: [
196 $class: 'GitSCM',
197 branches: [[name: "${GERRIT_BRANCH}"]],
198 extensions: scmExtensions,
Sergey Kulanov88054e62017-01-17 16:21:54 +0200199 userRemoteConfigs: [scmUserRemoteConfigs]
Denis Egorenko8c606552016-12-07 14:22:50 +0400200 ]
201 )
202}