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