Skip to content

Commit ba6ab78

Browse files
michel-latermanmergify[bot]
authored andcommitted
Add multiplatform images (#5075)
Fixes issue where change from make to mage, result in the loss of creating multiple platform images. (cherry picked from commit 5c01471)
1 parent d547eb1 commit ba6ab78

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

.buildkite/scripts/build_push_docker_image.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ with_mage
1313
echo "Building the docker image..."
1414
if ! docker pull -q ${DOCKER_IMAGE}:${DOCKER_IMAGE_SHA_TAG} 2> /dev/null; then
1515
DOCKER_IMAGE_TAG="${DOCKER_IMAGE_SHA_TAG}"
16-
DOCKER_IMAGE=${DOCKER_IMAGE} DOCKER_IMAGE_TAG=${DOCKER_IMAGE_TAG} mage docker:image docker:push
16+
DOCKER_IMAGE=${DOCKER_IMAGE} DOCKER_IMAGE_TAG=${DOCKER_IMAGE_TAG} mage docker:publish
1717
fi
1818

1919
if [[ "${DOCKER_IMAGE_GIT_TAG}" == "main" ]]; then
20-
DOCKER_IMAGE=${DOCKER_IMAGE} DOCKER_IMAGE_TAG="${DOCKER_IMAGE_LATEST_TAG}" mage docker:image docker:push
20+
DOCKER_IMAGE=${DOCKER_IMAGE} DOCKER_IMAGE_TAG="${DOCKER_IMAGE_LATEST_TAG}" mage docker:publish
2121
elif [[ ${BUILDKITE_PULL_REQUEST} == "false" ]]; then
22-
DOCKER_IMAGE=${DOCKER_IMAGE} DOCKER_IMAGE_TAG="${DOCKER_IMAGE_GIT_TAG}" mage docker:image docker:push
22+
DOCKER_IMAGE=${DOCKER_IMAGE} DOCKER_IMAGE_TAG="${DOCKER_IMAGE_GIT_TAG}" mage docker:publish
2323
fi

magefile.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ const (
101101
dockerBuilderName = "fleet-server-builder"
102102
dockerImage = "docker.elastic.co/beats-ci/elastic-agent-cloud-fleet"
103103
dockerAgentImage = "fleet-server-e2e-agent"
104+
dockerFleetImage = "docker.elastic.co/observability-ci/fleet-server"
104105
)
105106

106107
// e2e test certs
@@ -128,6 +129,12 @@ var (
128129
"linux/arm64",
129130
}
130131

132+
// platformsDocker is the list of all platforms that are supported by docker multiplatform builds.
133+
platformsDocker = []string{
134+
"linux/amd64",
135+
"linux/arm64",
136+
}
137+
131138
// platformRemap contains mappings for platforms where if the GOOS/GOARCH key is used, artifacts should use the value instead. Missing keys are unalted.
132139
platformRemap = map[string]string{
133140
"darwin/amd64": "darwin/x86_64",
@@ -278,6 +285,26 @@ var (
278285
return list
279286
})
280287

288+
// getDockerPlatforms returns a list of supported docker multiplatform targets.
289+
getDockerPlatforms = sync.OnceValue(func() []string {
290+
list := platformsDocker
291+
if pList, ok := os.LookupEnv(envPlatforms); ok {
292+
filtered := make([]string, 0)
293+
for _, plat := range strings.Split(pList, ",") {
294+
if slices.Contains(list, plat) {
295+
filtered = append(filtered, plat)
296+
} else {
297+
log.Printf("Skipping %q platform is not in the list of allowed platforms.", plat)
298+
}
299+
}
300+
if len(filtered) > 0 {
301+
return filtered
302+
}
303+
log.Printf("%s env var detected but value %q does not contain valid platforms. Using default list.", envPlatforms, pList)
304+
}
305+
return list
306+
})
307+
281308
// isFIPS returns a bool indicator of the FIPS env var.
282309
isFIPS = sync.OnceValue(func() bool {
283310
return envToBool(envFIPS)
@@ -1002,6 +1029,49 @@ func (Docker) Image() error {
10021029
)
10031030
}
10041031

1032+
// Publish creates a multiplatform images and pushes them to the registry.
1033+
// The name of the image is docker.elastic.co/observability-ci/fleet-server by default.
1034+
// FIPS creates a FIPS capable image, adds the -fips suffix to the image name.
1035+
// DEV creates a development image.
1036+
// SNAPSHOT creates a snapshot image.
1037+
// VERSION_QUALIFIER may be used to manually specify a version qualifer for the image tag.
1038+
// DOCKER_IMAGE may be used to completely specify the image name.
1039+
// DOCKER_IMAGE_TAG may be used to completely specify the image tag.
1040+
// PLATFORMS may be used to specify multiplatform build targets. Defaults to [linux/amd64, linux/arm64].
1041+
func (Docker) Publish() error {
1042+
dockerFile := "Dockerfile"
1043+
image := dockerFleetImage
1044+
version := getVersion()
1045+
if v, ok := os.LookupEnv(envDockerTag); ok && v != "" {
1046+
version = v
1047+
}
1048+
if isFIPS() {
1049+
dockerFile = dockerBuilderFIPS
1050+
image += "-fips"
1051+
}
1052+
if v, ok := os.LookupEnv(envDockerImage); ok && v != "" {
1053+
image = v
1054+
}
1055+
dockerEnv := map[string]string{"DOCKER_BUILDKIT": "1"}
1056+
if err := sh.RunWithV(dockerEnv, "docker", "buildx", "create", "--use"); err != nil {
1057+
return fmt.Errorf("docker buildx create failed: %w", err)
1058+
}
1059+
1060+
return sh.RunWithV(dockerEnv, "docker", "buildx", "build", "--push",
1061+
"--platform", strings.Join(getDockerPlatforms(), ","),
1062+
"--build-arg", "GO_VERSION="+getGoVersion(),
1063+
"--build-arg", "DEV="+strconv.FormatBool(isDEV()),
1064+
"--build-arg", "FIPS="+strconv.FormatBool(isFIPS()),
1065+
"--build-arg", "SNAPSHOT="+strconv.FormatBool(isSnapshot()),
1066+
"--build-arg", "VERSION="+getVersion(),
1067+
"--build-arg", "GCFLAGS="+getGCFlags(),
1068+
"--build-arg", "LDFLAGS="+getLDFlags(),
1069+
"-f", dockerFile,
1070+
"-t", image+":"+version,
1071+
".",
1072+
)
1073+
}
1074+
10051075
// Push pushs an image created by docker:image to the registry.
10061076
// FIPS may be used to push a FIPS capable image.
10071077
// DOCKER_IMAGE may be used to specify the image name.

0 commit comments

Comments
 (0)