Skip to content

Commit e3e28d2

Browse files
committed
refactor pushDockerImages
uses elastic/apm-pipeline-library#1550
1 parent 6769d47 commit e3e28d2

File tree

1 file changed

+31
-79
lines changed

1 file changed

+31
-79
lines changed

.ci/packaging.groovy

Lines changed: 31 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env groovy
22

3-
@Library('apm@current') _
3+
@Library('apm@feature/docker-tag-push-step') _
44

55
import groovy.transform.Field
66

@@ -268,21 +268,35 @@ pipeline {
268268
def pushCIDockerImages(Map args = [:]) {
269269
def arch = args.get('arch', 'amd64')
270270
catchError(buildResult: 'UNSTABLE', message: 'Unable to push Docker images', stageResult: 'FAILURE') {
271+
def defaultVariants = [
272+
'' : 'beats',
273+
'-oss' : 'beats',
274+
'-ubi8' : 'beats'
275+
]
271276
if (env?.BEATS_FOLDER?.endsWith('auditbeat')) {
272-
tagAndPush(beatName: 'auditbeat', arch: arch)
277+
project = [ name: 'auditbeat', variants: defaultVariants ]
273278
} else if (env?.BEATS_FOLDER?.endsWith('filebeat')) {
274-
tagAndPush(beatName: 'filebeat', arch: arch)
279+
project = [ name: 'filebeat', variants: defaultVariants ]
275280
} else if (env?.BEATS_FOLDER?.endsWith('heartbeat')) {
276-
tagAndPush(beatName: 'heartbeat', arch: arch)
281+
project = [ name: 'heartbeat', variants: defaultVariants ]
277282
} else if (env?.BEATS_FOLDER?.endsWith('metricbeat')) {
278-
tagAndPush(beatName: 'metricbeat', arch: arch)
283+
project = [ name: 'metricbeat', variants: defaultVariants ]
279284
} else if (env?.BEATS_FOLDER?.endsWith('osquerybeat')) {
280-
tagAndPush(beatName: 'osquerybeat', arch: arch)
285+
project = [ name: 'osquerybeat', variants: defaultVariants ]
281286
} else if ("${env.BEATS_FOLDER}" == "packetbeat"){
282-
tagAndPush(beatName: 'packetbeat', arch: arch)
287+
project = [ name: 'packetbeat', variants: defaultVariants ]
283288
} else if ("${env.BEATS_FOLDER}" == "x-pack/elastic-agent") {
284-
tagAndPush(beatName: 'elastic-agent', arch: arch)
289+
project = [ name: 'elastic-agent',
290+
variants: [
291+
'' : 'beats',
292+
'-oss' : 'beats',
293+
'-ubi8' : 'beats',
294+
'-complete' : 'beats',
295+
'-cloud' : 'beats-ci'
296+
]
297+
]
285298
}
299+
tagAndPush(arch: arch, project: project)
286300
}
287301
}
288302

@@ -291,77 +305,15 @@ def pushCIDockerImages(Map args = [:]) {
291305
* @param arch what architecture
292306
*/
293307
def tagAndPush(Map args = [:]) {
294-
def beatName = args.beatName
295-
def arch = args.get('arch', 'amd64')
296-
def libbetaVer = env.BEAT_VERSION
297-
def aliasVersion = ""
298-
if("${env.SNAPSHOT}" == "true"){
299-
aliasVersion = libbetaVer.substring(0, libbetaVer.lastIndexOf(".")) // remove third number in version
300-
301-
libbetaVer += "-SNAPSHOT"
302-
aliasVersion += "-SNAPSHOT"
303-
}
304-
305-
def tagName = "${libbetaVer}"
306-
if (isPR()) {
307-
tagName = "pr-${env.CHANGE_ID}"
308-
}
309-
310-
// supported tags
311-
def tags = [tagName, "${env.GIT_BASE_COMMIT}"]
312-
if (!isPR() && aliasVersion != "") {
313-
tags << aliasVersion
314-
}
315-
// supported image flavours
316-
def variants = ["", "-oss", "-ubi8"]
317-
318-
if(beatName == 'elastic-agent'){
319-
variants.add("-complete")
320-
variants.add("-cloud")
321-
}
322-
323-
variants.each { variant ->
324-
// cloud docker images are stored in the private docker namespace.
325-
def sourceNamespace = variant.equals('-cloud') ? 'beats-ci' : 'beats'
326-
tags.each { tag ->
327-
// TODO:
328-
// For backward compatibility let's ensure we tag only for amd64, then E2E can benefit from until
329-
// they support the versioning with the architecture
330-
if ("${arch}" == "amd64") {
331-
doTagAndPush(beatName: beatName, variant: variant, sourceTag: libbetaVer, targetTag: "${tag}", sourceNamespace: sourceNamespace)
332-
}
333-
doTagAndPush(beatName: beatName, variant: variant, sourceTag: libbetaVer, targetTag: "${tag}-${arch}", sourceNamespace: sourceNamespace)
334-
}
335-
}
336-
}
337-
338-
/**
339-
* @param beatName name of the Beat
340-
* @param variant name of the variant used to build the docker image name
341-
* @param sourceNamespace namespace to be used as source for the docker tag command
342-
* @param sourceTag tag to be used as source for the docker tag command, usually under the 'beats' namespace
343-
* @param targetTag tag to be used as target for the docker tag command, usually under the 'observability-ci' namespace
344-
*/
345-
def doTagAndPush(Map args = [:]) {
346-
def beatName = args.beatName
347-
def variant = args.variant
348-
def sourceTag = args.sourceTag
349-
def targetTag = args.targetTag
350-
def sourceNamespace = args.sourceNamespace
351-
def sourceName = "${DOCKER_REGISTRY}/${sourceNamespace}/${beatName}${variant}:${sourceTag}"
352-
def targetName = "${DOCKER_REGISTRY}/observability-ci/${beatName}${variant}:${targetTag}"
353-
def iterations = 0
354-
retryWithSleep(retries: 3, seconds: 5, backoff: true) {
355-
iterations++
356-
def status = sh(label: "Change tag and push ${targetName}",
357-
script: ".ci/scripts/docker-tag-push.sh ${sourceName} ${targetName}",
358-
returnStatus: true)
359-
if ( status > 0 && iterations < 3) {
360-
error("tag and push failed for ${beatName}, retry")
361-
} else if ( status > 0 ) {
362-
log(level: 'WARN', text: "${beatName} doesn't have ${variant} docker images. See https://github.com/elastic/beats/pull/21621")
363-
}
364-
}
308+
pushDockerImages(
309+
secret: env.DOCKERELASTIC_SECRET,
310+
registry: env.DOCKER_REGISTRY,
311+
arch: args.arch,
312+
version: env.BEAT_VERSION,
313+
snapshot: env.SNAPSHOT,
314+
project: args.project,
315+
targetNamespace: 'observability-ci'
316+
)
365317
}
366318

367319
def prepareE2ETestForPackage(String beat){

0 commit comments

Comments
 (0)