ci: consolidate to single workflow and add Docker layer caching; dev: optimize Dockerfile.dev for cache

This commit is contained in:
Ruidy 2025-08-30 22:16:48 -04:00
parent 90d82fb79b
commit 8ea3dbe553
No known key found for this signature in database
GPG key ID: 705C24D202990805
2 changed files with 60 additions and 34 deletions

View file

@ -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

View file

@ -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