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', |
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], |
| 34 | ], |
| 35 | ]) |
| 36 | script { |
Zack Williams | 8d92d6d | 2019-04-16 18:12:12 -0700 | [diff] [blame] | 37 | 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] | 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | stage('build'){ |
| 43 | steps { |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 44 | sh( script: """ |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 45 | #!/usr/bin/env bash |
| 46 | set -eu -o pipefail |
| 47 | |
| 48 | # checked out in a subdir so the log can be in WORKSPACE |
Zack Williams | 8d92d6d | 2019-04-16 18:12:12 -0700 | [diff] [blame] | 49 | cd "$projectName" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 50 | |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 51 | # set registry/repository variables |
| 52 | export DOCKER_REGISTRY="$dockerRegistry" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 53 | export DOCKER_REPOSITORY="$dockerRepo/" |
| 54 | |
| 55 | # Build w/branch |
| 56 | echo "Building image with branch" |
Zack Williams | 8d92d6d | 2019-04-16 18:12:12 -0700 | [diff] [blame] | 57 | 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] | 58 | |
| 59 | # Build w/tags if they exist |
| 60 | if [ -n "$git_tags" ] |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 61 | echo "Tags found in git, building:" |
| 62 | echo "$git_tags" |
| 63 | |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 64 | then |
| 65 | for tag in $git_tags |
| 66 | do |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 67 | echo "Building image with tag: \$tag (should reuse cached layers)" |
| 68 | make DOCKER_TAG="\$tag" docker-build |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 69 | done |
| 70 | fi |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 71 | """) |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | stage('push'){ |
| 76 | steps { |
| 77 | script { |
| 78 | withDockerRegistry([credentialsId: 'docker-artifact-push-credentials']) { |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 79 | sh( script:""" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 80 | #!/usr/bin/env bash |
| 81 | set -eu -o pipefail |
| 82 | |
| 83 | # checked out in a subdir so the log can be in WORKSPACE |
Zack Williams | 8d92d6d | 2019-04-16 18:12:12 -0700 | [diff] [blame] | 84 | cd "$projectName" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 85 | |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 86 | # set registry/repository variables |
| 87 | export DOCKER_REGISTRY="$dockerRegistry" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 88 | export DOCKER_REPOSITORY="$dockerRepo/" |
| 89 | |
| 90 | # Push w/branch |
| 91 | echo "Pushing image with branch" |
Zack Williams | 8d92d6d | 2019-04-16 18:12:12 -0700 | [diff] [blame] | 92 | 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] | 93 | |
| 94 | # Push w/tags if they exist |
| 95 | if [ -n "$git_tags" ] |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 96 | echo "Tags found in git, pushing:" |
| 97 | echo "$git_tags" |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 98 | then |
| 99 | for tag in $git_tags |
| 100 | do |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 101 | echo "Pushing image with tag: \$tag (should reuse cached layers)" |
| 102 | make DOCKER_TAG="\$tag" docker-push |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 103 | done |
| 104 | fi |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 105 | """) |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | post { |
| 113 | always { |
Zack Williams | 38126df | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 114 | archiveArtifacts artifacts: 'docker-*.log', fingerprint: true |
Zack Williams | e16a9bd | 2019-03-21 23:02:24 -0700 | [diff] [blame] | 115 | deleteDir() |
| 116 | } |
| 117 | } |
| 118 | } |