diff --git a/.github/workflows/build-and-release.yml b/.github/workflows/build-and-release.yml index ade2f08..978d0d6 100644 --- a/.github/workflows/build-and-release.yml +++ b/.github/workflows/build-and-release.yml @@ -12,36 +12,35 @@ jobs: runs-on: ubuntu-24.04 steps: - name: Check out code - uses: actions/checkout@master + uses: actions/checkout@v5 with: fetch-depth: 1 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 - name: Docker login uses: azure/docker-login@v1 with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - - name: Docker edge build & tag + - name: Docker edge build & push if: startsWith(github.ref, 'refs/tags/') != true && success() run: | - DOCKER_BUILDKIT=1 docker build --no-cache -t ${{ secrets.IMAGE_NAME }}:edge-latest . - docker tag ${{ secrets.IMAGE_NAME }}:edge-latest ${{ secrets.IMAGE_NAME }}:edge-${GITHUB_SHA::7} - - name: Docker edge push - if: startsWith(github.ref, 'refs/tags/') != true && success() - run: | - docker push ${{ secrets.IMAGE_NAME }}:edge-latest - docker push ${{ secrets.IMAGE_NAME }}:edge-${GITHUB_SHA::7} - - name: Docker stable build & tag - if: github.event_name != 'pull_request' && startsWith(github.ref, 'refs/tags/') && success() - run: | - DOCKER_BUILDKIT=1 docker build --no-cache -t ${{ secrets.IMAGE_NAME }}:stable-latest . - docker tag ${{ secrets.IMAGE_NAME }}:stable-latest ${{ secrets.IMAGE_NAME }}:stable-${GITHUB_REF/refs\/tags\//} - docker tag ${{ secrets.IMAGE_NAME }}:stable-latest ${{ secrets.IMAGE_NAME }}:stable-${GITHUB_SHA::7} - - name: Docker stable push + docker buildx build --platform linux/amd64,linux/arm64 \ + --push \ + --no-cache \ + -t ${{ secrets.IMAGE_NAME }}:edge-latest \ + -t ${{ secrets.IMAGE_NAME }}:edge-${GITHUB_SHA::7} \ + . + - name: Docker stable build & push if: github.event_name != 'pull_request' && startsWith(github.ref, 'refs/tags/') && success() run: | - docker push ${{ secrets.IMAGE_NAME }}:stable-latest - docker push ${{ secrets.IMAGE_NAME }}:stable-${GITHUB_REF/refs\/tags\//} - docker push ${{ secrets.IMAGE_NAME }}:stable-${GITHUB_SHA::7} + docker buildx build --platform linux/amd64,linux/arm64 \ + --push \ + --no-cache \ + -t ${{ secrets.IMAGE_NAME }}:stable-latest \ + -t ${{ secrets.IMAGE_NAME }}:stable-${GITHUB_REF/refs\/tags\//} \ + -t ${{ secrets.IMAGE_NAME }}:stable-${GITHUB_SHA::7} \ + . # - name: Docker Hub Description # if: github.event_name != 'pull_request' && startsWith(github.ref, 'refs/tags/') && success() # uses: peter-evans/dockerhub-description@v2.0.0 diff --git a/Dockerfile b/Dockerfile index a91d399..e902475 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,13 @@ # Build the manager binary -FROM golang:1.24 as builder +ARG BUILDPLATFORM=linux/amd64 +ARG TARGETPLATFORM=linux/amd64 +ARG TARGETOS=linux +ARG TARGETARCH=amd64 + +FROM --platform=${BUILDPLATFORM} golang:1.24 as builder + +ARG TARGETOS +ARG TARGETARCH WORKDIR /workspace # Copy the Go Modules manifests @@ -16,11 +24,12 @@ COPY controllers/ controllers/ COPY pkg/ pkg/ # Build -RUN CGO_ENABLED=0 GO111MODULE=on go build -a -o manager main.go +RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} GO111MODULE=on go build -a -o manager main.go # Use distroless as minimal base image to package the manager binary # Refer to https://github.com/GoogleContainerTools/distroless for more details -FROM gcr.io/distroless/static:nonroot +ARG TARGETPLATFORM +FROM --platform=${TARGETPLATFORM} gcr.io/distroless/static:nonroot WORKDIR / COPY --from=builder /workspace/manager . USER nonroot:nonroot diff --git a/Makefile b/Makefile index 643a50c..74b4f0a 100644 --- a/Makefile +++ b/Makefile @@ -150,13 +150,9 @@ docker-push: ## Push docker image with the manager. PLATFORMS ?= linux/arm64,linux/amd64 .PHONY: docker-buildx docker-buildx: test ## Build and push docker image for the manager for cross-platform support - # copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile - sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross - - docker buildx create --name project-v3-builder - docker buildx use project-v3-builder - - docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross . + - docker buildx create --name project-v3-builder --use + - docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile . - docker buildx rm project-v3-builder - rm Dockerfile.cross ##@ Deployment