diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 2a0ef34f0..4a96c65db 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -9,7 +9,7 @@ concurrency: cancel-in-progress: false jobs: - build-app: + build-images: runs-on: ubuntu-latest strategy: matrix: @@ -42,12 +42,6 @@ jobs: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_PASSWORD }} - - name: Set up Go - if: matrix.service_name == 'app' - uses: actions/setup-go@v4 - with: - go-version: '1.24' - - name: Prepare version info id: version run: | @@ -62,6 +56,20 @@ jobs: # 显示版本信息 ./scripts/get_version.sh info + - name: Build Cache for Docker + uses: actions/cache@v4 + id: cache + with: + path: go-build-cache + key: ${{ runner.os }}-go-build-cache-${{ hashFiles('**/go.sum') }} + + - name: Inject go-build-cache + uses: reproducible-containers/buildkit-cache-dance@v3 + with: + cache-source: go-build-cache + dockerfile: ${{ matrix.file }} + skip-extraction: ${{ steps.cache.outputs.cache-hit }} + - name: Build ${{ matrix.service_name }} Image uses: docker/build-push-action@v3 with: @@ -78,4 +86,5 @@ jobs: ${{ secrets.DOCKERHUB_USERNAME }}/weknora-${{ matrix.service_name }}:latest ${{ secrets.DOCKERHUB_USERNAME }}/weknora-${{ matrix.service_name }}:${{ steps.version.outputs.version }} cache-from: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/weknora-${{ matrix.service_name }}:cache - cache-to: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/weknora-${{ matrix.service_name }}:cache,mode=max \ No newline at end of file + cache-to: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/weknora-${{ matrix.service_name }}:cache,mode=max + \ No newline at end of file diff --git a/docker/Dockerfile.app b/docker/Dockerfile.app index ddb36e073..0ae72e14f 100644 --- a/docker/Dockerfile.app +++ b/docker/Dockerfile.app @@ -21,10 +21,9 @@ RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/re RUN go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest # Copy go mod and sum files -COPY go.mod . -COPY go.sum . -RUN go mod download -COPY . . +COPY go.mod go.sum ./ +RUN --mount=type=cache,target=/go/pkg/mod go mod download +RUN --mount=type=cache,target=/go/pkg/mod cp -r /go/pkg/mod/github.com/yanyiwu/ /app/yanyiwu/ # Get version and commit info for build injection ARG VERSION_ARG @@ -39,7 +38,8 @@ ENV BUILD_TIME=${BUILD_TIME_ARG} ENV GO_VERSION=${GO_VERSION_ARG} # Build the application with version info -RUN make build-prod +COPY . . +RUN --mount=type=cache,target=/go/pkg/mod make build-prod # Final stage FROM alpine:3.17 @@ -58,7 +58,7 @@ RUN mkdir -p /data/files && \ # Copy migrate tool from builder stage COPY --from=builder /go/bin/migrate /usr/local/bin/ -COPY --from=builder /go/pkg/mod/github.com/yanyiwu /go/pkg/mod/github.com/yanyiwu/ +COPY --from=builder /app/yanyiwu/ /go/pkg/mod/github.com/yanyiwu/ # Copy the binary from the builder stage COPY --from=builder /app/config ./config