Skip to content

Commit df82808

Browse files
authored
Use go cross-compilation to speed up test builds on CI (#2210)
After adding Arm runners on GHA, we made it so the Arm test image is always built. Unfortunately, because we use QEMU to build the images, the build has gotten quite slow. In order to speed up the build, this change makes it so we cross-compile the binaries for all our supported platforms locally and the image build simply copies the binaries into the final image.
1 parent dd36a83 commit df82808

File tree

4 files changed

+37
-64
lines changed

4 files changed

+37
-64
lines changed

.github/workflows/integration-test-containers.yml

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ jobs:
8080
- name: Setup Docker buildx
8181
uses: docker/setup-buildx-action@v3
8282

83+
- name: Setup go and its cache
84+
uses: actions/setup-go@v5
85+
with:
86+
go-version-file: integration-tests/go.mod
87+
cache-dependency-path: |
88+
integration-tests/go.sum
89+
8390
- name: Create Ansible Vars (inc. Secrets)
8491
run: |
8592
cat << EOF > ${{ github.workspace }}/ansible/secrets.yml
@@ -105,28 +112,6 @@ jobs:
105112
echo "COLLECTOR_TESTS_TAG=${COLLECTOR_TESTS_TAG}" >> "$GITHUB_ENV"
106113
echo "collector-tests-tag=${COLLECTOR_TESTS_TAG}" >> "$GITHUB_OUTPUT"
107114
108-
- name: Check if multiarch is needed
109-
run: |
110-
BUILD_MULTI_ARCH="false"
111-
112-
if [[ "${GITHUB_EVENT_NAME}" != "pull_request" ]]; then
113-
BUILD_MULTI_ARCH="true"
114-
fi
115-
116-
if [[ "${{ inputs.is-konflux }}" == "true" ]]; then
117-
BUILD_MULTI_ARCH="true"
118-
fi
119-
120-
if [[ "${{ contains(github.event.pull_request.labels.*.name, 'run-multiarch-builds') }}" == "true" ]]; then
121-
BUILD_MULTI_ARCH="true"
122-
fi
123-
124-
if [[ "${{ contains(github.event.pull_request.labels.*.name, 'run-cpaas-steps') }}" == "true" ]]; then
125-
BUILD_MULTI_ARCH="true"
126-
fi
127-
128-
echo "BUILD_MULTI_ARCH=${BUILD_MULTI_ARCH}" >> "$GITHUB_ENV"
129-
130115
- name: Build images
131116
run: |
132117
ansible-galaxy install -r ansible/requirements.yml
@@ -135,7 +120,6 @@ jobs:
135120
ansible-playbook \
136121
--connection local -i localhost, --limit localhost \
137122
-e test_image="quay.io/rhacs-eng/collector-tests:${COLLECTOR_TESTS_TAG}" \
138-
-e "{\"build_multi_arch\": $BUILD_MULTI_ARCH}" \
139123
-e @'${{ github.workspace }}/ansible/secrets.yml' \
140124
ansible/ci-build-tests.yml
141125

ansible/ci-build-tests.yml

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@
55
environment:
66

77
tasks:
8-
- set_fact:
9-
platforms: "linux/amd64,linux/ppc64le,linux/s390x,linux/arm64"
10-
when: build_multi_arch
11-
12-
- set_fact:
13-
platforms: "linux/amd64,linux/arm64"
14-
when: not build_multi_arch
15-
168
- set_fact:
179
collector_root: "{{ lookup('env', 'GITHUB_WORKSPACE') }}"
1810

11+
- name: Build test binaries
12+
community.general.make:
13+
chdir: "{{ collector_root }}/integration-tests"
14+
target: build-all
15+
jobs: "{{ ansible_facts['processor_vcpus'] }}"
16+
1917
- name: Login to quay.io
2018
community.docker.docker_login:
2119
registry_url: quay.io
@@ -31,7 +29,7 @@
3129
# - we can push the images and manifest in a single command. The make
3230
# target can be used for local development and testing.
3331
docker buildx build --push \
34-
--platform "{{ platforms }}" \
32+
--platform "linux/amd64,linux/arm64,linux/ppc64le,linux/s390x" \
3533
-t '{{ test_image }}' \
3634
{{ collector_root }}/integration-tests
3735
register: build_result
@@ -45,4 +43,3 @@
4543
registry_url: quay.io
4644
state: absent
4745
when: true
48-

integration-tests/Dockerfile

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,15 @@
1-
ARG TEST_ROOT="/tests"
2-
3-
FROM golang:1.23 as builder
1+
FROM alpine:3.18
42

5-
ARG TEST_ROOT
3+
ARG TARGETARCH
4+
ARG TEST_ROOT="/tests"
65

7-
ENV GOCACHE=/root/.cache/go-build
6+
RUN apk add docker && \
7+
mkdir -p $TEST_ROOT
88

9-
RUN mkdir -p "$TEST_ROOT"
109
WORKDIR "$TEST_ROOT"
1110

12-
# Cache dependencies
13-
COPY go.* "$TEST_ROOT"
14-
RUN go mod download
15-
16-
COPY suites "$TEST_ROOT/suites/"
17-
COPY pkg "$TEST_ROOT/pkg/"
18-
COPY integration_test.go "$TEST_ROOT"
19-
COPY benchmark_test.go "$TEST_ROOT"
20-
COPY k8s_test.go "$TEST_ROOT"
21-
22-
RUN --mount=type=cache,target="/root/.cache/go-build" CGO_ENABLED=0 GOOS=linux GOARCH=$TARGETARCH go test -tags bench,k8s -c -o collector-tests
11+
COPY bin/$TARGETARCH/collector-tests /usr/local/bin
2312

24-
FROM alpine:3.18
25-
26-
ARG TEST_ROOT
27-
28-
RUN apk add docker
29-
30-
COPY --from=builder $TEST_ROOT/collector-tests $TEST_ROOT/collector-tests
31-
COPY images.yml "$TEST_ROOT"
32-
33-
WORKDIR "$TEST_ROOT"
13+
COPY images.yml .
3414

35-
ENTRYPOINT ["./collector-tests"]
15+
ENTRYPOINT ["collector-tests"]

integration-tests/Makefile

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ ifeq ($(COLLECTOR_TESTS_IMAGE),)
2121
COLLECTOR_TESTS_IMAGE=$(COLLECTOR_TESTS_REPO):$(COLLECTOR_TAG)
2222
endif
2323

24+
GOARCH ?= $(shell go env GOARCH)
25+
2426
SHELL=/bin/bash
2527

2628
# Environment variable COLLECTOR_IMAGE is used by integration-tests
@@ -59,11 +61,21 @@ $(foreach element,$(ALL_TESTS),$(eval $(call make-test-target-dockerized,$(eleme
5961

6062
.PHONY: build
6163
build:
62-
mkdir -p bin
63-
go test -tags bench,k8s -c -o bin/collector-tests
64+
mkdir -p bin/$(GOARCH)
65+
CGO_ENABLED=0 GOOS=linux GOARCH=$(GOARCH) go test -tags bench,k8s -c -o bin/$(GOARCH)/collector-tests
66+
67+
ARCHS := amd64 arm64 ppc64le s390x
68+
69+
build-%:
70+
GOARCH=$(*) make build
71+
72+
73+
build-all: $(addprefix build-,$(ARCHS))
74+
75+
.PHONY: build-all build-%
6476

6577
.PHONY: build-image
66-
build-image:
78+
build-image: build-all
6779
docker build --platform $(PLATFORM) -t $(COLLECTOR_TESTS_IMAGE) \
6880
--build-arg QA_TAG=$(shell cat container/QA_TAG) \
6981
$(CURDIR)

0 commit comments

Comments
 (0)