use goreleaser in cross-build image to emulate CI more closely

This commit is contained in:
Kenneth Bingham 2024-07-24 15:17:18 -04:00
parent e1496baa18
commit efbe3df4c5
No known key found for this signature in database
GPG Key ID: 31709281860130B6
2 changed files with 37 additions and 68 deletions

View File

@ -1,5 +1,8 @@
# Stage 1: Install Node.js with nvm
FROM debian:bullseye-slim
FROM goreleaser/goreleaser AS goreleaser
FROM golang:1.21 AS golang
FROM debian:bookworm-slim
#
# this file mirrors the build params used in the GitHub Actions and enables
# reproducible builds for downstream forks for Ziti contributors
@ -7,38 +10,38 @@ FROM debian:bullseye-slim
ARG TARGETARCH
ARG golang_version=1.21.3
ARG go_distribution_file=go${golang_version}.linux-${TARGETARCH}.tar.gz
ARG go_path=/usr/share/go
ARG go_root=/usr/local/go
ARG go_cache=/usr/share/go_cache
ARG uid=1000
ARG gid=1000
ARG go_cache=/usr/share/go
RUN apt-get -y update \
&& apt-get -y install \
gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf gcc-aarch64-linux-gnu \
wget build-essential
gcc-arm-linux-gnueabi \
gcc-aarch64-linux-gnu \
wget \
git \
build-essential
RUN wget -q https://go.dev/dl/${go_distribution_file}
RUN tar -xzf ${go_distribution_file} -C /usr/local/
RUN wget -qO- https://deb.nodesource.com/setup_18.x | bash \
&& apt-get -y update \
&& apt-get -y install \
nodejs
RUN mkdir ${go_path} ${go_cache}
RUN chown -R ${uid}:${gid} ${go_path} ${go_cache}
COPY ./linux-build.sh /usr/local/bin/
COPY --from=golang /usr/local/go /usr/local/go
# RUN chmod -R go+rX ${go_path} ${go_cache}
COPY --from=goreleaser /usr/bin/goreleaser /usr/local/bin/goreleaser
COPY ./linux-build.sh /usr/local/bin/linux-build.sh
USER ${uid}:${gid}
ENV TARGETARCH=${TARGETARCH}
ENV GOPATH=${go_path}
ENV GOROOT=${go_root}
ENV GOCACHE=${go_cache}
ENV PATH=${go_path}/bin:${go_root}/bin:$PATH
RUN go install github.com/mitchellh/gox@latest
WORKDIR /mnt
ENTRYPOINT ["linux-build.sh"]

View File

@ -11,6 +11,19 @@ set -o nounset
set -o pipefail
set -o xtrace
resolveArch() {
case ${1} in
amd64) echo amd64
;;
arm|armv7*|arm/v7*|armhf*) echo armhf
;;
arm64|armv8*|arm/v8*) echo arm64
;;
*) echo ${1}
;;
esac
}
# if no architectures supplied then default list of three
if (( ${#} )); then
typeset -a JOBS=(${@})
@ -18,22 +31,6 @@ else
typeset -a JOBS=(amd64 arm arm64)
fi
# specify the Go template used by Gox to save the artifacts
GOX_OUTPUT="dist/{{.Arch}}/{{.OS}}/{{.Dir}}"
# count the number of available CPUs for time-efficient parallelism
PROC_COUNT=$(nproc --all)
# compute the number of processors available for each job, rounded down to integer
PROCS_PER_JOB=$((PROC_COUNT / ${#JOBS[@]}))
# if multiple jobs and at least one processor for each job then background, else foreground with all available CPUs-1 (gox default)
if (( ${#JOBS[@]} > 1 && ${PROCS_PER_JOB} )); then
BACKGROUND="&"
# initialize an associative array in which to map background PIDs to the ARCH being built
typeset -A BUILDS
else
BACKGROUND="" # run normally in foreground
PROCS_PER_JOB=0 # invokes gox default to use all CPUs-1
fi
(
HOME=/tmp/builder
# Navigate to the "ui" directory and run npm commands
@ -44,42 +41,11 @@ fi
npm run build
)
for ARCH in ${JOBS[@]}; do
GOX_CMD="
gox \
-cgo \
-os=linux \
-arch=${ARCH} \
-output=${GOX_OUTPUT} \
-parallel=${PROCS_PER_JOB} \
./cmd/zrok
"
case ${ARCH} in
amd64) eval ${GOX_CMD} ${BACKGROUND}
(( ${PROCS_PER_JOB} )) && BUILDS[${!}]=${ARCH} # if greater than zero procs per job then map background pid->arch
;;
arm) eval CC=arm-linux-gnueabihf-gcc ${GOX_CMD} ${BACKGROUND}
(( ${PROCS_PER_JOB} )) && BUILDS[${!}]=${ARCH}
;;
arm64) eval CC=aarch64-linux-gnu-gcc ${GOX_CMD} ${BACKGROUND}
(( ${PROCS_PER_JOB} )) && BUILDS[${!}]=${ARCH}
;;
*) echo "ERROR: invalid architecture '${ARCH}', must be one of amd64, arm, arm64" >&2
exit 1
;;
esac
for ARCH in "${JOBS[@]}"; do
goreleaser build \
--clean \
--snapshot \
--output ./dist/ \
--config "./.goreleaser-linux-$(resolveArch "${ARCH}").yml"
done
# if not background in parallel then exit now with well earned success
[[ -z "${BACKGROUND:-}" ]] && exit 0
# Wait for builds in the background and exit with an error if any fail
EXIT=0
while true; do
# "wait -p" requires BASH >=5.1 which is present in Ubuntu 20.10 and Debian Bullseye
wait -n -p JOB_PID; JOB_RESULT=$?
echo "Building for ${BUILDS[$JOB_PID]} finished with result ${JOB_RESULT}"
(( ${JOB_RESULT} )) && EXIT=1
done
exit ${EXIT}