Zack Williams | 3bf60d5 | 2019-06-07 12:56:10 -0700 | [diff] [blame] | 1 | // Copyright 2017-present Open Networking Foundation |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 15 | /* docker-publish pipeline */ |
| 16 | pipeline { |
| 17 | |
| 18 | /* no label, executor is determined by JJB */ |
| 19 | agent { |
| 20 | label "${params.executorNode}" |
| 21 | } |
| 22 | stages { |
| 23 | |
| 24 | stage('checkout') { |
| 25 | steps { |
| 26 | checkout([ |
| 27 | $class: 'GitSCM', |
| 28 | userRemoteConfigs: [[ |
| 29 | url: "${params.gitUrl}", |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 30 | name: "${params.gitRef}", |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 31 | ]], |
| 32 | extensions: [ |
| 33 | [$class: 'WipeWorkspace'], |
Zack Williams | 8d92d6d | 2019-04-16 18:12:12 -0700 | [diff] [blame] | 34 | [$class: 'RelativeTargetDirectory', relativeTargetDir: "${params.projectName}"], |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 35 | [$class: 'CloneOption', depth: 0, noTags: false, reference: '', shallow: false], |
| 36 | ], |
| 37 | ]) |
| 38 | script { |
Zack Williams | 8d92d6d | 2019-04-16 18:12:12 -0700 | [diff] [blame] | 39 | 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] | 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | stage('build'){ |
| 45 | steps { |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 46 | sh( script: """ |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 47 | #!/usr/bin/env bash |
| 48 | set -eu -o pipefail |
| 49 | |
| 50 | # checked out in a subdir so the log can be in WORKSPACE |
Zack Williams | 8d92d6d | 2019-04-16 18:12:12 -0700 | [diff] [blame] | 51 | cd "$projectName" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 52 | |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 53 | # set registry/repository variables |
| 54 | export DOCKER_REGISTRY="$dockerRegistry" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 55 | export DOCKER_REPOSITORY="$dockerRepo/" |
| 56 | |
| 57 | # Build w/branch |
| 58 | echo "Building image with branch" |
Zack Williams | 8d92d6d | 2019-04-16 18:12:12 -0700 | [diff] [blame] | 59 | 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] | 60 | |
| 61 | # Build w/tags if they exist |
| 62 | if [ -n "$git_tags" ] |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 63 | echo "Tags found in git, building:" |
| 64 | echo "$git_tags" |
| 65 | |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 66 | then |
| 67 | for tag in $git_tags |
| 68 | do |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 69 | echo "Building image with tag: \$tag (should reuse cached layers)" |
| 70 | make DOCKER_TAG="\$tag" docker-build |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 71 | done |
| 72 | fi |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 73 | """) |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | stage('push'){ |
| 78 | steps { |
| 79 | script { |
| 80 | withDockerRegistry([credentialsId: 'docker-artifact-push-credentials']) { |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 81 | sh( script:""" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 82 | #!/usr/bin/env bash |
| 83 | set -eu -o pipefail |
| 84 | |
| 85 | # checked out in a subdir so the log can be in WORKSPACE |
Zack Williams | 8d92d6d | 2019-04-16 18:12:12 -0700 | [diff] [blame] | 86 | cd "$projectName" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 87 | |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 88 | # set registry/repository variables |
| 89 | export DOCKER_REGISTRY="$dockerRegistry" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 90 | export DOCKER_REPOSITORY="$dockerRepo/" |
| 91 | |
| 92 | # Push w/branch |
| 93 | echo "Pushing image with branch" |
Zack Williams | 8d92d6d | 2019-04-16 18:12:12 -0700 | [diff] [blame] | 94 | 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] | 95 | |
| 96 | # Push w/tags if they exist |
| 97 | if [ -n "$git_tags" ] |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 98 | echo "Tags found in git, pushing:" |
| 99 | echo "$git_tags" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 100 | then |
| 101 | for tag in $git_tags |
| 102 | do |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 103 | echo "Pushing image with tag: \$tag (should reuse cached layers)" |
| 104 | make DOCKER_TAG="\$tag" docker-push |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 105 | done |
| 106 | fi |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 107 | """) |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | post { |
| 115 | always { |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 116 | archiveArtifacts artifacts: 'docker-*.log', fingerprint: true |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 117 | deleteDir() |
| 118 | } |
| 119 | } |
| 120 | } |