Skip to content

Commit d838639

Browse files
Add multiplatform images (elastic#5075)
Fixes issue where change from make to mage, result in the loss of creating multiple platform images.
1 parent 0f961a6 commit d838639

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
@@ -99,6 +99,7 @@ const (
9999
dockerBuilderName = "fleet-server-builder"
100100
dockerImage = "docker.elastic.co/beats-ci/elastic-agent-cloud-fleet"
101101
dockerAgentImage = "fleet-server-e2e-agent"
102+
dockerFleetImage = "docker.elastic.co/observability-ci/fleet-server"
102103
)
103104

104105
// e2e test certs
@@ -126,6 +127,12 @@ var (
126127
"linux/arm64",
127128
}
128129

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

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

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

0 commit comments

Comments
 (0)