pierventre | f41b90c | 2020-07-15 16:26:02 +0200 | [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 | |
| 15 | /* docker-publish pipeline */ |
| 16 | pipeline { |
| 17 | |
| 18 | /* executor is determined by JJB */ |
| 19 | agent { |
| 20 | label "${params.buildNode}" |
| 21 | } |
| 22 | stages { |
| 23 | |
| 24 | stage('checkout') { |
| 25 | steps { |
| 26 | checkout([ |
| 27 | $class: 'GitSCM', |
pierventre | 07ef37d | 2020-07-23 12:23:17 +0200 | [diff] [blame] | 28 | userRemoteConfigs: [[ url: "${params.gitUrl}", credentialsId: 'onos-jenkins-ssh', refspec: "${params.gitRefSpec}"]], |
pierventre | f41b90c | 2020-07-15 16:26:02 +0200 | [diff] [blame] | 29 | branches: [[ name: "${params.gitRef}", ]], |
| 30 | extensions: [ |
| 31 | [$class: 'WipeWorkspace'], |
| 32 | [$class: 'RelativeTargetDirectory', relativeTargetDir: "${params.projectName}"], |
| 33 | [$class: 'CloneOption', depth: 0, noTags: false, reference: '', shallow: false], |
| 34 | [$class: 'SubmoduleOption', recursiveSubmodules: true], |
| 35 | ], |
| 36 | ]) |
| 37 | script { |
| 38 | git_tags = sh(script:"cd $projectName; git tag -l --points-at HEAD", returnStdout: true).trim() |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | stage('build'){ |
| 44 | steps { |
pierventre | 65d85a2 | 2020-07-21 20:30:20 +0200 | [diff] [blame] | 45 | script { |
pierventre | 63d7e2c | 2020-07-30 17:49:02 +0200 | [diff] [blame] | 46 | withCredentials([string(credentialsId: 'onos-builder', variable: 'ONOS_BUILDER_API')]) { |
| 47 | withCredentials([string(credentialsId: '64fe2b1a-b33a-4f13-8442-ad8360434003', variable: 'OMECPROJECT_API')]) { |
| 48 | withDockerRegistry([credentialsId: 'docker-artifact-push-credentials']) { |
| 49 | sh( script: """ |
| 50 | #!/usr/bin/env bash |
| 51 | set -eu -o pipefail |
pierventre | f41b90c | 2020-07-15 16:26:02 +0200 | [diff] [blame] | 52 | |
pierventre | 63d7e2c | 2020-07-30 17:49:02 +0200 | [diff] [blame] | 53 | # checked out in a subdir so the log can be in WORKSPACE |
| 54 | cd "$projectName" |
pierventre | f41b90c | 2020-07-15 16:26:02 +0200 | [diff] [blame] | 55 | |
pierventre | 63d7e2c | 2020-07-30 17:49:02 +0200 | [diff] [blame] | 56 | # set registry/repository variables |
| 57 | export DOCKER_REGISTRY="$dockerRegistry/" |
| 58 | export DOCKER_REPOSITORY="$dockerRepo/" |
pierventre | f41b90c | 2020-07-15 16:26:02 +0200 | [diff] [blame] | 59 | |
pierventre | 63d7e2c | 2020-07-30 17:49:02 +0200 | [diff] [blame] | 60 | # Build w/branch |
| 61 | echo "Building image with branch" |
| 62 | $extraEnvironmentVars DOCKER_TAG="$branchName" make docker-build 2>&1 | tee "$WORKSPACE/docker-build.log" |
pierventre | f41b90c | 2020-07-15 16:26:02 +0200 | [diff] [blame] | 63 | |
pierventre | 63d7e2c | 2020-07-30 17:49:02 +0200 | [diff] [blame] | 64 | # Build w/tags if they exist |
| 65 | if [ -n "$git_tags" ] |
| 66 | echo "Tags found in git, building:" |
| 67 | echo "$git_tags" |
pierventre | f41b90c | 2020-07-15 16:26:02 +0200 | [diff] [blame] | 68 | |
pierventre | 63d7e2c | 2020-07-30 17:49:02 +0200 | [diff] [blame] | 69 | then |
| 70 | for tag in $git_tags |
| 71 | do |
| 72 | # remove leading 'v' on funky golang tags |
| 73 | clean_tag=\$(echo \$tag | sed 's/^v//g') |
| 74 | echo "Building image with tag: \$clean_tag (should reuse cached layers)" |
| 75 | $extraEnvironmentVars DOCKER_TAG="\$clean_tag" make docker-build |
| 76 | done |
| 77 | fi |
| 78 | """) |
| 79 | } |
pierventre | 65d85a2 | 2020-07-21 20:30:20 +0200 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | } |
pierventre | f41b90c | 2020-07-15 16:26:02 +0200 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | stage('push'){ |
| 87 | steps { |
| 88 | script { |
pierventre | 0ac6381 | 2020-07-27 11:59:49 +0200 | [diff] [blame] | 89 | withDockerRegistry(credentialsId: 'aether-internal-registry', url: "https://${params.dockerRegistry}") { |
pierventre | f41b90c | 2020-07-15 16:26:02 +0200 | [diff] [blame] | 90 | sh( script:""" |
| 91 | #!/usr/bin/env bash |
| 92 | set -eu -o pipefail |
| 93 | |
| 94 | # checked out in a subdir so the log can be in WORKSPACE |
| 95 | cd "$projectName" |
| 96 | |
| 97 | # set registry/repository variables |
pierventre | 0ac6381 | 2020-07-27 11:59:49 +0200 | [diff] [blame] | 98 | export DOCKER_REGISTRY="$dockerRegistry/" |
pierventre | f41b90c | 2020-07-15 16:26:02 +0200 | [diff] [blame] | 99 | export DOCKER_REPOSITORY="$dockerRepo/" |
| 100 | |
| 101 | # Push w/branch |
| 102 | echo "Pushing image with branch" |
| 103 | $extraEnvironmentVars DOCKER_TAG="$branchName" make docker-push 2>&1 | tee "$WORKSPACE/docker-push.log" |
| 104 | |
| 105 | # Push w/tags if they exist |
| 106 | if [ -n "$git_tags" ] |
| 107 | echo "Tags found in git, pushing:" |
| 108 | echo "$git_tags" |
| 109 | then |
| 110 | for tag in $git_tags |
| 111 | do |
| 112 | # remove leading 'v' on funky golang tags |
| 113 | clean_tag=\$(echo \$tag | sed 's/^v//g') |
| 114 | echo "Pushing image with tag: \$clean_tag (should reuse cached layers)" |
| 115 | $extraEnvironmentVars DOCKER_TAG="\$clean_tag" make docker-push |
| 116 | done |
| 117 | fi |
| 118 | """) |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | post { |
| 126 | always { |
| 127 | archiveArtifacts artifacts: 'docker-*.log', fingerprint: true |
| 128 | deleteDir() |
| 129 | } |
| 130 | failure { |
| 131 | step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: "${params.maintainers}", sendToIndividuals: false]) |
| 132 | } |
| 133 | } |
| 134 | } |