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.
153 lines
5.0 KiB
YAML
153 lines
5.0 KiB
YAML
name: Build and Push Docker Images
|
|
|
|
on:
|
|
push:
|
|
branches: [ main ]
|
|
tags: [ 'v*' ]
|
|
paths:
|
|
- 'backend/**'
|
|
- 'frontend/**'
|
|
- '.github/workflows/docker-build.yml'
|
|
workflow_dispatch:
|
|
|
|
jobs:
|
|
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
|
|
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 backend
|
|
uses: docker/build-push-action@v5
|
|
with:
|
|
context: ./backend
|
|
file: ./backend/Dockerfile
|
|
push: true
|
|
target: production
|
|
tags: |
|
|
gitea.pragmazest.com/${{ github.repository }}/backend:${{ steps.meta.outputs.version }}
|
|
gitea.pragmazest.com/${{ github.repository }}/backend:latest
|
|
labels: |
|
|
org.opencontainers.image.created=${{ steps.meta.outputs.date }}
|
|
org.opencontainers.image.version=${{ steps.meta.outputs.version }}
|
|
org.opencontainers.image.revision=${{ github.sha }}
|
|
|
|
- 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:
|
|
context: ./frontend
|
|
file: ./frontend/Dockerfile
|
|
push: true
|
|
target: runner
|
|
tags: |
|
|
gitea.pragmazest.com/${{ github.repository }}/frontend:${{ steps.meta.outputs.version }}
|
|
gitea.pragmazest.com/${{ github.repository }}/frontend:latest
|
|
labels: |
|
|
org.opencontainers.image.created=${{ steps.meta.outputs.date }}
|
|
org.opencontainers.image.version=${{ steps.meta.outputs.version }}
|
|
org.opencontainers.image.revision=${{ github.sha }}
|
|
|
|
- name: Image digest
|
|
run: |
|
|
echo "Frontend image: gitea.pragmazest.com/${{ github.repository }}/frontend:${{ steps.meta.outputs.version }}" |