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 | |
Zack Williams | b329208 | 2019-10-11 17:15:18 -0700 | [diff] [blame] | 18 | /* executor is determined by JJB */ |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 19 | agent { |
Zack Williams | b329208 | 2019-10-11 17:15:18 -0700 | [diff] [blame] | 20 | label "${params.buildNode}" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 21 | } |
| 22 | stages { |
| 23 | |
| 24 | stage('checkout') { |
| 25 | steps { |
| 26 | checkout([ |
| 27 | $class: 'GitSCM', |
Zack Williams | 9933027 | 2019-08-21 11:33:48 -0700 | [diff] [blame] | 28 | userRemoteConfigs: [[ url: "${params.gitUrl}", ]], |
| 29 | branches: [[ name: "${params.gitRef}", ]], |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 30 | extensions: [ |
| 31 | [$class: 'WipeWorkspace'], |
Zack Williams | 8d92d6d | 2019-04-16 18:12:12 -0700 | [diff] [blame] | 32 | [$class: 'RelativeTargetDirectory', relativeTargetDir: "${params.projectName}"], |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 33 | [$class: 'CloneOption', depth: 0, noTags: false, reference: '', shallow: false], |
You Wang | 715d5fc | 2020-04-30 16:23:35 -0700 | [diff] [blame] | 34 | [$class: 'SubmoduleOption', recursiveSubmodules: true], |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 35 | ], |
| 36 | ]) |
| 37 | script { |
Zack Williams | 8d92d6d | 2019-04-16 18:12:12 -0700 | [diff] [blame] | 38 | 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] | 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | stage('build'){ |
| 44 | steps { |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 45 | sh( script: """ |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 46 | #!/usr/bin/env bash |
| 47 | set -eu -o pipefail |
| 48 | |
| 49 | # checked out in a subdir so the log can be in WORKSPACE |
Zack Williams | 8d92d6d | 2019-04-16 18:12:12 -0700 | [diff] [blame] | 50 | cd "$projectName" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 51 | |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 52 | # set registry/repository variables |
| 53 | export DOCKER_REGISTRY="$dockerRegistry" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 54 | export DOCKER_REPOSITORY="$dockerRepo/" |
| 55 | |
| 56 | # Build w/branch |
| 57 | echo "Building image with branch" |
Matteo Scandolo | 8436612 | 2020-04-29 15:08:40 -0700 | [diff] [blame] | 58 | $extraEnvironmentVars DOCKER_TAG="$branchName" make docker-build 2>&1 | tee "$WORKSPACE/docker-build.log" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 59 | |
| 60 | # Build w/tags if they exist |
| 61 | if [ -n "$git_tags" ] |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 62 | echo "Tags found in git, building:" |
| 63 | echo "$git_tags" |
| 64 | |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 65 | then |
| 66 | for tag in $git_tags |
| 67 | do |
Zack Williams | 6e070f5 | 2019-10-04 11:08:59 -0700 | [diff] [blame] | 68 | # remove leading 'v' on funky golang tags |
Zack Williams | 2763f12 | 2019-10-04 15:35:35 -0700 | [diff] [blame] | 69 | clean_tag=\$(echo \$tag | sed 's/^v//g') |
Zack Williams | 6e070f5 | 2019-10-04 11:08:59 -0700 | [diff] [blame] | 70 | echo "Building image with tag: \$clean_tag (should reuse cached layers)" |
Matteo Scandolo | 8436612 | 2020-04-29 15:08:40 -0700 | [diff] [blame] | 71 | $extraEnvironmentVars DOCKER_TAG="\$clean_tag" make docker-build |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 72 | done |
| 73 | fi |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 74 | """) |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | stage('push'){ |
| 79 | steps { |
| 80 | script { |
| 81 | withDockerRegistry([credentialsId: 'docker-artifact-push-credentials']) { |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 82 | sh( script:""" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 83 | #!/usr/bin/env bash |
| 84 | set -eu -o pipefail |
| 85 | |
| 86 | # checked out in a subdir so the log can be in WORKSPACE |
Zack Williams | 8d92d6d | 2019-04-16 18:12:12 -0700 | [diff] [blame] | 87 | cd "$projectName" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 88 | |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 89 | # set registry/repository variables |
| 90 | export DOCKER_REGISTRY="$dockerRegistry" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 91 | export DOCKER_REPOSITORY="$dockerRepo/" |
| 92 | |
| 93 | # Push w/branch |
| 94 | echo "Pushing image with branch" |
Matteo Scandolo | 8436612 | 2020-04-29 15:08:40 -0700 | [diff] [blame] | 95 | $extraEnvironmentVars DOCKER_TAG="$branchName" make docker-push 2>&1 | tee "$WORKSPACE/docker-push.log" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 96 | |
| 97 | # Push w/tags if they exist |
| 98 | if [ -n "$git_tags" ] |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 99 | echo "Tags found in git, pushing:" |
| 100 | echo "$git_tags" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 101 | then |
| 102 | for tag in $git_tags |
| 103 | do |
Zack Williams | 6e070f5 | 2019-10-04 11:08:59 -0700 | [diff] [blame] | 104 | # remove leading 'v' on funky golang tags |
Zack Williams | 2763f12 | 2019-10-04 15:35:35 -0700 | [diff] [blame] | 105 | clean_tag=\$(echo \$tag | sed 's/^v//g') |
Zack Williams | 6e070f5 | 2019-10-04 11:08:59 -0700 | [diff] [blame] | 106 | echo "Pushing image with tag: \$clean_tag (should reuse cached layers)" |
Matteo Scandolo | 8436612 | 2020-04-29 15:08:40 -0700 | [diff] [blame] | 107 | $extraEnvironmentVars DOCKER_TAG="\$clean_tag" make docker-push |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 108 | done |
| 109 | fi |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 110 | """) |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | post { |
| 118 | always { |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 119 | archiveArtifacts artifacts: 'docker-*.log', fingerprint: true |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 120 | deleteDir() |
| 121 | } |
Matteo Scandolo | 3aba73a | 2019-09-27 09:03:38 -0700 | [diff] [blame] | 122 | failure { |
| 123 | step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: "${params.maintainers}", sendToIndividuals: false]) |
| 124 | } |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 125 | } |
| 126 | } |