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 |
Jakub Josef | 7dccebe | 2017-03-06 18:08:32 +0100 | [diff] [blame] | 16 | * @param poll Enable git polling (default true) |
| 17 | * @param timeout Set checkout timeout (default 10) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 18 | */ |
Jakub Josef | 7dccebe | 2017-03-06 18:08:32 +0100 | [diff] [blame] | 19 | def checkoutGitRepository(path, url, branch, credentialsId = null, poll = true, timeout = 10){ |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 20 | dir(path) { |
Jakub Josef | 6fa8cb1 | 2017-03-06 18:20:08 +0100 | [diff] [blame^] | 21 | checkout( |
| 22 | changelog:true, |
| 23 | poll: poll, |
| 24 | scm: [ |
| 25 | $class: 'GitSCM', |
| 26 | branches: [[name: "*/${branch}"]], |
| 27 | doGenerateSubmoduleConfigurations: false, |
| 28 | extensions: [ |
| 29 | [$class: 'RelativeTargetDirectory', relativeTargetDir: path], |
| 30 | [$class: 'CloneOption', depth: 0, noTags: false, reference: '', shallow: false, timeout: timeout]], |
| 31 | submoduleCfg: [], |
| 32 | userRemoteConfigs: [[url: url, credentialsId: credentialsId]]] |
| 33 | ) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 34 | sh(returnStdout: true, script: 'git rev-parse HEAD').trim() |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Parse HEAD of current directory and return commit hash |
| 40 | */ |
| 41 | def getGitCommit() { |
| 42 | git_commit = sh ( |
| 43 | script: 'git rev-parse HEAD', |
| 44 | returnStdout: true |
| 45 | ).trim() |
| 46 | return git_commit |
| 47 | } |
| 48 | |
| 49 | /** |
Ales Komarek | fb7cbcb | 2017-02-24 14:02:03 +0100 | [diff] [blame] | 50 | * Change actual working branch of repo |
| 51 | * |
| 52 | * @param path Path to the git repository |
| 53 | * @param branch Branch desired to switch to |
| 54 | */ |
| 55 | def changeGitBranch(path, branch) { |
| 56 | dir(path) { |
| 57 | git_cmd = sh ( |
| 58 | script: "git checkout -b ${branch}", |
| 59 | returnStdout: true |
| 60 | ).trim() |
| 61 | } |
| 62 | return git_cmd |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Commit changes to the git repo |
| 67 | * |
| 68 | * @param path Path to the git repository |
| 69 | * @param message A commit message |
| 70 | */ |
| 71 | def commitGitChanges(path, message) { |
| 72 | dir(path) { |
| 73 | sh( |
| 74 | script: 'git add -A', |
| 75 | returnStdout: true |
| 76 | ).trim() |
| 77 | git_cmd = sh( |
| 78 | script: "git commit -m '${message}'", |
| 79 | returnStdout: true |
| 80 | ).trim() |
| 81 | } |
| 82 | return git_cmd |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /** |
| 87 | * Push git changes to remote repo |
| 88 | * |
| 89 | * @param path Path to the git repository |
| 90 | * @param branch Branch on the remote git repository |
| 91 | * @param remote Name of the remote repository |
| 92 | */ |
| 93 | def pushGitChanges(path, branch = 'master', remote = 'origin') { |
| 94 | dir(path) { |
| 95 | git_cmd = sh( |
| 96 | script: "git push ${remote} ${branch}", |
| 97 | returnStdout: true |
| 98 | ).trim() |
| 99 | } |
| 100 | return git_cmd |
| 101 | } |
| 102 | |
| 103 | |
| 104 | /** |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 105 | * Checkout git repositories in parallel |
| 106 | * |
| 107 | * @param path Directory to checkout to |
| 108 | * @param url Git repository url |
| 109 | * @param branch Git repository branch |
| 110 | * @param credentialsId Credentials ID to use |
| 111 | * @param poll Poll automatically |
| 112 | * @param clean Clean status |
| 113 | */ |
| 114 | def checkoutGitParallel(path, url, branch, credentialsId = null, poll = true, clean = true) { |
| 115 | return { |
| 116 | print "Checking out ${url}, branch ${branch} into ${path}" |
| 117 | dir(path) { |
| 118 | git url: url, |
| 119 | branch: branch, |
| 120 | credentialsId: credentialsId, |
| 121 | poll: poll, |
| 122 | clean: clean |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Mirror git repository |
| 129 | */ |
| 130 | def mirrorReporitory(sourceUrl, targetUrl, credentialsId, branches, followTags = false, gitEmail = 'jenkins@localhost', gitUsername = 'Jenkins') { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 131 | def ssl = new com.mirantis.mk.Ssl() |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 132 | if (branches instanceof String) { |
| 133 | branches = branches.tokenize(',') |
| 134 | } |
| 135 | ssl.prepareSshAgentKey(credentialsId) |
| 136 | ssl.ensureKnownHosts(targetUrl) |
| 137 | |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 138 | sh "git remote | grep target || git remote add target ${targetUrl}" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 139 | agentSh "git remote update --prune" |
| 140 | for (i=0; i < branches.size; i++) { |
| 141 | branch = branches[i] |
| 142 | sh "git branch | grep ${branch} || git checkout -b ${branch} origin/${branch}" |
| 143 | sh "git branch | grep ${branch} && git checkout ${branch} && git reset --hard origin/${branch}" |
| 144 | |
| 145 | sh "git config --global user.email '${gitEmail}'" |
| 146 | sh "git config --global user.name '${gitUsername}'" |
| 147 | sh "git ls-tree target/${branch} && git merge --no-edit --ff target/${branch} || echo 'Target repository is empty, skipping merge'" |
| 148 | followTagsArg = followTags ? "--follow-tags" : "" |
| 149 | agentSh "git push ${followTagsArg} target HEAD:${branch}" |
| 150 | } |
| 151 | } |