98 lines
4.2 KiB
YAML
98 lines
4.2 KiB
YAML
name: Build & Push Docker Images
|
|
|
|
on:
|
|
push:
|
|
branches: ["main", "master"]
|
|
tags: ["v*.*.*"]
|
|
pull_request:
|
|
branches: ["main", "master"]
|
|
|
|
env:
|
|
BACKEND_IMAGE: imap-client-backend
|
|
FRONTEND_IMAGE: imap-client-frontend
|
|
# Uses local Gitea registry: <gitea-host>/<namespace>/<image>:tag
|
|
|
|
jobs:
|
|
build:
|
|
runs-on: docker
|
|
steps:
|
|
- name: Checkout
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Derive repository context
|
|
id: repo
|
|
shell: bash
|
|
run: |
|
|
set -e
|
|
RAW_REPO="${GITHUB_REPOSITORY:-${GITEA_REPOSITORY:-}}"
|
|
if [ -z "$RAW_REPO" ]; then
|
|
echo "RAW_REPO not set (expected GITHUB_REPOSITORY or GITEA_REPOSITORY)"; exit 1
|
|
fi
|
|
OWNER="${RAW_REPO%%/*}"
|
|
REPO="${RAW_REPO#*/}"
|
|
if [ -z "$OWNER" ] || [ "$OWNER" = "$RAW_REPO" ]; then
|
|
echo "Failed to parse owner from '$RAW_REPO'"; exit 1
|
|
fi
|
|
REG_HOST="${GITEA_SERVER_URL:-${GITHUB_SERVER_URL:-}}"
|
|
if [ -z "$REG_HOST" ]; then
|
|
echo "GITEA_SERVER_URL/GITHUB_SERVER_URL not set"; exit 1
|
|
fi
|
|
REG_HOST="${REG_HOST#http://}"
|
|
REG_HOST="${REG_HOST#https://}"
|
|
REG_HOST="${REG_HOST%%/*}"
|
|
echo "owner=$OWNER" >> $GITHUB_OUTPUT
|
|
echo "repo=$REPO" >> $GITHUB_OUTPUT
|
|
echo "host=$REG_HOST" >> $GITHUB_OUTPUT
|
|
echo "Derived -> host=$REG_HOST owner=$OWNER repo=$REPO"
|
|
|
|
- name: Prepare tags
|
|
id: meta
|
|
run: |
|
|
SHA_TAG=sha-${GITHUB_SHA::7}
|
|
echo "sha_tag=$SHA_TAG" >> $GITHUB_OUTPUT
|
|
if [[ "$GITHUB_REF" == refs/heads/main || "$GITHUB_REF" == refs/heads/master ]]; then
|
|
echo "latest_tag=latest" >> $GITHUB_OUTPUT
|
|
fi
|
|
if [[ "$GITHUB_REF" == refs/tags/v* ]]; then
|
|
VERSION_TAG=${GITHUB_REF#refs/tags/}
|
|
echo "version_tag=$VERSION_TAG" >> $GITHUB_OUTPUT
|
|
fi
|
|
|
|
- name: Show build plan
|
|
run: |
|
|
echo "Registry host: ${{ steps.repo.outputs.host }}"
|
|
echo "Namespace (owner): ${{ steps.repo.outputs.owner }}"
|
|
echo "Backend image: ${{ steps.repo.outputs.host }}/${{ steps.repo.outputs.owner }}/${BACKEND_IMAGE}"
|
|
echo "Frontend image: ${{ steps.repo.outputs.host }}/${{ steps.repo.outputs.owner }}/${FRONTEND_IMAGE}"
|
|
|
|
- name: Login to local Gitea registry
|
|
run: |
|
|
if [ -z "${{ secrets.CR_USERNAME }}" ] || [ -z "${{ secrets.CR_PASSWORD }}" ]; then
|
|
echo "CR_USERNAME / CR_PASSWORD secrets missing"; exit 1
|
|
fi
|
|
echo "${{ secrets.CR_PASSWORD }}" | docker login "${{ steps.repo.outputs.host }}" -u "${{ secrets.CR_USERNAME }}" --password-stdin
|
|
|
|
- name: Build backend
|
|
run: |
|
|
BASE="${{ steps.repo.outputs.host }}/${{ steps.repo.outputs.owner }}/${BACKEND_IMAGE}"
|
|
docker build -t ${BASE}:${{ steps.meta.outputs.sha_tag }} ./backend
|
|
if [ -n "${{ steps.meta.outputs.latest_tag }}" ]; then docker tag ${BASE}:${{ steps.meta.outputs.sha_tag }} ${BASE}:latest; fi
|
|
if [ -n "${{ steps.meta.outputs.version_tag }}" ]; then docker tag ${BASE}:${{ steps.meta.outputs.sha_tag }} ${BASE}:${{ steps.meta.outputs.version_tag }}; fi
|
|
|
|
- name: Build frontend
|
|
run: |
|
|
BASE="${{ steps.repo.outputs.host }}/${{ steps.repo.outputs.owner }}/${FRONTEND_IMAGE}"
|
|
docker build -t ${BASE}:${{ steps.meta.outputs.sha_tag }} ./frontend
|
|
if [ -n "${{ steps.meta.outputs.latest_tag }}" ]; then docker tag ${BASE}:${{ steps.meta.outputs.sha_tag }} ${BASE}:latest; fi
|
|
if [ -n "${{ steps.meta.outputs.version_tag }}" ]; then docker tag ${BASE}:${{ steps.meta.outputs.sha_tag }} ${BASE}:${{ steps.meta.outputs.version_tag }}; fi
|
|
|
|
- name: Push images
|
|
if: github.event_name != 'pull_request'
|
|
run: |
|
|
for IMG in ${BACKEND_IMAGE} ${FRONTEND_IMAGE}; do
|
|
BASE="${{ steps.repo.outputs.host }}/${{ steps.repo.outputs.owner }}/${IMG}"
|
|
docker push ${BASE}:${{ steps.meta.outputs.sha_tag }}
|
|
if [ -n "${{ steps.meta.outputs.latest_tag }}" ]; then docker push ${BASE}:latest; fi
|
|
if [ -n "${{ steps.meta.outputs.version_tag }}" ]; then docker push ${BASE}:${{ steps.meta.outputs.version_tag }}; fi
|
|
done
|