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 | f0183f9 | 2020-10-05 22:40:49 +0200 | [diff] [blame] | 46 | sshagent(credentials: ['github-onf-bot-ssh-key', 'github-omec-project-ssh-key']) { |
pierventre | d424a4e | 2020-10-06 14:42:27 +0200 | [diff] [blame^] | 47 | withDockerRegistry([credentialsId: 'docker-artifact-push-credentials']) { |
| 48 | sh( script: """ |
| 49 | #!/usr/bin/env bash |
| 50 | set -eu -o pipefail |
pierventre | f41b90c | 2020-07-15 16:26:02 +0200 | [diff] [blame] | 51 | |
pierventre | d424a4e | 2020-10-06 14:42:27 +0200 | [diff] [blame^] | 52 | # checked out in a subdir so the log can be in WORKSPACE |
| 53 | cd "$projectName" |
pierventre | f41b90c | 2020-07-15 16:26:02 +0200 | [diff] [blame] | 54 | |
pierventre | d424a4e | 2020-10-06 14:42:27 +0200 | [diff] [blame^] | 55 | # set registry/repository variables |
| 56 | export DOCKER_REGISTRY="$dockerRegistry/" |
| 57 | export DOCKER_REPOSITORY="$dockerRepo/" |
pierventre | f41b90c | 2020-07-15 16:26:02 +0200 | [diff] [blame] | 58 | |
pierventre | d424a4e | 2020-10-06 14:42:27 +0200 | [diff] [blame^] | 59 | # Build w/branch |
| 60 | echo "Building image with branch" |
| 61 | $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] | 62 | |
pierventre | d424a4e | 2020-10-06 14:42:27 +0200 | [diff] [blame^] | 63 | # Build w/tags if they exist |
| 64 | if [ -n "$git_tags" ] |
| 65 | echo "Tags found in git, building:" |
| 66 | echo "$git_tags" |
pierventre | f41b90c | 2020-07-15 16:26:02 +0200 | [diff] [blame] | 67 | |
pierventre | d424a4e | 2020-10-06 14:42:27 +0200 | [diff] [blame^] | 68 | then |
| 69 | for tag in $git_tags |
| 70 | do |
| 71 | # remove leading 'v' on funky golang tags |
| 72 | clean_tag=\$(echo \$tag | sed 's/^v//g') |
| 73 | echo "Building image with tag: \$clean_tag (should reuse cached layers)" |
| 74 | $extraEnvironmentVars DOCKER_TAG="\$clean_tag" make docker-build |
| 75 | done |
| 76 | fi |
| 77 | """) |
pierventre | 65d85a2 | 2020-07-21 20:30:20 +0200 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | } |
pierventre | f41b90c | 2020-07-15 16:26:02 +0200 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | stage('push'){ |
| 85 | steps { |
| 86 | script { |
pierventre | 0ac6381 | 2020-07-27 11:59:49 +0200 | [diff] [blame] | 87 | withDockerRegistry(credentialsId: 'aether-internal-registry', url: "https://${params.dockerRegistry}") { |
pierventre | f41b90c | 2020-07-15 16:26:02 +0200 | [diff] [blame] | 88 | sh( script:""" |
| 89 | #!/usr/bin/env bash |
| 90 | set -eu -o pipefail |
| 91 | |
| 92 | # checked out in a subdir so the log can be in WORKSPACE |
| 93 | cd "$projectName" |
| 94 | |
| 95 | # set registry/repository variables |
pierventre | 0ac6381 | 2020-07-27 11:59:49 +0200 | [diff] [blame] | 96 | export DOCKER_REGISTRY="$dockerRegistry/" |
pierventre | f41b90c | 2020-07-15 16:26:02 +0200 | [diff] [blame] | 97 | export DOCKER_REPOSITORY="$dockerRepo/" |
| 98 | |
| 99 | # Push w/branch |
| 100 | echo "Pushing image with branch" |
| 101 | $extraEnvironmentVars DOCKER_TAG="$branchName" make docker-push 2>&1 | tee "$WORKSPACE/docker-push.log" |
| 102 | |
| 103 | # Push w/tags if they exist |
| 104 | if [ -n "$git_tags" ] |
| 105 | echo "Tags found in git, pushing:" |
| 106 | echo "$git_tags" |
| 107 | then |
| 108 | for tag in $git_tags |
| 109 | do |
| 110 | # remove leading 'v' on funky golang tags |
| 111 | clean_tag=\$(echo \$tag | sed 's/^v//g') |
| 112 | echo "Pushing image with tag: \$clean_tag (should reuse cached layers)" |
| 113 | $extraEnvironmentVars DOCKER_TAG="\$clean_tag" make docker-push |
| 114 | done |
| 115 | fi |
| 116 | """) |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | post { |
| 124 | always { |
| 125 | archiveArtifacts artifacts: 'docker-*.log', fingerprint: true |
| 126 | deleteDir() |
| 127 | } |
| 128 | failure { |
| 129 | step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: "${params.maintainers}", sendToIndividuals: false]) |
| 130 | } |
| 131 | } |
| 132 | } |