Add conditional Docker builds for backend and frontend
Refactor workflow to build Docker images based on file changes. Introduce checks to determine if backend or frontend directories were modified. Optimize build processes and include support for manual triggers and version tagging.
This commit is contained in:
@@ -4,10 +4,57 @@ on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
tags: [ 'v*' ]
|
||||
paths:
|
||||
- 'backend/**'
|
||||
- 'frontend/**'
|
||||
- '.github/workflows/docker-build.yml'
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
build-and-push:
|
||||
changes:
|
||||
runs-on: docker
|
||||
container:
|
||||
image: ghcr.io/catthehacker/ubuntu:act-latest
|
||||
outputs:
|
||||
backend: ${{ steps.filter.outputs.backend }}
|
||||
frontend: ${{ steps.filter.outputs.frontend }}
|
||||
steps:
|
||||
- name: Checkout Repository
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0 # Needed to get all history for path filtering
|
||||
|
||||
- name: Check for changed files
|
||||
id: filter
|
||||
run: |
|
||||
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
|
||||
# If manually triggered, build both
|
||||
echo "backend=true" >> $GITHUB_OUTPUT
|
||||
echo "frontend=true" >> $GITHUB_OUTPUT
|
||||
elif [[ "${{ github.ref }}" == refs/tags/* ]]; then
|
||||
# If it's a tag, build both
|
||||
echo "backend=true" >> $GITHUB_OUTPUT
|
||||
echo "frontend=true" >> $GITHUB_OUTPUT
|
||||
else
|
||||
# Check which directories have changes
|
||||
git fetch origin main
|
||||
|
||||
if git diff --name-only origin/main..HEAD | grep -q "^backend/"; then
|
||||
echo "backend=true" >> $GITHUB_OUTPUT
|
||||
else
|
||||
echo "backend=false" >> $GITHUB_OUTPUT
|
||||
fi
|
||||
|
||||
if git diff --name-only origin/main..HEAD | grep -q "^frontend/"; then
|
||||
echo "frontend=true" >> $GITHUB_OUTPUT
|
||||
else
|
||||
echo "frontend=false" >> $GITHUB_OUTPUT
|
||||
fi
|
||||
fi
|
||||
|
||||
build-backend:
|
||||
needs: changes
|
||||
if: needs.changes.outputs.backend == 'true'
|
||||
runs-on: docker
|
||||
container:
|
||||
image: ghcr.io/catthehacker/ubuntu:act-latest
|
||||
@@ -22,7 +69,7 @@ jobs:
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: gitea.pragmazest.com
|
||||
username: ${{ secrets.DOCKER_REGISTRY_USERNAME }}
|
||||
username: ${{ secrets.DOCKER_REGISTRY_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_REGISTRY_ACCESS_TOKEN }}
|
||||
|
||||
- name: Extract metadata
|
||||
@@ -36,7 +83,6 @@ jobs:
|
||||
echo "version=${VERSION}" >> $GITHUB_OUTPUT
|
||||
echo "date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
|
||||
|
||||
# Build and push backend image
|
||||
- name: Build and push backend
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
@@ -52,7 +98,41 @@ jobs:
|
||||
org.opencontainers.image.version=${{ steps.meta.outputs.version }}
|
||||
org.opencontainers.image.revision=${{ github.sha }}
|
||||
|
||||
# Build and push frontend image
|
||||
- name: Image digest
|
||||
run: |
|
||||
echo "Backend image: gitea.pragmazest.com/${{ github.repository }}/backend:${{ steps.meta.outputs.version }}"
|
||||
|
||||
build-frontend:
|
||||
needs: changes
|
||||
if: needs.changes.outputs.frontend == 'true'
|
||||
runs-on: docker
|
||||
container:
|
||||
image: ghcr.io/catthehacker/ubuntu:act-latest
|
||||
steps:
|
||||
- name: Checkout Repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Login to Gitea Registry
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: gitea.pragmazest.com
|
||||
username: ${{ secrets.DOCKER_REGISTRY_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_REGISTRY_ACCESS_TOKEN }}
|
||||
|
||||
- name: Extract metadata
|
||||
id: meta
|
||||
run: |
|
||||
if [[ $GITHUB_REF == refs/tags/* ]]; then
|
||||
VERSION=${GITHUB_REF#refs/tags/v}
|
||||
else
|
||||
VERSION=latest
|
||||
fi
|
||||
echo "version=${VERSION}" >> $GITHUB_OUTPUT
|
||||
echo "date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Build and push frontend
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
@@ -68,7 +148,6 @@ jobs:
|
||||
org.opencontainers.image.version=${{ steps.meta.outputs.version }}
|
||||
org.opencontainers.image.revision=${{ github.sha }}
|
||||
|
||||
- name: Image digests
|
||||
- name: Image digest
|
||||
run: |
|
||||
echo "Backend image: gitea.pragmazest.com/${{ github.repository }}/backend:${{ steps.meta.outputs.version }}"
|
||||
echo "Frontend image: gitea.pragmazest.com/${{ github.repository }}/frontend:${{ steps.meta.outputs.version }}"
|
||||
Reference in New Issue
Block a user