From f84c83a7e28d72cd88efe3f4cdb78a2248892efd Mon Sep 17 00:00:00 2001 From: krau <71133316+krau@users.noreply.github.com> Date: Mon, 30 Jun 2025 22:47:47 +0800 Subject: [PATCH] fix(ci): try speed up docker build --- .github/workflows/build-docker.yml | 27 +++++++++------------------ Dockerfile | 7 +++++-- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/.github/workflows/build-docker.yml b/.github/workflows/build-docker.yml index 2d34ed8..1314611 100644 --- a/.github/workflows/build-docker.yml +++ b/.github/workflows/build-docker.yml @@ -29,13 +29,7 @@ jobs: type=semver,pattern={{version}} type=semver,pattern={{major}}.{{minor}} type=sha - type=raw,value=latest - type=ref,event=branch - type=ref,event=tag - labels: | - org.opencontainers.image.title=${{ env.IMAGE_NAME }} - org.opencontainers.image.source=https://github.com/krau/SaveAny-Bot - org.opencontainers.image.url=https://github.com/krau/SaveAny-Bot + type=raw,value=latest,enable={{is_default_branch}} - name: Set up QEMU uses: docker/setup-qemu-action@v3 @@ -50,23 +44,20 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - - name: Extract version from Git Ref - id: extract_version - run: | - VERSION=$(echo "${{ github.ref }}" | sed 's/refs\/tags\/v//') - echo "VERSION=${VERSION}" >> $GITHUB_ENV - - name: Build and push Docker image + id: build-and-push uses: docker/build-push-action@v6 with: context: . platforms: linux/amd64,linux/arm64 - cache-from: type=gha + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: | + type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest + type=gha cache-to: type=gha,mode=max build-args: | VERSION=${{ steps.meta.outputs.version }} GitCommit=${{ github.sha }} - BuildTime=${{ format(github.event.repository.updated_at, 'yyyy-MM-dd HH:mm:ss') }} - push: true - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} + BuildTime=${{ fromJson(toJSON(github.event.repository.pushed_at)) }} diff --git a/Dockerfile b/Dockerfile index 9f4d58f..5b407d6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,15 +6,18 @@ ARG BuildTime="Unknown" WORKDIR /app -COPY . . +COPY go.mod go.sum ./ +RUN --mount=type=cache,target=/go/pkg/mod \ + go mod download +COPY . . RUN --mount=type=cache,target=/root/.cache/go-build \ --mount=type=cache,target=/go/pkg \ CGO_ENABLED=0 \ go build -trimpath \ -ldflags "-s -w \ -X github.com/krau/SaveAny-Bot/common.Version=${VERSION} \ - -X github.com/krau/SaveAny-Bot/common.GitCommit=${GiTCommit} \ + -X github.com/krau/SaveAny-Bot/common.GitCommit=${GitCommit} \ -X github.com/krau/SaveAny-Bot/common.BuildTime=${BuildTime}" \ -o saveany-bot .