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