Ivan Udovichenko | 24af415 | 2020-10-29 20:57:08 +0300 | [diff] [blame] | 1 | /** |
| 2 | * File upload to AWS S3 pipeline |
| 3 | * |
| 4 | * Expected parameters: |
| 5 | * DEST S3 bucket to upload files to (e.g. s3://images-mirantis-com) |
| 6 | * IMAGE The image with s4cmd utility installed |
| 7 | * SOURCE The source URL to download files from (e.g. http://images.mcp.mirantis.net) |
| 8 | * FILENAMES Relative path to files. (SOURCE and FILENAMES values are being |
| 9 | * concantenated to form URL to download files from.) |
| 10 | * FORCE_UPLOAD Force file upload (will overwrite existing destination file) |
| 11 | */ |
| 12 | |
Kirill Mashchenko | da53589 | 2018-09-05 11:05:52 +0300 | [diff] [blame] | 13 | def common = new com.mirantis.mk.Common() |
Ivan Udovichenko | 26208ed | 2020-10-29 16:59:09 +0300 | [diff] [blame] | 14 | def s4cmdOpts = ' --sync-check ' |
| 15 | Boolean forceUpload = (env.FORCE_UPLOAD ?: false).toBoolean() |
Ivan Udovichenko | 24af415 | 2020-10-29 20:57:08 +0300 | [diff] [blame] | 16 | def wgetTries = env.WGET_TRIES ?: '50' |
Kirill Mashchenko | da53589 | 2018-09-05 11:05:52 +0300 | [diff] [blame] | 17 | |
| 18 | node("docker") { |
| 19 | stage('Prepare') { |
| 20 | img = docker.image(IMAGE) |
| 21 | img.pull() |
| 22 | } |
| 23 | stage('Upload') { |
Ivan Udovichenko | 26208ed | 2020-10-29 16:59:09 +0300 | [diff] [blame] | 24 | FILENAMES.split().each { filenamePath -> |
| 25 | url = "${SOURCE}/${filenamePath}" |
| 26 | filename = filenamePath.tokenize('/')[-1] |
azvyagintsev | ac363ef | 2018-11-23 20:13:08 +0200 | [diff] [blame] | 27 | opts = s4cmdOpts |
Ivan Udovichenko | 26208ed | 2020-10-29 16:59:09 +0300 | [diff] [blame] | 28 | if(filenamePath.contains('stable') || forceUpload) { |
| 29 | common.warningMsg("Going to ovveride ${filenamePath}") |
azvyagintsev | ac363ef | 2018-11-23 20:13:08 +0200 | [diff] [blame] | 30 | opts = opts + '--force' |
| 31 | } |
Kirill Mashchenko | da53589 | 2018-09-05 11:05:52 +0300 | [diff] [blame] | 32 | img.withRun("--entrypoint='/bin/bash'") { c -> |
Ivan Udovichenko | 26208ed | 2020-10-29 16:59:09 +0300 | [diff] [blame] | 33 | withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'aws-s3', |
Kirill Mashchenko | da53589 | 2018-09-05 11:05:52 +0300 | [diff] [blame] | 34 | usernameVariable: 'S3_ACCESS_KEY', passwordVariable: 'S3_SECRET_KEY']]) { |
| 35 | img.inside("-e S3_ACCESS_KEY=${S3_ACCESS_KEY} -e S3_SECRET_KEY=${S3_SECRET_KEY}") { |
| 36 | common.retry(3, 5) { |
Ivan Udovichenko | 24af415 | 2020-10-29 20:57:08 +0300 | [diff] [blame] | 37 | sh(script: "wget --progress=dot:giga --tries=${wgetTries} -O ${filename} ${url}", returnStdout: true) |
Ivan Udovichenko | 26208ed | 2020-10-29 16:59:09 +0300 | [diff] [blame] | 38 | sh(script: "/usr/local/bin/s4cmd put ${opts} ${filename} ${DEST}/${filenamePath}", returnStdout: true) |
Kirill Mashchenko | da53589 | 2018-09-05 11:05:52 +0300 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | |
| 44 | } |
| 45 | sh("rm ${filename}") |
| 46 | } |
| 47 | } |
| 48 | deleteDir() |
| 49 | } |