Skip to content

Commit 3e974fb

Browse files
committed
ci/github: Build images natively
Signed-off-by: Ryan Northey <[email protected]>
1 parent 067a362 commit 3e974fb

File tree

4 files changed

+83
-47
lines changed

4 files changed

+83
-47
lines changed

.github/workflows/_build_image.yml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@ on:
2323
type: string
2424
required: true
2525

26-
host_platform:
26+
host-platform:
2727
type: string
28-
default: ubuntu-22.04
28+
required: true
29+
target-platforms:
30+
type: string
31+
required: true
32+
runs-on:
33+
type: string
34+
required: true
2935

3036
go_version:
3137
type: string
@@ -38,14 +44,16 @@ on:
3844
type: string
3945
default:
4046

47+
4148
concurrency:
42-
group: ${{ github.workflow }}-${{ inputs.distro }}-${{ github.head_ref || github.run_id }}
49+
group: ${{ github.workflow }}-${{ inputs.distro }}-${{ inputs.host-platform }}-${{ github.head_ref || github.run_id }}
4350
cancel-in-progress: true
4451

4552
jobs:
4653
image:
47-
runs-on: ${{ inputs.host_platform }}
48-
name: Build and test Docker image
54+
name: |
55+
[${{ inputs.host-platform }}] Build and test Docker image
56+
runs-on: ${{ inputs.runs-on }}
4957
steps:
5058
- uses: envoyproxy/toolshed/gh-actions/[email protected]
5159
name: 'Free disk space'
@@ -63,6 +71,7 @@ jobs:
6371
name: Build (${{ inputs.image_prefix }}${{ inputs.distro }}@${{ steps.container.outputs.tag }})
6472
shell: bash
6573
env:
74+
BUILD_TOOLS_PLATFORMS: ${{ inputs.target-platforms }}
6675
OS_FAMILY: ${{ inputs.os_family }}
6776
SOURCE_BRANCH: ${{ github.ref }}
6877
DOCKERHUB_USERNAME: ${{ secrets.dockerhub_username }}

.github/workflows/build.yml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,18 @@ jobs:
2020
strategy:
2121
fail-fast: false
2222
matrix:
23-
include:
24-
- target: ubuntu
25-
name: Linux Ubuntu
26-
gcr_push: true
27-
- target: debian
28-
name: Linux Debian
29-
gcr_push: true
23+
target: [ubuntu, debian]
24+
arch: [amd64, arm64]
3025
name: Build (${{ matrix.name || matrix.target }})
3126
uses: ./.github/workflows/_build_image.yml
3227
with:
3328
distro: ${{ matrix.target }}
34-
gcr_push: ${{ matrix.gcr_push }}
29+
gcr_push: true
3530
gcr_name: ${{ matrix.gcr_name != '' && matrix.gcr_name || 'envoy-build' }}
3631
os_family: ${{ matrix.os_family != '' && matrix.os_family || 'linux' }}
37-
host_platform: ${{ matrix.host_platform != '' && matrix.host_platform || 'ubuntu-22.04' }}
32+
host-platform: ${{ matrix.arch }}
33+
runs-on: ${{ matrix.arch == 'amd64' && 'ubuntu-22.04' || 'ubuntu-22.04-arm' }}
34+
target-platforms: ${{ matrix.arch == 'amd64' && 'linux/amd64' || 'linux/arm64' }}
3835
secrets:
3936
dockerhub_username: ${{ secrets.DOCKERHUB_USERNAME }}
4037
dockerhub_password: ${{ secrets.DOCKERHUB_PASSWORD }}

docker/linux/build.sh

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@ UBUNTU_DOCKER_VARIANTS=("ci" "mobile" "test")
66
IMAGE_TAGS=${IMAGE_TAGS:-}
77

88

9-
# Setting environments for buildx tools
10-
config_env() {
11-
# Install QEMU emulators
12-
docker run --rm --privileged tonistiigi/binfmt --install all
13-
14-
# Remove older build instance
15-
docker buildx rm envoy-build-tools-builder &> /dev/null || :
16-
docker buildx create --use --name envoy-build-tools-builder --platform "${BUILD_TOOLS_PLATFORMS}"
17-
}
18-
199
[[ -z "${OS_DISTRO}" ]] && OS_DISTRO="ubuntu"
2010
[[ -z "${IMAGE_NAME}" ]] && IMAGE_NAME="envoyproxy/envoy-build-${OS_DISTRO}"
2111

@@ -27,7 +17,28 @@ if [[ -z "${BUILD_TOOLS_PLATFORMS}" ]]; then
2717
fi
2818
fi
2919

30-
ci_log_run config_env
20+
HOST_ARCH="$(uname -m)"
21+
MULTI_ARCH=
22+
CROSS_ARCH=
23+
24+
if [[ "$BUILD_TOOLS_PLATFORMS" == *","* ]]; then
25+
MULTI_ARCH=true
26+
elif [[ "$HOST_ARCH" == "x86_64" && "$BUILD_TOOLS_PLATFORMS" != "linux/amd64" ]]; then
27+
CROSS_ARCH=true
28+
elif [[ "$HOST_ARCH" == "aarch64" && "$BUILD_TOOLS_PLATFORMS" != "linux/arm64" ]]; then
29+
CROSS_ARCH=true
30+
fi
31+
32+
# Setting environments for buildx tools
33+
config_env() {
34+
if [[ -n "$MULTI_ARCH" || -n "$CROSS_ARCH" ]]; then
35+
# Install QEMU emulators
36+
docker run --rm --privileged tonistiigi/binfmt --install all
37+
fi
38+
# Remove older build instance
39+
docker buildx rm envoy-build-tools-builder &> /dev/null || :
40+
docker buildx create --use --name envoy-build-tools-builder --platform "${BUILD_TOOLS_PLATFORMS}"
41+
}
3142

3243
# TODO(phlax): add (json) build images config
3344
build_and_push_variants () {
@@ -44,10 +55,12 @@ build_and_push_variants () {
4455
fi
4556

4657
if [[ "$variant" == "test" || "$variant" == "ci" ]]; then
47-
platform="linux/amd64,linux/arm64"
48-
else
49-
# Only build variants for linux/amd64
58+
platform="$BUILD_TOOLS_PLATFORMS"
59+
elif [[ "$BUILD_TOOLS_PLATFORMS" == *"linux/amd64"* ]]; then
60+
# devtools and mobile are amd64 only (matching original behavior for full/mobile)
5061
platform="linux/amd64"
62+
else
63+
continue
5164
fi
5265
ci_log_run docker buildx build . \
5366
-f "${OS_DISTRO}/Dockerfile" \
@@ -58,6 +71,8 @@ build_and_push_variants () {
5871
done
5972
}
6073

74+
ci_log_run config_env
75+
6176
ci_log_run docker buildx build . -f "${OS_DISTRO}/Dockerfile" -t "${IMAGE_NAME}:${CONTAINER_TAG}" --target full --platform "${BUILD_TOOLS_PLATFORMS}"
6277

6378
if [[ -z "${NO_BUILD_VARIANTS}" ]]; then
@@ -76,7 +91,7 @@ if [[ -n "${IMAGE_TAGS}" ]]; then
7691
done
7792
fi
7893

79-
if [[ "$LOAD_IMAGE" == "true" ]]; then
94+
if [[ "$LOAD_IMAGE" == "true" && "$BUILD_TOOLS_PLATFORMS" == *"linux/amd64"* ]]; then
8095
# Testing after push to save CI time because this invalidates arm64 cache
8196
ci_log_run docker buildx build . -f "${OS_DISTRO}/Dockerfile" -t "${IMAGE_NAME}:${CONTAINER_TAG}" --platform "linux/amd64" --load
8297
fi

docker/linux/debian_build.sh

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,41 @@ DEBIAN_DOCKER_VARIANTS=("worker" "ci" "devtools" "mobile" "test")
77
IMAGE_TAGS=${IMAGE_TAGS:-}
88
OS_DISTRO="debian"
99

10-
# Setting environments for buildx tools
11-
config_env() {
12-
# Install QEMU emulators
13-
docker run --rm --privileged tonistiigi/binfmt --install all
14-
15-
# Remove older build instance
16-
docker buildx rm envoy-build-tools-builder &> /dev/null || :
17-
docker buildx create --use --name envoy-build-tools-builder --platform "${BUILD_TOOLS_PLATFORMS}"
18-
}
19-
2010
[[ -z "${IMAGE_NAME}" ]] && IMAGE_NAME="envoyproxy/envoy-build"
2111

2212
# Debian uses multi-arch builds
2313
if [[ -z "${BUILD_TOOLS_PLATFORMS}" ]]; then
2414
export BUILD_TOOLS_PLATFORMS=linux/arm64,linux/amd64
2515
fi
2616

27-
ci_log_run config_env
17+
HOST_ARCH="$(uname -m)"
18+
MULTI_ARCH=
19+
CROSS_ARCH=
20+
21+
if [[ "$BUILD_TOOLS_PLATFORMS" == *","* ]]; then
22+
MULTI_ARCH=true
23+
elif [[ "$HOST_ARCH" == "x86_64" && "$BUILD_TOOLS_PLATFORMS" != "linux/amd64" ]]; then
24+
CROSS_ARCH=true
25+
elif [[ "$HOST_ARCH" == "aarch64" && "$BUILD_TOOLS_PLATFORMS" != "linux/arm64" ]]; then
26+
CROSS_ARCH=true
27+
fi
28+
29+
# Setting environments for buildx tools
30+
config_env() {
31+
if [[ -n "$MULTI_ARCH" || -n "$CROSS_ARCH" ]]; then
32+
# Install QEMU emulators
33+
docker run --rm --privileged tonistiigi/binfmt --install all
34+
fi
35+
36+
# Remove older build instance
37+
docker buildx rm envoy-build-tools-builder &> /dev/null || :
38+
docker buildx create --use --name envoy-build-tools-builder --platform "${BUILD_TOOLS_PLATFORMS}"
39+
}
2840

2941
# Build Debian variants with specific platform logic
3042
build_and_push_variants () {
3143
local variant="" platform push_arg
32-
44+
3345
for variant in "${DEBIAN_DOCKER_VARIANTS[@]}"; do
3446
push_arg=()
3547
if [[ -n "${IMAGE_TAGS}" && "$variant" != "test" ]]; then
@@ -40,12 +52,13 @@ build_and_push_variants () {
4052

4153
# Platform logic: worker, ci and test get multi-arch, others get amd64 only
4254
if [[ "$variant" == "test" || "$variant" == "ci" || "$variant" == "worker" ]]; then
43-
platform="linux/amd64,linux/arm64"
44-
else
55+
platform="${BUILD_TOOLS_PLATFORMS}"
56+
elif [[ "$BUILD_TOOLS_PLATFORMS" == *"linux/amd64"* ]]; then
4557
# devtools and mobile are amd64 only (matching original behavior for full/mobile)
4658
platform="linux/amd64"
59+
else
60+
continue
4761
fi
48-
4962
ci_log_run docker buildx build . \
5063
-f "${OS_DISTRO}/Dockerfile" \
5164
-t "${IMAGE_NAME}:${variant}-${CONTAINER_TAG}" \
@@ -55,6 +68,9 @@ build_and_push_variants () {
5568
done
5669
}
5770

71+
72+
ci_log_run config_env
73+
5874
# Default target for Debian is 'ci' (includes bazel for builds)
5975
ci_log_run docker buildx build . -f "${OS_DISTRO}/Dockerfile" -t "${IMAGE_NAME}:${CONTAINER_TAG}" --target ci --platform "${BUILD_TOOLS_PLATFORMS}"
6076

@@ -75,7 +91,6 @@ if [[ -n "${IMAGE_TAGS}" ]]; then
7591
done
7692
fi
7793

78-
if [[ "$LOAD_IMAGE" == "true" ]]; then
79-
# Testing after push to save CI time because this invalidates arm64 cache
94+
if [[ "$LOAD_IMAGE" == "true" && "$BUILD_TOOLS_PLATFORMS" == *"linux/amd64"* ]]; then
8095
ci_log_run docker buildx build . -f "${OS_DISTRO}/Dockerfile" -t "${IMAGE_NAME}:${CONTAINER_TAG}" --target ci --platform "linux/amd64" --load
81-
fi
96+
fi

0 commit comments

Comments
 (0)