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: |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 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 |
Denis Egorenko | e945ec9 | 2016-12-21 19:15:10 +0400 | [diff] [blame] | 49 | * - withWipeOut, wipe repository and force clone |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 50 | * |
| 51 | * Usage example: |
| 52 | * |
| 53 | * def gitFunc = new com.mirantis.mcp.Git() |
Denis Egorenko | e945ec9 | 2016-12-21 19:15:10 +0400 | [diff] [blame] | 54 | * gitFunc.gitSSHCheckout ([ |
| 55 | * credentialsId : 'mcp-ci-gerrit' |
| 56 | * branch : 'mcp-0.1' |
| 57 | * host : 'ci.mcp-ci.local' |
| 58 | * project : 'project' |
| 59 | * ]) |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 60 | */ |
Denis Egorenko | e945ec9 | 2016-12-21 19:15:10 +0400 | [diff] [blame] | 61 | def 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 Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 66 | |
| 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 Egorenko | bb1f879 | 2016-12-21 13:13:24 +0400 | [diff] [blame] | 78 | // we need wipe workspace before checkout |
| 79 | if (wipe) { |
| 80 | scmExtensions.add([$class: 'WipeWorkspace']) |
| 81 | } |
| 82 | |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 83 | checkout( |
| 84 | scm: [ |
| 85 | $class: 'GitSCM', |
| 86 | branches: [[name: "${config.branch}"]], |
| 87 | extensions: scmExtensions, |
| 88 | userRemoteConfigs: [[ |
| 89 | credentialsId: "${config.credentialsId}", |
| 90 | name: 'origin', |
Artem Panchenko | 86e19c7 | 2017-01-31 13:09:42 +0200 | [diff] [blame^] | 91 | url: "ssh://${config.host}:${port}/${config.project}.git" |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 92 | ]] |
| 93 | ] |
| 94 | ) |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Execute git clone and checkout stage from gerrit review |
| 99 | * |
Denis Egorenko | e945ec9 | 2016-12-21 19:15:10 +0400 | [diff] [blame] | 100 | * @param config LinkedHashMap |
| 101 | * config includes next parameters: |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 102 | * - 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 Kulanov | 88054e6 | 2017-01-17 16:21:54 +0200 | [diff] [blame] | 107 | * //anonymous gerrit checkout |
| 108 | * def gitFunc = new com.mirantis.mcp.Git() |
| 109 | * gitFunc.gerritPatchsetCheckout([ |
| 110 | * withMerge : true |
| 111 | * ]) |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 112 | * |
| 113 | * def gitFunc = new com.mirantis.mcp.Git() |
Denis Egorenko | e945ec9 | 2016-12-21 19:15:10 +0400 | [diff] [blame] | 114 | * gitFunc.gerritPatchsetCheckout([ |
Sergey Kulanov | 88054e6 | 2017-01-17 16:21:54 +0200 | [diff] [blame] | 115 | * credentialsId : 'mcp-ci-gerrit', |
Denis Egorenko | e945ec9 | 2016-12-21 19:15:10 +0400 | [diff] [blame] | 116 | * withMerge : true |
| 117 | * ]) |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 118 | */ |
Denis Egorenko | e945ec9 | 2016-12-21 19:15:10 +0400 | [diff] [blame] | 119 | def gerritPatchsetCheckout(LinkedHashMap config) { |
| 120 | def merge = config.get('withMerge', false) |
| 121 | def wipe = config.get('withWipeOut', false) |
Sergey Kulanov | 88054e6 | 2017-01-17 16:21:54 +0200 | [diff] [blame] | 122 | def credentials = config.get('credentialsId','') |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 123 | |
| 124 | // default parameters |
| 125 | def scmExtensions = [ |
| 126 | [$class: 'CleanCheckout'], |
| 127 | [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']] |
| 128 | ] |
Sergey Kulanov | 88054e6 | 2017-01-17 16:21:54 +0200 | [diff] [blame] | 129 | 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 Panchenko | 86e19c7 | 2017-01-31 13:09:42 +0200 | [diff] [blame^] | 139 | scmUserRemoteConfigs.put('url',"ssh://${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git") |
Sergey Kulanov | 88054e6 | 2017-01-17 16:21:54 +0200 | [diff] [blame] | 140 | scmUserRemoteConfigs.put('credentialsId',credentials) |
| 141 | } |
| 142 | |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 143 | // 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 Kulanov | 88054e6 | 2017-01-17 16:21:54 +0200 | [diff] [blame] | 157 | userRemoteConfigs: [scmUserRemoteConfigs] |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 158 | ] |
| 159 | ) |
| 160 | } |