Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 1 | /* docker-publish pipeline */ |
| 2 | pipeline { |
| 3 | |
| 4 | /* no label, executor is determined by JJB */ |
| 5 | agent { |
| 6 | label "${params.executorNode}" |
| 7 | } |
| 8 | stages { |
| 9 | |
| 10 | stage('checkout') { |
| 11 | steps { |
| 12 | checkout([ |
| 13 | $class: 'GitSCM', |
| 14 | userRemoteConfigs: [[ |
| 15 | url: "${params.gitUrl}", |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 16 | name: "${params.gitRef}", |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 17 | ]], |
| 18 | extensions: [ |
| 19 | [$class: 'WipeWorkspace'], |
Zack Williams | 8d92d6d | 2019-04-16 18:12:12 -0700 | [diff] [blame] | 20 | [$class: 'RelativeTargetDirectory', relativeTargetDir: "${params.projectName}"], |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 21 | [$class: 'CloneOption', depth: 0, noTags: false, reference: '', shallow: false], |
| 22 | ], |
| 23 | ]) |
| 24 | script { |
Zack Williams | 8d92d6d | 2019-04-16 18:12:12 -0700 | [diff] [blame] | 25 | git_tags = sh(script:"cd $projectName; git tag -l --points-at HEAD", returnStdout: true).trim() |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | stage('build'){ |
| 31 | steps { |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 32 | sh( script: """ |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 33 | #!/usr/bin/env bash |
| 34 | set -eu -o pipefail |
| 35 | |
| 36 | # checked out in a subdir so the log can be in WORKSPACE |
Zack Williams | 8d92d6d | 2019-04-16 18:12:12 -0700 | [diff] [blame] | 37 | cd "$projectName" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 38 | |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 39 | # set registry/repository variables |
| 40 | export DOCKER_REGISTRY="$dockerRegistry" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 41 | export DOCKER_REPOSITORY="$dockerRepo/" |
| 42 | |
| 43 | # Build w/branch |
| 44 | echo "Building image with branch" |
Zack Williams | 8d92d6d | 2019-04-16 18:12:12 -0700 | [diff] [blame] | 45 | make DOCKER_TAG="$branchName" docker-build 2>&1 | tee "$WORKSPACE/docker-build.log" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 46 | |
| 47 | # Build w/tags if they exist |
| 48 | if [ -n "$git_tags" ] |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 49 | echo "Tags found in git, building:" |
| 50 | echo "$git_tags" |
| 51 | |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 52 | then |
| 53 | for tag in $git_tags |
| 54 | do |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 55 | echo "Building image with tag: \$tag (should reuse cached layers)" |
| 56 | make DOCKER_TAG="\$tag" docker-build |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 57 | done |
| 58 | fi |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 59 | """) |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | stage('push'){ |
| 64 | steps { |
| 65 | script { |
| 66 | withDockerRegistry([credentialsId: 'docker-artifact-push-credentials']) { |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 67 | sh( script:""" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 68 | #!/usr/bin/env bash |
| 69 | set -eu -o pipefail |
| 70 | |
| 71 | # checked out in a subdir so the log can be in WORKSPACE |
Zack Williams | 8d92d6d | 2019-04-16 18:12:12 -0700 | [diff] [blame] | 72 | cd "$projectName" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 73 | |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 74 | # set registry/repository variables |
| 75 | export DOCKER_REGISTRY="$dockerRegistry" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 76 | export DOCKER_REPOSITORY="$dockerRepo/" |
| 77 | |
| 78 | # Push w/branch |
| 79 | echo "Pushing image with branch" |
Zack Williams | 8d92d6d | 2019-04-16 18:12:12 -0700 | [diff] [blame] | 80 | make DOCKER_TAG="$branchName" docker-push 2>&1 | tee "$WORKSPACE/docker-push.log" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 81 | |
| 82 | # Push w/tags if they exist |
| 83 | if [ -n "$git_tags" ] |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 84 | echo "Tags found in git, pushing:" |
| 85 | echo "$git_tags" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 86 | then |
| 87 | for tag in $git_tags |
| 88 | do |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 89 | echo "Pushing image with tag: \$tag (should reuse cached layers)" |
| 90 | make DOCKER_TAG="\$tag" docker-push |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 91 | done |
| 92 | fi |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 93 | """) |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | post { |
| 101 | always { |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 102 | archiveArtifacts artifacts: 'docker-*.log', fingerprint: true |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 103 | deleteDir() |
| 104 | } |
| 105 | } |
| 106 | } |