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', |
| 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 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: |
| 107 | * |
| 108 | * def gitFunc = new com.mirantis.mcp.Git() |
Denis Egorenko | e945ec9 | 2016-12-21 19:15:10 +0400 | [diff] [blame^] | 109 | * gitFunc.gerritPatchsetCheckout([ |
| 110 | * credentialsId : 'mcp-ci-gerrit' |
| 111 | * withMerge : true |
| 112 | * ]) |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 113 | */ |
Denis Egorenko | e945ec9 | 2016-12-21 19:15:10 +0400 | [diff] [blame^] | 114 | def gerritPatchsetCheckout(LinkedHashMap config) { |
| 115 | def merge = config.get('withMerge', false) |
| 116 | def wipe = config.get('withWipeOut', false) |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 117 | |
| 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 | } |