Sergey Galkin | 24ebd76 | 2020-01-14 14:36:34 +0400 | [diff] [blame^] | 1 | - job-template: |
| 2 | project-type: pipeline |
| 3 | description: '{job-description}' |
| 4 | sandbox: true |
| 5 | concurrent: true |
| 6 | disabled: false |
| 7 | dsl: |2- |
| 8 | |
| 9 | |
| 10 | def image_sha = null |
| 11 | def image_md5 = null |
| 12 | def image_name = null |
| 13 | def image_size = null |
| 14 | def storage_path = '/home/jenkins/images' |
| 15 | def node_name = "${{NODE_NAME}}" |
| 16 | def DOWNLOAD_FLAG = true |
| 17 | |
| 18 | node(node_name) {{ |
| 19 | |
| 20 | stage("Clean Workspace") {{ |
| 21 | step([$class: 'WsCleanup']) |
| 22 | }} |
| 23 | |
| 24 | stage("Check already exist file") {{ |
| 25 | try {{ |
| 26 | def file_name = "${{IMAGE_URL}}".split('/').last() |
| 27 | sh "[ -f ${{storage_path}}/${{file_name}} ]" |
| 28 | echo "We have already downloaded the image ${{IMAGE_URL}} in ${{storage_path}}" |
| 29 | DOWNLOAD_FLAG = false |
| 30 | currentBuild.result = 'NOT_BUILT' |
| 31 | }} catch (e) {{ |
| 32 | echo "We don't have an image, need to download" |
| 33 | DOWNLOAD_FLAG = true |
| 34 | }} |
| 35 | }} |
| 36 | |
| 37 | stage("Download image") {{ |
| 38 | if (DOWNLOAD_FLAG) {{ |
| 39 | sh "wget --progress=dot:giga ${{IMAGE_URL}}" |
| 40 | image_name = sh(returnStdout: true, script: "basename ${{IMAGE_URL}}").trim() |
| 41 | image_size = sh(returnStdout: true, script: "stat --printf='%s' ${{image_name}}").trim() |
| 42 | }} else {{ |
| 43 | echo "Skip due to DOWNLOAD_FLAG ins't true" |
| 44 | }} |
| 45 | |
| 46 | }} |
| 47 | |
| 48 | stage("Calculate sha") {{ |
| 49 | if (DOWNLOAD_FLAG) {{ |
| 50 | image_sha = sh(returnStdout: true, script: "sha256sum ${{image_name}} | cut -d ' ' -f 1").trim() |
| 51 | image_md5 = sh(returnStdout: true, script: "md5sum ${{image_name}} | cut -d ' ' -f 1").trim() |
| 52 | }} else {{ |
| 53 | echo "Skip due to DOWNLOAD_FLAG ins't true" |
| 54 | }} |
| 55 | }} |
| 56 | |
| 57 | stage("Save in home") {{ |
| 58 | if (DOWNLOAD_FLAG) {{ |
| 59 | // sh "mv ${{image_name}} ${{image_name}}-${{image_md5}}" |
| 60 | // sh "mv ${{image_name}}-${{image_md5}} ${{storage_path}}/" |
| 61 | sh "mv ${{image_name}} ${{storage_path}}/" |
| 62 | }} else {{ |
| 63 | echo "Skip due to DOWNLOAD_FLAG ins't true" |
| 64 | }} |
| 65 | }} |
| 66 | |
| 67 | stage("Create source file") {{ |
| 68 | if (DOWNLOAD_FLAG) {{ |
| 69 | sh """ |
| 70 | touch image_data.txt |
| 71 | echo IMAGE_URL=${{IMAGE_URL}} >> image_data.txt |
| 72 | echo IMAGE_SHA256=${{image_sha}} >> image_data.txt |
| 73 | echo IMAGE_MD5=${{image_md5}} >> image_data.txt |
| 74 | echo IMAGE_NAME=\$(basename ${{IMAGE_URL}}) >> image_data.txt |
| 75 | echo IMAGE_PATH=${{storage_path}}/${{image_name}} >> image_data.txt |
| 76 | echo IMAGE_DOWNLOADED=\$(date +%Y%m%d%H%M%S) >> image_data.txt |
| 77 | """ |
| 78 | sh """ |
| 79 | touch image_data.json |
| 80 | cat << EOM > image_data.json |
| 81 | {{ |
| 82 | "IMAGE_URL": "${{IMAGE_URL}}", |
| 83 | "IMAGE_SHA256": "${{image_sha}}", |
| 84 | "IMAGE_MD5": "${{image_md5}}", |
| 85 | "IMAGE_NAME": "\$(basename ${{IMAGE_URL}})", |
| 86 | "IMAGE_PATH": "${{storage_path}}/${{image_name}}", |
| 87 | "IMAGE_DOWNLOADED": "\$(date +%Y%m%d%H%M%S)" |
| 88 | }} |
| 89 | EOM |
| 90 | """ |
| 91 | }} else {{ |
| 92 | echo "Skip due to DOWNLOAD_FLAG ins't true" |
| 93 | }} |
| 94 | |
| 95 | }} |
| 96 | |
| 97 | stage("Save artifacts") {{ |
| 98 | if (DOWNLOAD_FLAG) {{ |
| 99 | archiveArtifacts allowEmptyArchive: false, |
| 100 | artifacts: "image_data.*" |
| 101 | }} else {{ |
| 102 | echo "Skip due to DOWNLOAD_FLAG ins't true" |
| 103 | }} |
| 104 | }} |
| 105 | |
| 106 | }} |
| 107 | name: download-config-drive |
| 108 | parameters: |
| 109 | - string: |
| 110 | default: '' |
| 111 | description: '' |
| 112 | name: IMAGE_URL |
| 113 | - string: |
| 114 | default: offline-deploy |
| 115 | description: '' |
| 116 | name: NODE_NAME |
| 117 | |