From 52d6d30a76fec38a7865a762afee4e7b08b006e2 Mon Sep 17 00:00:00 2001 From: Kuniwak Date: Fri, 19 Apr 2024 10:12:49 +0900 Subject: [PATCH] Add builds for ARM64 --- .circleci/images/Dockerfile | 11 +----- .github/images/Dockerfile | 9 ++--- Dockerfile | 11 +----- Makefile | 78 ++++++++++++++++++++++++++++++++++++- build.Dockerfile | 15 +++++++ go.mod | 2 +- scripts/build-image | 24 ++++++++++-- scripts/deploy-gh-release | 16 +++++++- 8 files changed, 134 insertions(+), 32 deletions(-) create mode 100644 build.Dockerfile diff --git a/.circleci/images/Dockerfile b/.circleci/images/Dockerfile index b856d62..a6d56de 100644 --- a/.circleci/images/Dockerfile +++ b/.circleci/images/Dockerfile @@ -1,14 +1,7 @@ # image-name: ghcr.io/dena/unity-meta-check/unity-meta-check-circleci -FROM golang:1.19-buster as builder -WORKDIR /go/src/unity-meta-check -COPY . . -RUN make out/unity-meta-check-linux-amd64 out/unity-meta-check-junit-linux-amd64 out/unity-meta-check-github-pr-comment-linux-amd64 out/unity-meta-autofix-linux-amd64 && \ - mv ./out/unity-meta-check-linux-amd64 ./out/unity-meta-check && \ - mv ./out/unity-meta-check-junit-linux-amd64 ./out/unity-meta-check-junit && \ - mv ./out/unity-meta-check-github-pr-comment-linux-amd64 ./out/unity-meta-check-github-pr-comment && \ - mv ./out/unity-meta-autofix-linux-amd64 ./out/unity-meta-autofix +FROM ghcr.io/dena/unity-meta-check/unity-meta-check-builder:latest as builder -FROM debian:buster-slim +FROM debian:bookworm-slim # https://circleci.com/docs/2.0/custom-images/#required-tools-for-primary-containers RUN apt-get update \ && apt-get install --yes --no-install-recommends git openssh-server tar gzip ca-certificates \ diff --git a/.github/images/Dockerfile b/.github/images/Dockerfile index a7424bc..0aa0b27 100644 --- a/.github/images/Dockerfile +++ b/.github/images/Dockerfile @@ -1,14 +1,11 @@ # image-name: ghcr.io/dena/unity-meta-check/unity-meta-check-gh-action -FROM golang:1.19-buster as builder -WORKDIR /go/src/unity-meta-check -COPY . . -RUN make out/gh-action-linux-amd64 && mv ./out/gh-action-linux-amd64 ./out/gh-action +FROM ghcr.io/dena/unity-meta-check/unity-meta-check-builder:latest as builder -FROM debian:buster-slim +FROM debian:bookworm-slim RUN apt-get update \ && apt-get install --yes --no-install-recommends git ca-certificates \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* -COPY --from=builder /go/src/unity-meta-check/out/* /usr/bin/ +COPY --from=builder /go/src/unity-meta-check/out/gh-action /usr/bin/ ENTRYPOINT ["gh-action"] CMD ["-help"] diff --git a/Dockerfile b/Dockerfile index 6ed3616..267d3ee 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,7 @@ # image-name: ghcr.io/dena/unity-meta-check/unity-meta-check -FROM golang:1.19-buster as builder -WORKDIR /go/src/unity-meta-check -COPY . . -RUN make out/unity-meta-check-linux-amd64 out/unity-meta-check-junit-linux-amd64 out/unity-meta-check-github-pr-comment-linux-amd64 out/unity-meta-autofix-linux-amd64 && \ - mv ./out/unity-meta-check-linux-amd64 ./out/unity-meta-check && \ - mv ./out/unity-meta-check-junit-linux-amd64 ./out/unity-meta-check-junit && \ - mv ./out/unity-meta-check-github-pr-comment-linux-amd64 ./out/unity-meta-check-github-pr-comment && \ - mv ./out/unity-meta-autofix-linux-amd64 ./out/unity-meta-autofix +FROM ghcr.io/dena/unity-meta-check/unity-meta-check-builder:latest as builder -FROM debian:buster-slim +FROM debian:bookworm-slim RUN apt-get update \ && apt-get install --yes --no-install-recommends git \ && apt-get clean \ diff --git a/Makefile b/Makefile index a4a38db..d681947 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -all: darwin-amd64 linux-amd64 windows-amd64 +all: darwin-amd64 linux-amd64 windows-amd64 darwin-arm64 linux-arm64 windows-arm64 clean: @@ -38,6 +38,30 @@ out/gh-action-darwin-amd64: out CGO_ENABLED=0 GOARCH=amd64 GOOS=darwin go build -v -o "$@" ./tool/gh-action +darwin-arm64: out/unity-meta-check-darwin-arm64 out/unity-meta-check-junit-darwin-arm64 out/unity-meta-check-github-pr-comment-darwin-arm64 out/unity-meta-autofix-darwin-arm64 out/gh-action-yaml-gen-darwin-arm64 + +out/unity-meta-check-darwin-arm64: out + CGO_ENABLED=0 GOARCH=arm64 GOOS=darwin go build -v -o "$@" + +out/unity-meta-check-junit-darwin-arm64: out + CGO_ENABLED=0 GOARCH=arm64 GOOS=darwin go build -v -o "$@" ./tool/unity-meta-check-junit + +out/unity-meta-check-github-pr-comment-darwin-arm64: out + GOARCH=arm64 GOOS=darwin go build -a -tags netgo -installsuffix netgo -v -o "$@" ./tool/unity-meta-check-github-pr-comment + +out/unity-meta-check-meta-audit-darwin-arm64: out + CGO_ENABLED=0 GOARCH=arm64 GOOS=darwin go build -v -o "$@" ./tool/unity-meta-check-meta-audit + +out/unity-meta-autofix-darwin-arm64: out + CGO_ENABLED=0 GOARCH=arm64 GOOS=darwin go build -v -o "$@" ./tool/unity-meta-autofix + +out/gh-action-yaml-gen-darwin-arm64: out + CGO_ENABLED=0 GOARCH=arm64 GOOS=darwin go build -v -o "$@" ./tool/gh-action/action-yaml-gen + +out/gh-action-darwin-arm64: out + CGO_ENABLED=0 GOARCH=arm64 GOOS=darwin go build -v -o "$@" ./tool/gh-action + + # NOTE: meta-audit は Debug 用ツールなので必要になったら生成してください(生成されちゃうと Releases へあげるときに間引かないといけなくてめんどい) linux-amd64: out/unity-meta-check-linux-amd64 out/unity-meta-check-junit-linux-amd64 out/unity-meta-check-github-pr-comment-linux-amd64 out/unity-meta-autofix-linux-amd64 out/gh-action-yaml-gen-linux-amd64 out/gh-action-linux-amd64 @@ -63,6 +87,31 @@ out/gh-action-linux-amd64: out CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -v -o "$@" ./tool/gh-action +# NOTE: meta-audit は Debug 用ツールなので必要になったら生成してください(生成されちゃうと Releases へあげるときに間引かないといけなくてめんどい) +linux-arm64: out/unity-meta-check-linux-arm64 out/unity-meta-check-junit-linux-arm64 out/unity-meta-check-github-pr-comment-linux-arm64 out/unity-meta-autofix-linux-arm64 out/gh-action-yaml-gen-linux-arm64 out/gh-action-linux-arm64 + +out/unity-meta-check-linux-arm64: out + CGO_ENABLED=0 GOARCH=arm64 GOOS=linux go build -v -o "$@" + +out/unity-meta-check-junit-linux-arm64: out + CGO_ENABLED=0 GOARCH=arm64 GOOS=linux go build -v -o "$@" ./tool/unity-meta-check-junit + +out/unity-meta-check-github-pr-comment-linux-arm64: out + GOARCH=arm64 GOOS=linux go build -a -tags netgo -installsuffix netgo -v -o "$@" ./tool/unity-meta-check-github-pr-comment + +out/unity-meta-check-meta-audit-linux-arm64: out + CGO_ENABLED=0 GOARCH=arm64 GOOS=linux go build -v -o "$@" ./tool/unity-meta-check-meta-audit + +out/unity-meta-autofix-linux-arm64: out + CGO_ENABLED=0 GOARCH=arm64 GOOS=linux go build -v -o "$@" ./tool/unity-meta-autofix + +out/gh-action-yaml-gen-linux-arm64: out + CGO_ENABLED=0 GOARCH=arm64 GOOS=linux go build -v -o "$@" ./tool/gh-action/action-yaml-gen + +out/gh-action-linux-arm64: out + CGO_ENABLED=0 GOARCH=arm64 GOOS=linux go build -v -o "$@" ./tool/gh-action + + # NOTE: meta-audit は Debug 用ツールなので必要になったら生成してください(生成されちゃうと Releases へあげるときに間引かないといけなくてめんどい) windows-amd64: out/unity-meta-check-windows-amd64.exe out/unity-meta-check-junit-windows-amd64.exe out/unity-meta-check-github-pr-comment-windows-amd64.exe out/unity-meta-autofix-windows-amd64.exe out/gh-action-yaml-gen-windows-amd64.exe @@ -88,4 +137,29 @@ out/gh-action-windows-amd64.exe: out CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -v -o "$@" ./tool/gh-action -.PHONY: all test clean darwin-amd64 linux-amd64 windows-amd64 +# NOTE: meta-audit は Debug 用ツールなので必要になったら生成してください(生成されちゃうと Releases へあげるときに間引かないといけなくてめんどい) +windows-arm64: out/unity-meta-check-windows-arm64.exe out/unity-meta-check-junit-windows-arm64.exe out/unity-meta-check-github-pr-comment-windows-arm64.exe out/unity-meta-autofix-windows-arm64.exe out/gh-action-yaml-gen-windows-arm64.exe + +out/unity-meta-check-windows-arm64.exe: out + CGO_ENABLED=0 GOARCH=arm64 GOOS=windows go build -v -o "$@" + +out/unity-meta-check-junit-windows-arm64.exe: out + CGO_ENABLED=0 GOARCH=arm64 GOOS=windows go build -v -o "$@" ./tool/unity-meta-check-junit + +out/unity-meta-check-github-pr-comment-windows-arm64.exe: out + GOARCH=arm64 GOOS=windows go build -a -tags netgo -installsuffix netgo -v -o "$@" ./tool/unity-meta-check-github-pr-comment + +out/unity-meta-check-meta-audit-windows-arm64.exe: out + CGO_ENABLED=0 GOARCH=arm64 GOOS=windows go build -v -o "$@" ./tool/unity-meta-check-meta-audit + +out/unity-meta-autofix-windows-arm64.exe: out + CGO_ENABLED=0 GOARCH=arm64 GOOS=windows go build -v -o "$@" ./tool/unity-meta-autofix + +out/gh-action-yaml-gen-windows-arm64.exe: out + CGO_ENABLED=0 GOARCH=arm64 GOOS=windows go build -v -o "$@" ./tool/gh-action/action-yaml-gen + +out/gh-action-windows-arm64.exe: out + CGO_ENABLED=0 GOARCH=arm64 GOOS=linux go build -v -o "$@" ./tool/gh-action + + +.PHONY: all test clean darwin-amd64 linux-amd64 windows-amd64 darwin-arm64 linux-arm64 windows-arm64 diff --git a/build.Dockerfile b/build.Dockerfile new file mode 100644 index 0000000..885f831 --- /dev/null +++ b/build.Dockerfile @@ -0,0 +1,15 @@ +# image-name: ghcr.io/dena/unity-meta-check/unity-meta-check-builder +FROM golang:1.22.2-bookworm as builder +ARG TARGETARCH +WORKDIR /go/src/unity-meta-check +COPY . . +RUN make -j$(nproc) out/unity-meta-check-linux-${TARGETARCH} \ + out/unity-meta-check-junit-linux-${TARGETARCH} \ + out/unity-meta-check-github-pr-comment-linux-${TARGETARCH} \ + out/unity-meta-autofix-linux-${TARGETARCH} \ + out/gh-action-linux-${TARGETARCH} && \ + mv ./out/unity-meta-check-linux-${TARGETARCH} ./out/unity-meta-check && \ + mv ./out/unity-meta-check-junit-linux-${TARGETARCH} ./out/unity-meta-check-junit && \ + mv ./out/unity-meta-check-github-pr-comment-linux-${TARGETARCH} ./out/unity-meta-check-github-pr-comment && \ + mv ./out/unity-meta-autofix-linux-${TARGETARCH} ./out/unity-meta-autofix && \ + mv ./out/gh-action-linux-${TARGETARCH} ./out/gh-action diff --git a/go.mod b/go.mod index e827533..b144fc5 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/DeNA/unity-meta-check -go 1.18 +go 1.22.2 require ( github.com/google/go-cmp v0.5.5 diff --git a/scripts/build-image b/scripts/build-image index 707e1b0..5394be0 100755 --- a/scripts/build-image +++ b/scripts/build-image @@ -1,7 +1,8 @@ #!/bin/bash -set -euo pipefail +set -euox pipefail BASE_DIR="$(cd "$(dirname "$0")/.."; pwd)" +BUILDER_DOCKERFILE="${BASE_DIR}/build.Dockerfile" DOCKERFILE="${BASE_DIR}/Dockerfile" GH_ACTION_DOCKERFILE="${BASE_DIR}/.github/images/Dockerfile" CIRCLECI_DOCKERFILE="${BASE_DIR}/.circleci/images/Dockerfile" @@ -25,6 +26,9 @@ main() { local version version="$("$BASE_DIR/scripts/print-version")" + local builder_image + builder_image="$("$BASE_DIR/scripts/print-image-name" < "$BUILDER_DOCKERFILE")" + local circleci_image circleci_image="$("$BASE_DIR/scripts/print-image-name" < "$CIRCLECI_DOCKERFILE")" @@ -34,17 +38,29 @@ main() { local image image="$("$BASE_DIR/scripts/print-image-name" < "$DOCKERFILE")" - docker build --platform linux/amd64 -t "$image:$version" -f "$DOCKERFILE" . + # NOTE: Create a docker-container driver to avoid the following error: + # > ERROR: Multi-platform build is not supported for the docker driver. + # > Switch to a different driver, or turn on the containerd image store, and try again. + # > Learn more at https://docs.docker.com/go/build-multi-platform/ + if ! docker buildx inspect unity-meta-check-driver; then + docker run --privileged --rm tonistiigi/binfmt --install all + docker buildx create --name unity-meta-check-driver --driver docker-container --bootstrap + fi + + # NOTE: --push is necessary, because --load is not supported for multi-platform build yet. + docker buildx build --builder unity-meta-check-driver --platform linux/arm64,linux/amd64 -t "$builder_image:latest" -f "$BUILDER_DOCKERFILE" . --push + + docker buildx build --builder unity-meta-check-driver --platform linux/arm64,linux/amd64 -t "$image:$version" -f "$DOCKERFILE" . if [[ "$arg" == "--latest" ]]; then docker tag "$image:$version" "$image:latest" fi - docker build --platform linux/amd64 -t "$gh_action_image:$version" -f "$GH_ACTION_DOCKERFILE" . + docker buildx build --builder unity-meta-check-driver --platform linux/arm64,linux/amd64 -t "$gh_action_image:$version" -f "$GH_ACTION_DOCKERFILE" . if [[ "$arg" == "--latest" ]]; then docker tag "$gh_action_image:$version" "$gh_action_image:latest" fi - docker build --platform linux/amd64 -t "$circleci_image:$version" -f "$CIRCLECI_DOCKERFILE" . + docker buildx build --builder unity-meta-check-driver --platform linux/arm64,linux/amd64 -t "$circleci_image:$version" -f "$CIRCLECI_DOCKERFILE" . if [[ "$arg" == "--latest" ]]; then docker tag "$circleci_image:$version" "$circleci_image:latest" fi diff --git a/scripts/deploy-gh-release b/scripts/deploy-gh-release index 86441d2..835c439 100755 --- a/scripts/deploy-gh-release +++ b/scripts/deploy-gh-release @@ -4,6 +4,12 @@ set -euo pipefail BASE_DIR="$(cd "$(dirname "$0")/.."; pwd)" +has() { + local cmd="$1" + which "$cmd" >/dev/null 2>&1 +} + + echo-stderr() { local message="$*" printf "%s\n" "$message" 1>&2 @@ -42,6 +48,14 @@ main() { gh --version || throw "gh must be available (see https://cli.github.com/)" gh auth status + local nproc=1 + if has sysctl; then + nproc="$(sysctl -n hw.logicalcpu)" + fi + if has nproc; then + nproc="$(nproc)" + fi + (cd "$BASE_DIR" local title title="$(git log -1 --format=%s HEAD)" @@ -70,7 +84,7 @@ main() { echo-stderr echo-stderr "building executables..." - make clean all + make -j"$nproc" clean all echo-stderr [[ -d "./out" ]] || throw "cannot find the generated distribution dir: '${BASE_DIR}/out'"