Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 1 | def common = new com.mirantis.mk.Common() |
Filip Pytloun | 38005aa | 2017-03-06 10:26:38 +0100 | [diff] [blame] | 2 | def git = new com.mirantis.mk.Git() |
azvyagintsev | 8710f80 | 2019-04-18 19:38:45 +0300 | [diff] [blame] | 3 | |
| 4 | stage('Mirror') { |
| 5 | timeout(time: 12, unit: 'HOURS') { |
| 6 | node() { |
| 7 | try { |
Ivan Berezovskiy | 7a0e6ce | 2019-07-09 15:46:47 +0400 | [diff] [blame] | 8 | 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 | } |
azvyagintsev | 8710f80 | 2019-04-18 19:38:45 +0300 | [diff] [blame] | 24 | 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 Berezovskiy | 7a0e6ce | 2019-07-09 15:46:47 +0400 | [diff] [blame] | 37 | extensions: [[$class: 'CleanCheckout']], submoduleCfg: [], |
| 38 | userRemoteConfigs: [[credentialsId: sourceCreds, url: SOURCE_URL]]] |
azvyagintsev | 8710f80 | 2019-04-18 19:38:45 +0300 | [diff] [blame] | 39 | 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 Berezovskiy | 7a0e6ce | 2019-07-09 15:46:47 +0400 | [diff] [blame] | 46 | } finally { |
| 47 | sh "git config --global --unset credential.${SOURCE_URL}.username || true" |
| 48 | deleteDir() |
azvyagintsev | 8710f80 | 2019-04-18 19:38:45 +0300 | [diff] [blame] | 49 | } |
Martin Polreich | 2c8beed | 2019-03-12 16:44:46 +0100 | [diff] [blame] | 50 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 51 | } |
Filip Pytloun | 38005aa | 2017-03-06 10:26:38 +0100 | [diff] [blame] | 52 | } |