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