Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 1 | package com.mirantis.mcp |
| 2 | |
| 3 | /** |
| 4 | * Parse HEAD of current directory and return commit hash |
| 5 | */ |
| 6 | def 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 Kulanov | 8567272 | 2016-11-16 17:46:58 +0200 | [diff] [blame] | 16 | * |
| 17 | * @param useShort Boolean, which String format returns as result. |
| 18 | * false (Default): {gitTag}-{numCommits}-g{gitsha} |
| 19 | * true: {gitTag}-{numCommits} |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 20 | */ |
Sergey Kulanov | 8567272 | 2016-11-16 17:46:58 +0200 | [diff] [blame] | 21 | def 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 Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 34 | return git_commit |
| 35 | } |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Execute git clone+checkout stage for some project, |
| 39 | * through SSH |
| 40 | * |
Denis Egorenko | e945ec9 | 2016-12-21 19:15:10 +0400 | [diff] [blame] | 41 | * @param config LinkedHashMap |
| 42 | * config includes next parameters: |
azvyagintsev | 40d384e | 2017-02-06 15:11:05 +0200 | [diff] [blame] | 43 | * - credentialsId, id of user which should make checkout(Jenkins Credential) |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 44 | * - branch, branch of project |
azvyagintsev | 40d384e | 2017-02-06 15:11:05 +0200 | [diff] [blame] | 45 | * - host, gerrit-ci hostname(Also, could be in format username@host) |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 46 | * - project, name of project |
| 47 | * - targetDir, target directory of cloned repo |
| 48 | * - withMerge, prevent detached mode in repo |
Denis Egorenko | e945ec9 | 2016-12-21 19:15:10 +0400 | [diff] [blame] | 49 | * - withWipeOut, wipe repository and force clone |
azvyagintsev | 40d384e | 2017-02-06 15:11:05 +0200 | [diff] [blame] | 50 | * - 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 Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 54 | * |
| 55 | * Usage example: |
| 56 | * |
| 57 | * def gitFunc = new com.mirantis.mcp.Git() |
Denis Egorenko | e945ec9 | 2016-12-21 19:15:10 +0400 | [diff] [blame] | 58 | * gitFunc.gitSSHCheckout ([ |
azvyagintsev | 40d384e | 2017-02-06 15:11:05 +0200 | [diff] [blame] | 59 | * credentialsId : 'mcp-ci-gerrit', |
| 60 | * branch : 'mcp-0.1', |
| 61 | * host : 'user@ci.mcp-ci.local', |
| 62 | * project : 'project', |
Denis Egorenko | e945ec9 | 2016-12-21 19:15:10 +0400 | [diff] [blame] | 63 | * ]) |
azvyagintsev | 40d384e | 2017-02-06 15:11:05 +0200 | [diff] [blame] | 64 | * |
| 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 Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 73 | */ |
azvyagintsev | 40d384e | 2017-02-06 15:11:05 +0200 | [diff] [blame] | 74 | def gitCheckout(LinkedHashMap config) { |
Denis Egorenko | e945ec9 | 2016-12-21 19:15:10 +0400 | [diff] [blame] | 75 | 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") |
azvyagintsev | 40d384e | 2017-02-06 15:11:05 +0200 | [diff] [blame] | 79 | def credentialsId = config.get('credentialsId', '') |
| 80 | def protocol = config.get('protocol', 'ssh') |
| 81 | def refspec = config.get('refspec', null) |
Dmitry Teselkin | 70ef700 | 2020-10-19 20:03:21 +0300 | [diff] [blame] | 82 | String branch = config.get('branch', 'FETCH_HEAD') |
| 83 | Integer depth = config.get('depth', 0) |
| 84 | Integer timeout = config.get('timeout', 0) |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 85 | |
| 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 Teselkin | 70ef700 | 2020-10-19 20:03:21 +0300 | [diff] [blame] | 94 | scmExtensions.add([$class: 'LocalBranch', localBranch: "${branch}"]) |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 95 | } |
| 96 | |
Denis Egorenko | bb1f879 | 2016-12-21 13:13:24 +0400 | [diff] [blame] | 97 | // we need wipe workspace before checkout |
| 98 | if (wipe) { |
| 99 | scmExtensions.add([$class: 'WipeWorkspace']) |
| 100 | } |
| 101 | |
Dmitry Teselkin | 70ef700 | 2020-10-19 20:03:21 +0300 | [diff] [blame] | 102 | // 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 Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 112 | checkout( |
| 113 | scm: [ |
| 114 | $class: 'GitSCM', |
Dmitry Teselkin | 70ef700 | 2020-10-19 20:03:21 +0300 | [diff] [blame] | 115 | branches: [[name: "${branch}"]], |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 116 | extensions: scmExtensions, |
| 117 | userRemoteConfigs: [[ |
azvyagintsev | 40d384e | 2017-02-06 15:11:05 +0200 | [diff] [blame] | 118 | credentialsId: credentialsId, |
| 119 | refspec: refspec, |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 120 | name: 'origin', |
azvyagintsev | 40d384e | 2017-02-06 15:11:05 +0200 | [diff] [blame] | 121 | url: "${protocol}://${config.host}:${port}/${config.project}.git" |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 122 | ]] |
| 123 | ] |
| 124 | ) |
| 125 | } |
| 126 | |
azvyagintsev | 40d384e | 2017-02-06 15:11:05 +0200 | [diff] [blame] | 127 | def gitSSHCheckout(LinkedHashMap config) { |
| 128 | config['protocol'] = config.get('protocol', 'ssh') |
| 129 | config['port'] = config.get('port', 29418) |
| 130 | gitCheckout(config) |
| 131 | } |
| 132 | |
| 133 | def gitHTTPCheckout(LinkedHashMap config) { |
| 134 | config['protocol'] = config.get('protocol', 'http') |
| 135 | config['port'] = config.get('port', 80) |
| 136 | gitCheckout(config) |
| 137 | } |
| 138 | |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 139 | /** |
| 140 | * Execute git clone and checkout stage from gerrit review |
| 141 | * |
Denis Egorenko | e945ec9 | 2016-12-21 19:15:10 +0400 | [diff] [blame] | 142 | * @param config LinkedHashMap |
| 143 | * config includes next parameters: |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 144 | * - 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 Kulanov | 88054e6 | 2017-01-17 16:21:54 +0200 | [diff] [blame] | 149 | * //anonymous gerrit checkout |
| 150 | * def gitFunc = new com.mirantis.mcp.Git() |
| 151 | * gitFunc.gerritPatchsetCheckout([ |
| 152 | * withMerge : true |
| 153 | * ]) |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 154 | * |
| 155 | * def gitFunc = new com.mirantis.mcp.Git() |
Denis Egorenko | e945ec9 | 2016-12-21 19:15:10 +0400 | [diff] [blame] | 156 | * gitFunc.gerritPatchsetCheckout([ |
Sergey Kulanov | 88054e6 | 2017-01-17 16:21:54 +0200 | [diff] [blame] | 157 | * credentialsId : 'mcp-ci-gerrit', |
Denis Egorenko | e945ec9 | 2016-12-21 19:15:10 +0400 | [diff] [blame] | 158 | * withMerge : true |
| 159 | * ]) |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 160 | */ |
Denis Egorenko | e945ec9 | 2016-12-21 19:15:10 +0400 | [diff] [blame] | 161 | def gerritPatchsetCheckout(LinkedHashMap config) { |
| 162 | def merge = config.get('withMerge', false) |
| 163 | def wipe = config.get('withWipeOut', false) |
Sergey Kulanov | 88054e6 | 2017-01-17 16:21:54 +0200 | [diff] [blame] | 164 | def credentials = config.get('credentialsId','') |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 165 | |
| 166 | // default parameters |
| 167 | def scmExtensions = [ |
| 168 | [$class: 'CleanCheckout'], |
| 169 | [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']] |
| 170 | ] |
Sergey Kulanov | 88054e6 | 2017-01-17 16:21:54 +0200 | [diff] [blame] | 171 | 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 Panchenko | 86e19c7 | 2017-01-31 13:09:42 +0200 | [diff] [blame] | 181 | scmUserRemoteConfigs.put('url',"ssh://${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git") |
Sergey Kulanov | 88054e6 | 2017-01-17 16:21:54 +0200 | [diff] [blame] | 182 | scmUserRemoteConfigs.put('credentialsId',credentials) |
| 183 | } |
| 184 | |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 185 | // 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 Kulanov | 88054e6 | 2017-01-17 16:21:54 +0200 | [diff] [blame] | 199 | userRemoteConfigs: [scmUserRemoteConfigs] |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 200 | ] |
| 201 | ) |
| 202 | } |