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) |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 82 | |
| 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 Egorenko | bb1f879 | 2016-12-21 13:13:24 +0400 | [diff] [blame] | 94 | // we need wipe workspace before checkout |
| 95 | if (wipe) { |
| 96 | scmExtensions.add([$class: 'WipeWorkspace']) |
| 97 | } |
| 98 | |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 99 | checkout( |
| 100 | scm: [ |
| 101 | $class: 'GitSCM', |
| 102 | branches: [[name: "${config.branch}"]], |
| 103 | extensions: scmExtensions, |
| 104 | userRemoteConfigs: [[ |
azvyagintsev | 40d384e | 2017-02-06 15:11:05 +0200 | [diff] [blame] | 105 | credentialsId: credentialsId, |
| 106 | refspec: refspec, |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 107 | name: 'origin', |
azvyagintsev | 40d384e | 2017-02-06 15:11:05 +0200 | [diff] [blame] | 108 | url: "${protocol}://${config.host}:${port}/${config.project}.git" |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 109 | ]] |
| 110 | ] |
| 111 | ) |
| 112 | } |
| 113 | |
azvyagintsev | 40d384e | 2017-02-06 15:11:05 +0200 | [diff] [blame] | 114 | def gitSSHCheckout(LinkedHashMap config) { |
| 115 | config['protocol'] = config.get('protocol', 'ssh') |
| 116 | config['port'] = config.get('port', 29418) |
| 117 | gitCheckout(config) |
| 118 | } |
| 119 | |
| 120 | def gitHTTPCheckout(LinkedHashMap config) { |
| 121 | config['protocol'] = config.get('protocol', 'http') |
| 122 | config['port'] = config.get('port', 80) |
| 123 | gitCheckout(config) |
| 124 | } |
| 125 | |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 126 | /** |
| 127 | * Execute git clone and checkout stage from gerrit review |
| 128 | * |
Denis Egorenko | e945ec9 | 2016-12-21 19:15:10 +0400 | [diff] [blame] | 129 | * @param config LinkedHashMap |
| 130 | * config includes next parameters: |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 131 | * - 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 Kulanov | 88054e6 | 2017-01-17 16:21:54 +0200 | [diff] [blame] | 136 | * //anonymous gerrit checkout |
| 137 | * def gitFunc = new com.mirantis.mcp.Git() |
| 138 | * gitFunc.gerritPatchsetCheckout([ |
| 139 | * withMerge : true |
| 140 | * ]) |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 141 | * |
| 142 | * def gitFunc = new com.mirantis.mcp.Git() |
Denis Egorenko | e945ec9 | 2016-12-21 19:15:10 +0400 | [diff] [blame] | 143 | * gitFunc.gerritPatchsetCheckout([ |
Sergey Kulanov | 88054e6 | 2017-01-17 16:21:54 +0200 | [diff] [blame] | 144 | * credentialsId : 'mcp-ci-gerrit', |
Denis Egorenko | e945ec9 | 2016-12-21 19:15:10 +0400 | [diff] [blame] | 145 | * withMerge : true |
| 146 | * ]) |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 147 | */ |
Denis Egorenko | e945ec9 | 2016-12-21 19:15:10 +0400 | [diff] [blame] | 148 | def gerritPatchsetCheckout(LinkedHashMap config) { |
| 149 | def merge = config.get('withMerge', false) |
| 150 | def wipe = config.get('withWipeOut', false) |
Sergey Kulanov | 88054e6 | 2017-01-17 16:21:54 +0200 | [diff] [blame] | 151 | def credentials = config.get('credentialsId','') |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 152 | |
| 153 | // default parameters |
| 154 | def scmExtensions = [ |
| 155 | [$class: 'CleanCheckout'], |
| 156 | [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']] |
| 157 | ] |
Sergey Kulanov | 88054e6 | 2017-01-17 16:21:54 +0200 | [diff] [blame] | 158 | 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 Panchenko | 86e19c7 | 2017-01-31 13:09:42 +0200 | [diff] [blame] | 168 | scmUserRemoteConfigs.put('url',"ssh://${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git") |
Sergey Kulanov | 88054e6 | 2017-01-17 16:21:54 +0200 | [diff] [blame] | 169 | scmUserRemoteConfigs.put('credentialsId',credentials) |
| 170 | } |
| 171 | |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 172 | // 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 Kulanov | 88054e6 | 2017-01-17 16:21:54 +0200 | [diff] [blame] | 186 | userRemoteConfigs: [scmUserRemoteConfigs] |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 187 | ] |
| 188 | ) |
| 189 | } |