blob: 6f14866bf4577156298f85625bfa8277cd8a3944 [file] [log] [blame]
Jakub Josefc5a223a2017-03-01 14:40:08 +01001def common = new com.mirantis.mk.Common()
Filip Pytloun38005aa2017-03-06 10:26:38 +01002def git = new com.mirantis.mk.Git()
azvyagintsev8710f802019-04-18 19:38:45 +03003
4stage('Mirror') {
5 timeout(time: 12, unit: 'HOURS') {
6 node() {
7 try {
Ivan Berezovskiy7a0e6ce2019-07-09 15:46:47 +04008 def sourceCreds = env.SOURCE_CREDENTIALS
9 if (sourceCreds && common.getCredentialsById(sourceCreds, 'password')) {
10 withCredentials([
11 [$class : 'UsernamePasswordMultiBinding',
12 credentialsId : sourceCreds,
13 passwordVariable: 'GIT_PASS',
14 usernameVariable: 'GIT_USER']
15 ]) {
16 sh """
17 set +x
18 git config --global credential.${SOURCE_URL}.username \${GIT_USER}
19 echo "echo \${GIT_PASS}" > askpass.sh && chmod +x askpass.sh
20 """
21 env.GIT_ASKPASS = "${env.WORKSPACE}/askpass.sh"
22 }
23 }
azvyagintsev8710f802019-04-18 19:38:45 +030024 if (BRANCHES == '*' || BRANCHES.contains('*')) {
25 branches = git.getBranchesForGitRepo(SOURCE_URL, BRANCHES)
26 } else {
27 branches = BRANCHES.tokenize(',')
28 }
29 common.infoMsg('branches to fetch:' + branches.toString())
30 def pollBranches = []
31 for (i = 0; i < branches.size(); i++) {
32 pollBranches.add([name: branches[i]])
33 }
34 dir('source') {
35 checkout changelog: true, poll: true,
36 scm: [$class : 'GitSCM', branches: pollBranches, doGenerateSubmoduleConfigurations: false,
Ivan Berezovskiy7a0e6ce2019-07-09 15:46:47 +040037 extensions: [[$class: 'CleanCheckout']], submoduleCfg: [],
38 userRemoteConfigs: [[credentialsId: sourceCreds, url: SOURCE_URL]]]
azvyagintsev8710f802019-04-18 19:38:45 +030039 git.mirrorGit(SOURCE_URL, TARGET_URL, CREDENTIALS_ID, branches, true)
40 }
41 } catch (Throwable e) {
42 // If there was an error or exception thrown, the build failed
43 currentBuild.result = 'FAILURE'
44 currentBuild.description = currentBuild.description ? e.message + '' + currentBuild.description : e.message
45 throw e
Ivan Berezovskiy7a0e6ce2019-07-09 15:46:47 +040046 } finally {
47 sh "git config --global --unset credential.${SOURCE_URL}.username || true"
48 deleteDir()
azvyagintsev8710f802019-04-18 19:38:45 +030049 }
Martin Polreich2c8beed2019-03-12 16:44:46 +010050 }
Jakub Josefc5a223a2017-03-01 14:40:08 +010051 }
Filip Pytloun38005aa2017-03-06 10:26:38 +010052}