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