Richard Felkl | 655f0ce | 2018-04-19 14:57:43 +0200 | [diff] [blame] | 1 | /** |
| 2 | * |
| 3 | * Aptly add packages pipeline |
| 4 | * |
| 5 | * Expected parameters: |
| 6 | * APTLY_API_URL - URL of Aptly API, example: http://10.1.0.12:8080 |
| 7 | * APTLY_REPO - Name of aptly repo to put packages |
| 8 | * CLEANUP_REPO - Option to cleanup repo contents after publish |
| 9 | * PROMOTE - Option to promote the publish |
| 10 | * PROMOTE_COMPONENT - Component to promote (only used when PROMOTE = True) |
| 11 | * PROMOTE_SOURCE - Source regex to promote from (only used when PROMOTE = True) |
| 12 | * PROMOTE_TARGET - Target regex to promote to (only used when PROMOTE = True) |
| 13 | * PROMOTE_STORAGES - Storages to promote (only used when PROMOTE = True) |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | // Load shared libs |
| 18 | aptly = new com.mirantis.mk.Aptly() |
| 19 | common = new com.mirantis.mk.Common() |
| 20 | |
| 21 | timeout(time: 12, unit: 'HOURS') { |
| 22 | node("docker&&hardware") { |
| 23 | try { |
| 24 | def aptlyServer = ["url": APTLY_API_URL] |
| 25 | def workspace = common.getWorkspace() |
| 26 | def actualTime = (System.currentTimeMillis()/1000).toInteger() |
| 27 | def snapshotName = APTLY_REPO + "-" + actualTime |
| 28 | |
| 29 | lock("aptly-api") { |
| 30 | stage("upload") { |
| 31 | def inputFile = input message: 'Upload file', parameters: [file(name: 'packages.tar.gz')] |
| 32 | new hudson.FilePath(new File("$workspace/packages.tar.gz")).copyFrom(inputFile) |
| 33 | inputFile.delete() |
| 34 | |
| 35 | sh "mkdir ${workspace}/packages;tar -xvzf ${workspace}/packages.tar.gz --directory ${workspace}/packages" |
| 36 | |
| 37 | def packages = sh(script: "ls -1a ${workspace}/packages | tail -n +3", returnStdout: true) |
| 38 | packages = packages.tokenize("\n") |
| 39 | for(pkg in packages){ |
| 40 | aptly.uploadPackage("${workspace}/packages/${pkg}", APTLY_API_URL, APTLY_REPO) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | stage("publish") { |
| 45 | aptly.snapshotCreateByAPI(aptlyServer, APTLY_REPO, snapshotName) |
| 46 | aptly.publish(APTLY_API_URL) |
| 47 | |
| 48 | if(PROMOTE.toBoolean()){ |
| 49 | aptly.promotePublish(APTLY_API_URL, PROMOTE_SOURCE, PROMOTE_TARGET, false, PROMOTE_COMPONENT, null, false, '-d --timeout 600', false, PROMOTE_STORAGES) |
| 50 | } |
| 51 | |
| 52 | if(CLEANUP_REPO.toBoolean()){ |
| 53 | def packageList = aptly.listPackagesFromRepoByAPI(aptlyServer, APTLY_REPO) |
| 54 | aptly.deletePackagesFromRepoByAPI(aptlyServer, APTLY_REPO, packageList) |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | sh "rm -rf ${workspace}/*" |
| 59 | } |
| 60 | catch (Throwable e) { |
| 61 | // If there was an error or exception thrown, the build failed |
| 62 | sh "rm -rf ${workspace}/*" |
| 63 | currentBuild.result = "FAILURE" |
| 64 | throw e |
| 65 | } |
| 66 | } |
| 67 | } |