Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame^] | 1 | package com.mirantis.mk |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * Git functions |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Checkout single git repository |
| 11 | * |
| 12 | * @param path Directory to checkout repository to |
| 13 | * @param url Source Git repository URL |
| 14 | * @param branch Source Git repository branch |
| 15 | * @param credentialsId Credentials ID to use for source Git |
| 16 | */ |
| 17 | def checkoutGitRepository(path, url, branch, credentialsId = null){ |
| 18 | checkout([ |
| 19 | $class: 'GitSCM', |
| 20 | branches: [[name: "*/${branch}"]], |
| 21 | doGenerateSubmoduleConfigurations: false, |
| 22 | extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: path]], |
| 23 | submoduleCfg: [], |
| 24 | userRemoteConfigs: [[url: url, credentialsId: credentialsId]] |
| 25 | ]) |
| 26 | dir(path) { |
| 27 | sh(returnStdout: true, script: 'git rev-parse HEAD').trim() |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Parse HEAD of current directory and return commit hash |
| 33 | */ |
| 34 | def getGitCommit() { |
| 35 | git_commit = sh ( |
| 36 | script: 'git rev-parse HEAD', |
| 37 | returnStdout: true |
| 38 | ).trim() |
| 39 | return git_commit |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Checkout git repositories in parallel |
| 44 | * |
| 45 | * @param path Directory to checkout to |
| 46 | * @param url Git repository url |
| 47 | * @param branch Git repository branch |
| 48 | * @param credentialsId Credentials ID to use |
| 49 | * @param poll Poll automatically |
| 50 | * @param clean Clean status |
| 51 | */ |
| 52 | def checkoutGitParallel(path, url, branch, credentialsId = null, poll = true, clean = true) { |
| 53 | return { |
| 54 | print "Checking out ${url}, branch ${branch} into ${path}" |
| 55 | dir(path) { |
| 56 | git url: url, |
| 57 | branch: branch, |
| 58 | credentialsId: credentialsId, |
| 59 | poll: poll, |
| 60 | clean: clean |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Mirror git repository |
| 67 | */ |
| 68 | def mirrorReporitory(sourceUrl, targetUrl, credentialsId, branches, followTags = false, gitEmail = 'jenkins@localhost', gitUsername = 'Jenkins') { |
| 69 | def ssl = new com.mirantis.mk.ssl() |
| 70 | if (branches instanceof String) { |
| 71 | branches = branches.tokenize(',') |
| 72 | } |
| 73 | ssl.prepareSshAgentKey(credentialsId) |
| 74 | ssl.ensureKnownHosts(targetUrl) |
| 75 | |
| 76 | sh "git remote | grep target || git remote add target ${TARGET_URL}" |
| 77 | agentSh "git remote update --prune" |
| 78 | for (i=0; i < branches.size; i++) { |
| 79 | branch = branches[i] |
| 80 | sh "git branch | grep ${branch} || git checkout -b ${branch} origin/${branch}" |
| 81 | sh "git branch | grep ${branch} && git checkout ${branch} && git reset --hard origin/${branch}" |
| 82 | |
| 83 | sh "git config --global user.email '${gitEmail}'" |
| 84 | sh "git config --global user.name '${gitUsername}'" |
| 85 | sh "git ls-tree target/${branch} && git merge --no-edit --ff target/${branch} || echo 'Target repository is empty, skipping merge'" |
| 86 | followTagsArg = followTags ? "--follow-tags" : "" |
| 87 | agentSh "git push ${followTagsArg} target HEAD:${branch}" |
| 88 | } |
| 89 | } |