feat: implement Docker build cache for CI acceleration
Some checks failed
CI/CD Pipeline / CI Pipeline (push) Has been cancelled
CI/CD Pipeline / Build Docker Cache (push) Has been cancelled

This commit is contained in:
2026-04-07 08:31:24 +02:00
parent 7c9dfdcc2a
commit 816e1b7bc8
3 changed files with 128 additions and 0 deletions

View File

@@ -57,8 +57,69 @@ env:
CI_REGISTRY: "gitea.arcodange.lab"
jobs:
build-cache:
name: Build Docker Cache
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[skip ci]') && github.actor != 'ci-bot'"
outputs:
deps_hash: ${{ steps.calculate_hash.outputs.deps_hash }}
cache_hit: ${{ steps.check_cache.outputs.cache_hit }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Calculate dependency hash
id: calculate_hash
run: |
# Calculate hash of go.mod + go.sum
DEPS_HASH=$(sha256sum go.mod go.sum | sha256sum | cut -d' ' -f1 | head -c 12)
echo "Dependency hash: $DEPS_HASH"
echo "deps_hash=$DEPS_HASH" >> $GITHUB_OUTPUT
- name: Check for existing cache
id: check_cache
run: |
# Check if image exists in registry
IMAGE_NAME="${{ env.CI_REGISTRY }}/${{ env.GITEA_ORG }}/${{ env.GITEA_REPO }}-build-cache:${{ steps.calculate_hash.outputs.deps_hash }}"
# Try to pull the image to see if it exists
if docker pull "$IMAGE_NAME" >/dev/null 2>&1; then
echo "✅ Cache hit - using existing build cache"
echo "cache_hit=true" >> $GITHUB_OUTPUT
else
echo "⚠️ Cache miss - will build new cache image"
echo "cache_hit=false" >> $GITHUB_OUTPUT
fi
- name: Login to Gitea Container Registry
if: steps.check_cache.outputs.cache_hit == 'false'
uses: docker/login-action@v3
with:
registry: ${{ env.CI_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.PACKAGES_TOKEN }}
- name: Set up Docker Buildx
if: steps.check_cache.outputs.cache_hit == 'false'
uses: docker/setup-buildx-action@v3
- name: Build and push Docker cache image
if: steps.check_cache.outputs.cache_hit == 'false'
run: |
IMAGE_NAME="${{ env.CI_REGISTRY }}/${{ env.GITEA_ORG }}/${{ env.GITEA_REPO }}-build-cache:${{ steps.calculate_hash.outputs.deps_hash }}"
echo "Building cache image: $IMAGE_NAME"
docker buildx build \
--file Dockerfile.build \
--tag "$IMAGE_NAME" \
--push \
.
echo "✅ Build cache image pushed successfully"
ci-pipeline:
name: CI Pipeline
needs: build-cache
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[skip ci]') && github.actor != 'ci-bot'"
@@ -66,6 +127,22 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Gitea Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.CI_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.PACKAGES_TOKEN }}
- name: Pull Docker build cache
run: |
IMAGE_NAME="${{ env.CI_REGISTRY }}/${{ env.GITEA_ORG }}/${{ env.GITEA_REPO }}-build-cache:${{ needs.build-cache.outputs.deps_hash }}"
echo "Using build cache image: $IMAGE_NAME"
docker pull "$IMAGE_NAME" || echo "Cache pull failed, will use regular setup"
- name: Set up Go
uses: actions/setup-go@v4
with: