blob: 0e861facafcf22ed2294f4bdfeba4f9d493b6c97 [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',
91 url: "ssh://${config.credentialsId}@${config.host}:${port}/${config.project}.git"
92 ]]
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:
107 *
108 * def gitFunc = new com.mirantis.mcp.Git()
Denis Egorenkoe945ec92016-12-21 19:15:10 +0400109 * gitFunc.gerritPatchsetCheckout([
110 * credentialsId : 'mcp-ci-gerrit'
111 * withMerge : true
112 * ])
Denis Egorenko8c606552016-12-07 14:22:50 +0400113 */
Denis Egorenkoe945ec92016-12-21 19:15:10 +0400114def gerritPatchsetCheckout(LinkedHashMap config) {
115 def merge = config.get('withMerge', false)
116 def wipe = config.get('withWipeOut', false)
Denis Egorenko8c606552016-12-07 14:22:50 +0400117
118 // default parameters
119 def scmExtensions = [
120 [$class: 'CleanCheckout'],
121 [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']]
122 ]
123 // if we need to "merge" code from patchset to GERRIT_BRANCH branch
124 if (merge) {
125 scmExtensions.add([$class: 'LocalBranch', localBranch: "${GERRIT_BRANCH}"])
126 }
127 // we need wipe workspace before checkout
128 if (wipe) {
129 scmExtensions.add([$class: 'WipeWorkspace'])
130 }
131
132 checkout(
133 scm: [
134 $class: 'GitSCM',
135 branches: [[name: "${GERRIT_BRANCH}"]],
136 extensions: scmExtensions,
137 userRemoteConfigs: [[
138 credentialsId: "${config.credentialsId}",
139 name: 'gerrit',
140 url: "ssh://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git",
141 refspec: "${GERRIT_REFSPEC}"
142 ]]
143 ]
144 )
145}