From 1df5cf42e32abaeedddc504afeb431d77154f78f Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Fri, 21 Jan 2022 12:31:24 +0100 Subject: [PATCH] Simplify Docker build in CI --- .dockerignore | 4 +++ .github/workflows/check.yaml | 17 ++++------ Dockerfile | 19 ----------- docker-bake.hcl | 64 ++++++++++-------------------------- 4 files changed, 27 insertions(+), 77 deletions(-) diff --git a/.dockerignore b/.dockerignore index 0c50c0918..aea2924dd 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,7 +1,11 @@ target/ crates/*/target crates/*/node_modules +docs/ +.devcontainer/ .git/ +.github/ +.gitignore Dockerfile .dockerignore docker-bake.hcl diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index bd89692f1..afc495148 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -322,7 +322,6 @@ jobs: bake-target: docker-metadata-action tags: | type=ref,event=branch - type=ref,event=pr type=semver,pattern={{version}} type=semver,pattern={{major}}.{{minor}} type=semver,pattern={{major}} @@ -336,7 +335,6 @@ jobs: bake-target: docker-metadata-action-debug tags: | type=ref,event=branch,suffix=-debug - type=ref,event=pr,suffix=-debug type=semver,pattern={{version}},suffix=-debug type=semver,pattern={{major}}.{{minor}},suffix=-debug type=semver,pattern={{major}},suffix=-debug @@ -353,9 +351,6 @@ jobs: [registry."docker.io"] mirrors = ["mirror.gcr.io"] - [worker.oci] - max-parallelism = 2 - - name: Login to GitHub Container Registry if: github.event_name != 'pull_request' uses: docker/login-action@v1 @@ -364,20 +359,20 @@ jobs: username: ${{ github.repository_owner }} password: ${{ secrets.GITHUB_TOKEN }} + # For pull-requests, only read from the cache, do not try to push to the + # cache or the image itself - name: Build uses: docker/bake-action@v1 if: github.event_name == 'pull_request' with: - targets: gha set: | - gha-push.cache-from=type=registry,ref=${{ env.IMAGE }}:buildcache + base.cache-from=type=registry,ref=${{ env.IMAGE }}:buildcache - name: Build and push uses: docker/bake-action@v1 if: github.event_name != 'pull_request' with: - targets: gha set: | - gha-push.output=type=image,push=true - gha-push.cache-from=type=registry,ref=${{ env.IMAGE }}:buildcache - gha-push.cache-to=type=registry,ref=${{ env.IMAGE }}:buildcache,mode=max + base.output=type=image,push=true + base.cache-from=type=registry,ref=${{ env.IMAGE }}:buildcache + base.cache-to=type=registry,ref=${{ env.IMAGE }}:buildcache,mode=max diff --git a/Dockerfile b/Dockerfile index 35dd13740..860f7c4ee 100644 --- a/Dockerfile +++ b/Dockerfile @@ -104,25 +104,6 @@ RUN cargo build \ # Move the binary to avoid having to guess its name in the next stage RUN mv target/$(/docker-arch-to-rust-target.sh "${TARGETPLATFORM}")/release/mas-cli /mas-cli -## Stage to run unit tests ## -FROM --platform=${BUILDPLATFORM} chef AS test - -ARG TARGETPLATFORM - -# Build dependencies -COPY --from=planner /app/recipe.json recipe.json -RUN cargo chef cook \ - --recipe-path recipe.json \ - --target $(/docker-arch-to-rust-target.sh "${TARGETPLATFORM}") - -# Run the tests -COPY . . -COPY --from=static-files /app/crates/static-files/public /app/crates/static-files/public -ENV SQLX_OFFLINE=true -RUN cargo test \ - --workspace \ - --target $(/docker-arch-to-rust-target.sh "${TARGETPLATFORM}") - ## Runtime stage, debug variant ## FROM --platform=${TARGETPLATFORM} gcr.io/distroless/cc-debian${DEBIAN_VERSION}:debug-nonroot AS debug COPY --from=builder /mas-cli /mas-cli diff --git a/docker-bake.hcl b/docker-bake.hcl index 089af042f..97acacd5c 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -1,55 +1,25 @@ -// Target filled by GitHub Actions, one for the regular tag, one for the debug tag +// This is what is baked by GitHub Actions +group "default" { targets = ["regular", "debug"] } + +// Targets filled by GitHub Actions: one for the regular tag, one for the debug tag target "docker-metadata-action" {} target "docker-metadata-action-debug" {} -target "default" { - dockerfile = "Dockerfile" - context = "./" +// This sets the platforms and is further extended by GitHub Actions to set the +// output and the cache locations +target "base" { + platforms = [ + "linux/amd64", + "linux/arm64", + "linux/arm", + ] +} + +target "regular" { + inherits = ["base", "docker-metadata-action"] } target "debug" { - inherits = ["default"] + inherits = ["base", "docker-metadata-action-debug"] target = "debug" } - -target "test" { - inherits = ["default"] - target = "test" -} - -target "release" { - inherits = ["default"] - platforms = [ - "linux/amd64", - "linux/arm64", - "linux/arm", - ] -} - -// This is what is baked by GitHub Actions -group "gha" { targets = ["gha-regular", "gha-debug", "gha-test"] } - -target "gha-base" { - inherits = ["release"] - // TODO: there seems to be a bug with inheriting twice the platforms, here is a workaround - platforms = [ - "linux/amd64", - "linux/arm64", - "linux/arm", - ] -} - -// This is filled by GitHub Actions -target "gha-push" {} - -target "gha-regular" { - inherits = ["gha-base", "gha-push", "docker-metadata-action"] -} - -target "gha-debug" { - inherits = ["gha-base", "gha-push", "debug", "docker-metadata-action-debug"] -} - -target "gha-test" { - inherits = ["gha-base", "test"] -}