From 8ea3dbe55337a2db1108289388ba9e3012c462d0 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Sat, 30 Aug 2025 22:16:48 -0400 Subject: [PATCH] ci: consolidate to single workflow and add Docker layer caching; dev: optimize Dockerfile.dev for cache --- .github/workflows/ci.yml | 73 ++++++++++++++++++++++++++-------------- Dockerfile.dev | 21 +++++++----- 2 files changed, 60 insertions(+), 34 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e82c7b9..306f26d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,41 +6,62 @@ on: - main pull_request: branches: - - main + - "**" jobs: - build: + checks: runs-on: ubuntu-latest env: - GO111MODULE: on - + NAME: rentease + PORT: 8000 + DB_USER: ci + DB_NAME: villafleurie steps: - - name: Checkout code - uses: actions/checkout@v3 + - name: Checkout + uses: actions/checkout@v4 - - name: Set up Go - uses: actions/setup-go@v5 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Build dev image (cached) + uses: docker/build-push-action@v6 with: - go-version: 1.23 + context: . + file: Dockerfile.dev + tags: ${{ env.NAME }}:dev + load: true + cache-from: type=gha,scope=dev + cache-to: type=gha,scope=dev,mode=max - - name: Install dependencies + - name: Start dev container (background) run: | - go mod download + docker run -d \ + --name ${NAME} \ + -v "$GITHUB_WORKSPACE":/app \ + -v /app/tmp \ + ${NAME}:dev sleep infinity - - name: Lint code + - name: Make format + run: make format + + - name: Make lint + run: make lint + + - name: Make test + run: make test + + - name: Stop container + if: always() run: | - go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest - golangci-lint run + docker logs ${NAME} || true + docker stop ${NAME} || true - - name: Run tests - run: | - go test ./... - - # - name: Run Gosec Security Scanner - # uses: securego/gosec@master - # with: - # args: ./... - - - name: Build Docker image - run: | - docker build -t rentease . + - name: Build production image (cached, push only) + if: github.event_name == 'push' + uses: docker/build-push-action@v6 + with: + context: . + file: Dockerfile + tags: ${{ env.NAME }}:latest + cache-from: type=gha,scope=prod + cache-to: type=gha,scope=prod,mode=max diff --git a/Dockerfile.dev b/Dockerfile.dev index 7653a23..37f3cf2 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -2,19 +2,24 @@ FROM golang:1.24-alpine AS builder WORKDIR /app -RUN apk update && apk add --no-cache build-base +RUN apk add --no-cache build-base +# Install tooling early so it stays cached across source changes +RUN go install github.com/air-verse/air@latest \ + && go install github.com/a-h/templ/cmd/templ@latest + +# Leverage module cache COPY go.mod go.sum ./ -RUN go mod download +RUN --mount=type=cache,target=/go/pkg/mod \ + go mod download +# Copy the rest of the sources COPY . . -RUN go build -ldflags="-s -w" -o rentease main.go - -# Install air and templ for live reload and templating in dev -RUN go install github.com/air-verse/air@latest \ - && go install github.com/a-h/templ/cmd/templ@latest \ - && rm -rf /go/pkg/mod /root/.cache/go-build +# Build once (helps verify builds and speeds CI with cache) +RUN --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/go/pkg/mod \ + go build -ldflags="-s -w" -o rentease main.go # ----------- Dev Stage ----------- FROM golang:1.24-alpine AS dev