192 lines
6.3 KiB
YAML
192 lines
6.3 KiB
YAML
name: Build and Push Docker Images
|
|
|
|
on:
|
|
push:
|
|
branches: [ main ]
|
|
tags: [ 'v*' ]
|
|
paths:
|
|
- 'backend/**'
|
|
- 'frontend/**'
|
|
- '.gitea/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
|
|
shell: bash
|
|
run: |
|
|
echo "Checking for changes..."
|
|
|
|
# Initialize outputs
|
|
BACKEND="false"
|
|
FRONTEND="false"
|
|
|
|
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
|
|
echo "Workflow manually triggered - building both"
|
|
BACKEND="true"
|
|
FRONTEND="true"
|
|
elif [ "${{ github.ref }}" = refs/tags/* ]; then
|
|
echo "Tag detected - building both"
|
|
BACKEND="true"
|
|
FRONTEND="true"
|
|
else
|
|
echo "Checking changed files..."
|
|
|
|
# For push events, compare with the parent commit
|
|
if [ "${{ github.event_name }}" = "push" ]; then
|
|
echo "Push event detected"
|
|
# Get the parent commit
|
|
PARENT_SHA=$(git rev-parse HEAD^)
|
|
CURRENT_SHA=${{ github.sha }}
|
|
|
|
echo "Comparing parent ${PARENT_SHA} with current ${CURRENT_SHA}"
|
|
|
|
# List all changed files
|
|
CHANGED_FILES=$(git diff --name-only ${PARENT_SHA} ${CURRENT_SHA})
|
|
echo "Changed files:"
|
|
echo "$CHANGED_FILES"
|
|
|
|
# Check for changes in specific directories
|
|
if echo "$CHANGED_FILES" | grep -q "^backend/"; then
|
|
echo "Backend changes detected"
|
|
BACKEND="true"
|
|
fi
|
|
|
|
if echo "$CHANGED_FILES" | grep -q "^frontend/"; then
|
|
echo "Frontend changes detected"
|
|
FRONTEND="true"
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
# Output results
|
|
echo "backend=$BACKEND" >> $GITHUB_OUTPUT
|
|
echo "frontend=$FRONTEND" >> $GITHUB_OUTPUT
|
|
|
|
# Debug output
|
|
echo "Debug: Backend will build: $BACKEND"
|
|
echo "Debug: Frontend will build: $FRONTEND"
|
|
|
|
|
|
build-backend:
|
|
needs: changes
|
|
if: ${{ needs.changes.outputs.backend == 'true' }}
|
|
runs-on: docker
|
|
container:
|
|
image: ghcr.io/catthehacker/ubuntu:act-latest
|
|
steps:
|
|
- name: Debug output
|
|
run: |
|
|
echo "Backend build triggered"
|
|
echo "Backend output was: ${{ needs.changes.outputs.backend }}"
|
|
- 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
|
|
shell: bash
|
|
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: Debug output
|
|
run: |
|
|
echo "Frontend build triggered"
|
|
echo "Frontend output was: ${{ needs.changes.outputs.frontend }}"
|
|
- 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
|
|
shell: bash
|
|
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 }}" |