blob: 4bf2056725b2e19a7bcb40281fb57f6e26ad45fd [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 *
41 * @param body Closure
42 * body includes next parameters:
43 * - 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
49 *
50 * Usage example:
51 *
52 * def gitFunc = new com.mirantis.mcp.Git()
53 * gitFunc.gitSSHCheckout {
54 * credentialsId = 'mcp-ci-gerrit'
55 * branch = 'mcp-0.1'
56 * host = 'ci.mcp-ci.local'
57 * project = 'project'
58 * }
59 */
60def gitSSHCheckout = { body ->
61 // evaluate the body block, and collect configuration into the object
62 def config = [:]
63 body.resolveStrategy = Closure.DELEGATE_FIRST
64 body.delegate = config
65 body()
66
67 def merge = config.withMerge ?: false
68 def targetDir = config.targetDir ?: "./"
69 def port = config.port ?: "29418"
70
71 // default parameters
72 def scmExtensions = [
73 [$class: 'CleanCheckout'],
74 [$class: 'RelativeTargetDirectory', relativeTargetDir: "${targetDir}"]
75 ]
76
77 // https://issues.jenkins-ci.org/browse/JENKINS-6856
78 if (merge) {
79 scmExtensions.add([$class: 'LocalBranch', localBranch: "${config.branch}"])
80 }
81
82 checkout(
83 scm: [
84 $class: 'GitSCM',
85 branches: [[name: "${config.branch}"]],
86 extensions: scmExtensions,
87 userRemoteConfigs: [[
88 credentialsId: "${config.credentialsId}",
89 name: 'origin',
90 url: "ssh://${config.credentialsId}@${config.host}:${port}/${config.project}.git"
91 ]]
92 ]
93 )
94}
95
96/**
97 * Execute git clone and checkout stage from gerrit review
98 *
99 * @param body Closure
100 * body includes next parameters:
101 * - credentialsId, id of user which should make checkout
102 * - withMerge, prevent detached mode in repo
103 * - withWipeOut, wipe repository and force clone
104 *
105 * Usage example:
106 *
107 * def gitFunc = new com.mirantis.mcp.Git()
108 * gitFunc.gerritPatchsetCheckout {
109 * credentialsId = 'mcp-ci-gerrit'
110 * withMerge = true
111 * }
112 */
113def gerritPatchsetCheckout = { body ->
114 // evaluate the body block, and collect configuration into the object
115 def config = [:]
116 body.resolveStrategy = Closure.DELEGATE_FIRST
117 body.delegate = config
118 body()
119
120
121 def merge = config.withMerge ?: false
122 def wipe = config.withWipeOut ?: false
123
124 // default parameters
125 def scmExtensions = [
126 [$class: 'CleanCheckout'],
127 [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']]
128 ]
129 // if we need to "merge" code from patchset to GERRIT_BRANCH branch
130 if (merge) {
131 scmExtensions.add([$class: 'LocalBranch', localBranch: "${GERRIT_BRANCH}"])
132 }
133 // we need wipe workspace before checkout
134 if (wipe) {
135 scmExtensions.add([$class: 'WipeWorkspace'])
136 }
137
138 checkout(
139 scm: [
140 $class: 'GitSCM',
141 branches: [[name: "${GERRIT_BRANCH}"]],
142 extensions: scmExtensions,
143 userRemoteConfigs: [[
144 credentialsId: "${config.credentialsId}",
145 name: 'gerrit',
146 url: "ssh://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git",
147 refspec: "${GERRIT_REFSPEC}"
148 ]]
149 ]
150 )
151}