Jakub Josef | 65641f6 | 2018-01-26 17:34:26 +0100 | [diff] [blame] | 1 | #!groovy |
| 2 | |
| 3 | // Collect parameters |
| 4 | String mirror_name = env.MIRROR_NAME |
| 5 | String mirror_target = env.MIRROR_TARGET ?: env.MIRROR_NAME |
| 6 | |
| 7 | String snapshot_name = env.SNAPSHOT_NAME as String |
| 8 | String snapshot_id = env.SNAPSHOT_ID as String |
| 9 | String snapshot_dir = env.SNAPSHOT_DIR |
| 10 | String snapshot_rel_dir = env.SNAPSHOT_REL_DIR |
| 11 | |
| 12 | String root_dir = env.ROOT_DIR |
| 13 | |
| 14 | String slave_label = env.SLAVE_LABEL |
| 15 | |
| 16 | // Snapshot name can be hierarchical, i.e. can have subdirectories, so let's flatten it |
| 17 | String normalized_snapshot_name = snapshot_name.replaceAll('/', '-') |
| 18 | |
| 19 | String _snapshot = '' |
| 20 | |
| 21 | node(slave_label) { |
| 22 | try { |
| 23 | dir(snapshot_dir) { |
| 24 | // Guess link target |
| 25 | if (snapshot_id ==~ /^\d{4}-\d{2}-\d{2}-\d{6}$/) { |
| 26 | // Exact snapshot ID |
| 27 | _snapshot = "${mirror_target}-${snapshot_id}" |
| 28 | } else if (snapshot_id == 'latest') { |
| 29 | // Latest available snapshot |
| 30 | _snapshot = sh (script: "sed '1p;d' '${mirror_target}-${snapshot_id}.target.txt'", returnStdout: true).trim() |
| 31 | } else { |
| 32 | // Some named snapshot |
| 33 | _snapshot = sh (script: "readlink '${mirror_target}-${snapshot_id}'", returnStdout: true).trim() |
| 34 | } |
| 35 | |
| 36 | // Set name for the snapshot to prevent it from time-based cleanup |
| 37 | sh "ln -sfn '${_snapshot}' '${mirror_target}-${normalized_snapshot_name}'" |
| 38 | } |
| 39 | |
| 40 | // Set top-level name |
| 41 | dir("${root_dir}/${snapshot_name}") { |
| 42 | sh "ln -sfn '${snapshot_rel_dir}/${_snapshot}' '${mirror_name}'" |
| 43 | sh "echo '${snapshot_rel_dir}/${_snapshot}' > '${mirror_name}'.target.txt" |
| 44 | } |
| 45 | } finally { |
| 46 | // Cleanup |
| 47 | dir("${snapshot_dir}@tmp") { |
| 48 | deleteDir() |
| 49 | } |
| 50 | dir("${root_dir}/${snapshot_name}@tmp") { |
| 51 | deleteDir() |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // Set build description |
| 57 | currentBuild.description = "<p><b>${_snapshot}</b> (from ${snapshot_id})</p>" |
| 58 | |