blob: 92359a089cc4151a707a5010f28b9f003d72fd94 [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)
Denis Egorenko8c606552016-12-07 14:22:50 +040082
83 // default parameters
84 def scmExtensions = [
85 [$class: 'CleanCheckout'],
86 [$class: 'RelativeTargetDirectory', relativeTargetDir: "${targetDir}"]
87 ]
88
89 // https://issues.jenkins-ci.org/browse/JENKINS-6856
90 if (merge) {
91 scmExtensions.add([$class: 'LocalBranch', localBranch: "${config.branch}"])
92 }
93
Denis Egorenkobb1f8792016-12-21 13:13:24 +040094 // we need wipe workspace before checkout
95 if (wipe) {
96 scmExtensions.add([$class: 'WipeWorkspace'])
97 }
98
Denis Egorenko8c606552016-12-07 14:22:50 +040099 checkout(
100 scm: [
101 $class: 'GitSCM',
102 branches: [[name: "${config.branch}"]],
103 extensions: scmExtensions,
104 userRemoteConfigs: [[
azvyagintsev40d384e2017-02-06 15:11:05 +0200105 credentialsId: credentialsId,
106 refspec: refspec,
Denis Egorenko8c606552016-12-07 14:22:50 +0400107 name: 'origin',
azvyagintsev40d384e2017-02-06 15:11:05 +0200108 url: "${protocol}://${config.host}:${port}/${config.project}.git"
Denis Egorenko8c606552016-12-07 14:22:50 +0400109 ]]
110 ]
111 )
112}
113
azvyagintsev40d384e2017-02-06 15:11:05 +0200114def gitSSHCheckout(LinkedHashMap config) {
115 config['protocol'] = config.get('protocol', 'ssh')
116 config['port'] = config.get('port', 29418)
117 gitCheckout(config)
118}
119
120def gitHTTPCheckout(LinkedHashMap config) {
121 config['protocol'] = config.get('protocol', 'http')
122 config['port'] = config.get('port', 80)
123 gitCheckout(config)
124}
125
Denis Egorenko8c606552016-12-07 14:22:50 +0400126/**
127 * Execute git clone and checkout stage from gerrit review
128 *
Denis Egorenkoe945ec92016-12-21 19:15:10 +0400129 * @param config LinkedHashMap
130 * config includes next parameters:
Denis Egorenko8c606552016-12-07 14:22:50 +0400131 * - credentialsId, id of user which should make checkout
132 * - withMerge, prevent detached mode in repo
133 * - withWipeOut, wipe repository and force clone
134 *
135 * Usage example:
Sergey Kulanov88054e62017-01-17 16:21:54 +0200136 * //anonymous gerrit checkout
137 * def gitFunc = new com.mirantis.mcp.Git()
138 * gitFunc.gerritPatchsetCheckout([
139 * withMerge : true
140 * ])
Denis Egorenko8c606552016-12-07 14:22:50 +0400141 *
142 * def gitFunc = new com.mirantis.mcp.Git()
Denis Egorenkoe945ec92016-12-21 19:15:10 +0400143 * gitFunc.gerritPatchsetCheckout([
Sergey Kulanov88054e62017-01-17 16:21:54 +0200144 * credentialsId : 'mcp-ci-gerrit',
Denis Egorenkoe945ec92016-12-21 19:15:10 +0400145 * withMerge : true
146 * ])
Denis Egorenko8c606552016-12-07 14:22:50 +0400147 */
Denis Egorenkoe945ec92016-12-21 19:15:10 +0400148def gerritPatchsetCheckout(LinkedHashMap config) {
149 def merge = config.get('withMerge', false)
150 def wipe = config.get('withWipeOut', false)
Sergey Kulanov88054e62017-01-17 16:21:54 +0200151 def credentials = config.get('credentialsId','')
Denis Egorenko8c606552016-12-07 14:22:50 +0400152
153 // default parameters
154 def scmExtensions = [
155 [$class: 'CleanCheckout'],
156 [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']]
157 ]
Sergey Kulanov88054e62017-01-17 16:21:54 +0200158 def scmUserRemoteConfigs = [
159 name: 'gerrit',
160 refspec: "${GERRIT_REFSPEC}"
161 ]
162
163 if (credentials == '') {
164 // then try to checkout in anonymous mode
165 scmUserRemoteConfigs.put('url',"https://${GERRIT_HOST}/${GERRIT_PROJECT}")
166 } else {
167 // else use ssh checkout
Artem Panchenko86e19c72017-01-31 13:09:42 +0200168 scmUserRemoteConfigs.put('url',"ssh://${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git")
Sergey Kulanov88054e62017-01-17 16:21:54 +0200169 scmUserRemoteConfigs.put('credentialsId',credentials)
170 }
171
Denis Egorenko8c606552016-12-07 14:22:50 +0400172 // if we need to "merge" code from patchset to GERRIT_BRANCH branch
173 if (merge) {
174 scmExtensions.add([$class: 'LocalBranch', localBranch: "${GERRIT_BRANCH}"])
175 }
176 // we need wipe workspace before checkout
177 if (wipe) {
178 scmExtensions.add([$class: 'WipeWorkspace'])
179 }
180
181 checkout(
182 scm: [
183 $class: 'GitSCM',
184 branches: [[name: "${GERRIT_BRANCH}"]],
185 extensions: scmExtensions,
Sergey Kulanov88054e62017-01-17 16:21:54 +0200186 userRemoteConfigs: [scmUserRemoteConfigs]
Denis Egorenko8c606552016-12-07 14:22:50 +0400187 ]
188 )
189}