blob: 3bdacca1f294386d09da5f03a0d01b22db288353 [file] [log] [blame]
Sergey Kolekonovba203982016-12-21 18:32:17 +04001package 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 Josef7dccebe2017-03-06 18:08:32 +010016 * @param poll Enable git polling (default true)
17 * @param timeout Set checkout timeout (default 10)
Sergey Kolekonovba203982016-12-21 18:32:17 +040018 */
Jakub Josef7dccebe2017-03-06 18:08:32 +010019def checkoutGitRepository(path, url, branch, credentialsId = null, poll = true, timeout = 10){
Sergey Kolekonovba203982016-12-21 18:32:17 +040020 dir(path) {
Jakub Josef6fa8cb12017-03-06 18:20:08 +010021 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 Kolekonovba203982016-12-21 18:32:17 +040034 sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
35 }
36}
37
38/**
39 * Parse HEAD of current directory and return commit hash
40 */
41def getGitCommit() {
42 git_commit = sh (
43 script: 'git rev-parse HEAD',
44 returnStdout: true
45 ).trim()
46 return git_commit
47}
48
49/**
Ales Komarekfb7cbcb2017-02-24 14:02:03 +010050 * Change actual working branch of repo
51 *
52 * @param path Path to the git repository
53 * @param branch Branch desired to switch to
54 */
55def 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 */
71def 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 */
93def 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 Kolekonovba203982016-12-21 18:32:17 +0400105 * 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 */
114def 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 */
130def mirrorReporitory(sourceUrl, targetUrl, credentialsId, branches, followTags = false, gitEmail = 'jenkins@localhost', gitUsername = 'Jenkins') {
iberezovskiyd4240b52017-02-20 17:18:28 +0400131 def ssl = new com.mirantis.mk.Ssl()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400132 if (branches instanceof String) {
133 branches = branches.tokenize(',')
134 }
135 ssl.prepareSshAgentKey(credentialsId)
136 ssl.ensureKnownHosts(targetUrl)
137
Jakub Josef1b75ca82017-02-20 16:08:13 +0100138 sh "git remote | grep target || git remote add target ${targetUrl}"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400139 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}