blob: b9b755674441dcc5fa09d551bc0d43a6f16e2201 [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:
Denis Egorenko8c606552016-12-07 14:22:50 +040043 * - credentialsId, id of user which should make checkout
44 * - branch, branch of project
45 * - host, gerrit-ci hostname
46 * - 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
Denis Egorenko8c606552016-12-07 14:22:50 +040050 *
51 * Usage example:
52 *
53 * def gitFunc = new com.mirantis.mcp.Git()
Denis Egorenkoe945ec92016-12-21 19:15:10 +040054 * gitFunc.gitSSHCheckout ([
55 * credentialsId : 'mcp-ci-gerrit'
56 * branch : 'mcp-0.1'
57 * host : 'ci.mcp-ci.local'
58 * project : 'project'
59 * ])
Denis Egorenko8c606552016-12-07 14:22:50 +040060 */
Denis Egorenkoe945ec92016-12-21 19:15:10 +040061def gitSSHCheckout(LinkedHashMap config) {
62 def merge = config.get('withMerge', false)
63 def wipe = config.get('withWipeOut', false)
64 def targetDir = config.get('targetDir', "./")
65 def port = config.get('port', "29418")
Denis Egorenko8c606552016-12-07 14:22:50 +040066
67 // default parameters
68 def scmExtensions = [
69 [$class: 'CleanCheckout'],
70 [$class: 'RelativeTargetDirectory', relativeTargetDir: "${targetDir}"]
71 ]
72
73 // https://issues.jenkins-ci.org/browse/JENKINS-6856
74 if (merge) {
75 scmExtensions.add([$class: 'LocalBranch', localBranch: "${config.branch}"])
76 }
77
Denis Egorenkobb1f8792016-12-21 13:13:24 +040078 // we need wipe workspace before checkout
79 if (wipe) {
80 scmExtensions.add([$class: 'WipeWorkspace'])
81 }
82
Denis Egorenko8c606552016-12-07 14:22:50 +040083 checkout(
84 scm: [
85 $class: 'GitSCM',
86 branches: [[name: "${config.branch}"]],
87 extensions: scmExtensions,
88 userRemoteConfigs: [[
89 credentialsId: "${config.credentialsId}",
90 name: 'origin',
Artem Panchenko86e19c72017-01-31 13:09:42 +020091 url: "ssh://${config.host}:${port}/${config.project}.git"
Denis Egorenko8c606552016-12-07 14:22:50 +040092 ]]
93 ]
94 )
95}
96
97/**
98 * Execute git clone and checkout stage from gerrit review
99 *
Denis Egorenkoe945ec92016-12-21 19:15:10 +0400100 * @param config LinkedHashMap
101 * config includes next parameters:
Denis Egorenko8c606552016-12-07 14:22:50 +0400102 * - credentialsId, id of user which should make checkout
103 * - withMerge, prevent detached mode in repo
104 * - withWipeOut, wipe repository and force clone
105 *
106 * Usage example:
Sergey Kulanov88054e62017-01-17 16:21:54 +0200107 * //anonymous gerrit checkout
108 * def gitFunc = new com.mirantis.mcp.Git()
109 * gitFunc.gerritPatchsetCheckout([
110 * withMerge : true
111 * ])
Denis Egorenko8c606552016-12-07 14:22:50 +0400112 *
113 * def gitFunc = new com.mirantis.mcp.Git()
Denis Egorenkoe945ec92016-12-21 19:15:10 +0400114 * gitFunc.gerritPatchsetCheckout([
Sergey Kulanov88054e62017-01-17 16:21:54 +0200115 * credentialsId : 'mcp-ci-gerrit',
Denis Egorenkoe945ec92016-12-21 19:15:10 +0400116 * withMerge : true
117 * ])
Denis Egorenko8c606552016-12-07 14:22:50 +0400118 */
Denis Egorenkoe945ec92016-12-21 19:15:10 +0400119def gerritPatchsetCheckout(LinkedHashMap config) {
120 def merge = config.get('withMerge', false)
121 def wipe = config.get('withWipeOut', false)
Sergey Kulanov88054e62017-01-17 16:21:54 +0200122 def credentials = config.get('credentialsId','')
Denis Egorenko8c606552016-12-07 14:22:50 +0400123
124 // default parameters
125 def scmExtensions = [
126 [$class: 'CleanCheckout'],
127 [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']]
128 ]
Sergey Kulanov88054e62017-01-17 16:21:54 +0200129 def scmUserRemoteConfigs = [
130 name: 'gerrit',
131 refspec: "${GERRIT_REFSPEC}"
132 ]
133
134 if (credentials == '') {
135 // then try to checkout in anonymous mode
136 scmUserRemoteConfigs.put('url',"https://${GERRIT_HOST}/${GERRIT_PROJECT}")
137 } else {
138 // else use ssh checkout
Artem Panchenko86e19c72017-01-31 13:09:42 +0200139 scmUserRemoteConfigs.put('url',"ssh://${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git")
Sergey Kulanov88054e62017-01-17 16:21:54 +0200140 scmUserRemoteConfigs.put('credentialsId',credentials)
141 }
142
Denis Egorenko8c606552016-12-07 14:22:50 +0400143 // if we need to "merge" code from patchset to GERRIT_BRANCH branch
144 if (merge) {
145 scmExtensions.add([$class: 'LocalBranch', localBranch: "${GERRIT_BRANCH}"])
146 }
147 // we need wipe workspace before checkout
148 if (wipe) {
149 scmExtensions.add([$class: 'WipeWorkspace'])
150 }
151
152 checkout(
153 scm: [
154 $class: 'GitSCM',
155 branches: [[name: "${GERRIT_BRANCH}"]],
156 extensions: scmExtensions,
Sergey Kulanov88054e62017-01-17 16:21:54 +0200157 userRemoteConfigs: [scmUserRemoteConfigs]
Denis Egorenko8c606552016-12-07 14:22:50 +0400158 ]
159 )
160}