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) |
Jakub Josef | 61f29e6 | 2017-03-08 16:42:06 +0100 | [diff] [blame] | 18 | * @param depth Git depth param (default 0 means no depth) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 19 | */ |
Jakub Josef | 61f29e6 | 2017-03-08 16:42:06 +0100 | [diff] [blame] | 20 | def checkoutGitRepository(path, url, branch, credentialsId = null, poll = true, timeout = 10, depth = 0){ |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 21 | dir(path) { |
Jakub Josef | 6fa8cb1 | 2017-03-06 18:20:08 +0100 | [diff] [blame] | 22 | checkout( |
| 23 | changelog:true, |
| 24 | poll: poll, |
| 25 | scm: [ |
| 26 | $class: 'GitSCM', |
| 27 | branches: [[name: "*/${branch}"]], |
| 28 | doGenerateSubmoduleConfigurations: false, |
| 29 | extensions: [ |
Jakub Josef | 61f29e6 | 2017-03-08 16:42:06 +0100 | [diff] [blame] | 30 | [$class: 'CheckoutOption', timeout: timeout], |
Jakub Josef | 1589f9a | 2017-03-08 17:41:21 +0100 | [diff] [blame] | 31 | [$class: 'CloneOption', depth: depth, noTags: false, reference: '', shallow: depth > 0, timeout: timeout]], |
Jakub Josef | 6fa8cb1 | 2017-03-06 18:20:08 +0100 | [diff] [blame] | 32 | submoduleCfg: [], |
| 33 | userRemoteConfigs: [[url: url, credentialsId: credentialsId]]] |
| 34 | ) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 35 | sh(returnStdout: true, script: 'git rev-parse HEAD').trim() |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Parse HEAD of current directory and return commit hash |
| 41 | */ |
| 42 | def getGitCommit() { |
| 43 | git_commit = sh ( |
| 44 | script: 'git rev-parse HEAD', |
| 45 | returnStdout: true |
| 46 | ).trim() |
| 47 | return git_commit |
| 48 | } |
| 49 | |
| 50 | /** |
Filip Pytloun | 49d6630 | 2017-03-06 10:26:22 +0100 | [diff] [blame] | 51 | * Get remote URL |
| 52 | * |
Filip Pytloun | 3e4a1c9 | 2017-03-06 11:36:51 +0100 | [diff] [blame] | 53 | * @param name Name of remote (default any) |
Filip Pytloun | 49d6630 | 2017-03-06 10:26:22 +0100 | [diff] [blame] | 54 | * @param type Type (fetch or push, default fetch) |
| 55 | */ |
Filip Pytloun | 3e4a1c9 | 2017-03-06 11:36:51 +0100 | [diff] [blame] | 56 | def getGitRemote(name = '', type = 'fetch') { |
Filip Pytloun | 49d6630 | 2017-03-06 10:26:22 +0100 | [diff] [blame] | 57 | gitRemote = sh ( |
Filip Pytloun | 3e4a1c9 | 2017-03-06 11:36:51 +0100 | [diff] [blame] | 58 | script: "git remote -v | grep '${name}' | grep ${type} | awk '{print \$2}' | head -1", |
Filip Pytloun | 49d6630 | 2017-03-06 10:26:22 +0100 | [diff] [blame] | 59 | returnStdout: true |
| 60 | ).trim() |
| 61 | return gitRemote |
| 62 | } |
| 63 | |
| 64 | /** |
Ales Komarek | fb7cbcb | 2017-02-24 14:02:03 +0100 | [diff] [blame] | 65 | * Change actual working branch of repo |
| 66 | * |
| 67 | * @param path Path to the git repository |
| 68 | * @param branch Branch desired to switch to |
| 69 | */ |
| 70 | def changeGitBranch(path, branch) { |
| 71 | dir(path) { |
| 72 | git_cmd = sh ( |
| 73 | script: "git checkout -b ${branch}", |
| 74 | returnStdout: true |
| 75 | ).trim() |
| 76 | } |
| 77 | return git_cmd |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Commit changes to the git repo |
| 82 | * |
| 83 | * @param path Path to the git repository |
| 84 | * @param message A commit message |
| 85 | */ |
| 86 | def commitGitChanges(path, message) { |
| 87 | dir(path) { |
| 88 | sh( |
| 89 | script: 'git add -A', |
| 90 | returnStdout: true |
| 91 | ).trim() |
| 92 | git_cmd = sh( |
| 93 | script: "git commit -m '${message}'", |
| 94 | returnStdout: true |
| 95 | ).trim() |
| 96 | } |
| 97 | return git_cmd |
| 98 | } |
| 99 | |
| 100 | |
| 101 | /** |
| 102 | * Push git changes to remote repo |
| 103 | * |
| 104 | * @param path Path to the git repository |
| 105 | * @param branch Branch on the remote git repository |
| 106 | * @param remote Name of the remote repository |
| 107 | */ |
| 108 | def pushGitChanges(path, branch = 'master', remote = 'origin') { |
| 109 | dir(path) { |
| 110 | git_cmd = sh( |
| 111 | script: "git push ${remote} ${branch}", |
| 112 | returnStdout: true |
| 113 | ).trim() |
| 114 | } |
| 115 | return git_cmd |
| 116 | } |
| 117 | |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 118 | /** |
Filip Pytloun | 49d6630 | 2017-03-06 10:26:22 +0100 | [diff] [blame] | 119 | * Mirror git repository, merge target changes (downstream) on top of source |
| 120 | * (upstream) and push target or both if pushSource is true |
| 121 | * |
| 122 | * @param sourceUrl Source git repository |
| 123 | * @param targetUrl Target git repository |
| 124 | * @param credentialsId Credentials id to use for accessing source/target |
| 125 | * repositories |
| 126 | * @param branches List or comma-separated string of branches to sync |
| 127 | * @param followTags Mirror tags |
| 128 | * @param pushSource Push back into source branch, resulting in 2-way sync |
| 129 | * @param pushSourceTags Push target tags into source or skip pushing tags |
| 130 | * @param gitEmail Email for creation of merge commits |
| 131 | * @param gitName Name for creation of merge commits |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 132 | */ |
Filip Pytloun | 49d6630 | 2017-03-06 10:26:22 +0100 | [diff] [blame] | 133 | def mirrorGit(sourceUrl, targetUrl, credentialsId, branches, followTags = false, pushSource = false, pushSourceTags = false, gitEmail = 'jenkins@localhost', gitName = 'Jenkins') { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 134 | if (branches instanceof String) { |
| 135 | branches = branches.tokenize(',') |
| 136 | } |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 137 | |
Filip Pytloun | 49d6630 | 2017-03-06 10:26:22 +0100 | [diff] [blame] | 138 | def ssh = new com.mirantis.mk.Ssh() |
| 139 | ssh.prepareSshAgentKey(credentialsId) |
| 140 | ssh.ensureKnownHosts(targetUrl) |
| 141 | sh "git config user.email '${gitEmail}'" |
| 142 | sh "git config user.name '${gitName}'" |
| 143 | |
| 144 | sh "git remote | grep target || git remote add target ${TARGET_URL}" |
| 145 | ssh.agentSh "git remote update --prune" |
| 146 | |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 147 | for (i=0; i < branches.size; i++) { |
| 148 | branch = branches[i] |
| 149 | sh "git branch | grep ${branch} || git checkout -b ${branch} origin/${branch}" |
| 150 | sh "git branch | grep ${branch} && git checkout ${branch} && git reset --hard origin/${branch}" |
| 151 | |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 152 | sh "git ls-tree target/${branch} && git merge --no-edit --ff target/${branch} || echo 'Target repository is empty, skipping merge'" |
| 153 | followTagsArg = followTags ? "--follow-tags" : "" |
Filip Pytloun | 49d6630 | 2017-03-06 10:26:22 +0100 | [diff] [blame] | 154 | ssh.agentSh "git push ${followTagsArg} target HEAD:${branch}" |
| 155 | |
| 156 | if (pushSource == true) { |
| 157 | followTagsArg = followTags && pushSourceTags ? "--follow-tags" : "" |
Filip Pytloun | b36387e | 2017-03-06 11:39:19 +0100 | [diff] [blame] | 158 | ssh.agentSh "git push ${followTagsArg} origin HEAD:${branch}" |
Filip Pytloun | 49d6630 | 2017-03-06 10:26:22 +0100 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | if (followTags == true) { |
| 163 | ssh.agentSh "git push target --tags" |
| 164 | |
| 165 | if (pushSourceTags == true) { |
| 166 | ssh.agentSh "git push origin --tags" |
| 167 | } |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 168 | } |
| 169 | } |