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 DOCKER_BUILDKIT: "1" # Uses local Gitea registry: //:tag jobs: build: runs-on: docker steps: - name: Checkout uses: actions/checkout@v4 - name: Derive repository context id: repo shell: bash run: | set -euo pipefail RAW_REPO="${GITHUB_REPOSITORY:-${GITEA_REPOSITORY:-}}" if [ -z "$RAW_REPO" ]; then echo "Missing GITHUB_REPOSITORY/GITEA_REPOSITORY"; exit 1; fi OWNER="${RAW_REPO%%/*}"; REPO="${RAW_REPO#*/}" REG_HOST_RAW="${GITEA_SERVER_URL:-${GITHUB_SERVER_URL:-}}" if [ -z "$REG_HOST_RAW" ]; then echo "Missing GITEA_SERVER_URL/GITHUB_SERVER_URL"; exit 1; fi REG_HOST="${REG_HOST_RAW#http://}"; REG_HOST="${REG_HOST#https://}"; REG_HOST="${REG_HOST%%/*}" # Override if needed (set secret/env to git.thumeit.com[:port]) OVERRIDE="${{ secrets.GITEA_REGISTRY_HOST }}" [ -z "${OVERRIDE:-}" ] && OVERRIDE="${GITEA_REGISTRY_HOST:-}" [ -z "${OVERRIDE:-}" ] && OVERRIDE="${{ secrets.REGISTRY_HOST }}" [ -z "${OVERRIDE:-}" ] && OVERRIDE="${REGISTRY_HOST:-}" if [ "$REG_HOST" = "gitea" ] && [ -n "${OVERRIDE:-}" ]; then REG_HOST="$OVERRIDE"; fi echo "owner=tfaour" >> $GITHUB_OUTPUT echo "repo=imap_client" >> $GITHUB_OUTPUT echo "host=git.thumeit.com" >> $GITHUB_OUTPUT echo "Derived -> host=$REG_HOST owner=$OWNER repo=$REPO" - name: Prepare tags id: meta shell: bash run: | set -euo pipefail SHA_TAG=sha-${GITHUB_SHA::7} echo "sha_tag=$SHA_TAG" >> $GITHUB_OUTPUT [[ "$GITHUB_REF" =~ refs/heads/(main|master)$ ]] && echo "latest_tag=latest" >> $GITHUB_OUTPUT || true 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: ${{ 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 (optional) id: login shell: bash run: | set -euo pipefail HOST="${{ steps.repo.outputs.host }}" if [ -n "${{ secrets.CR_USERNAME }}" ] && [ -n "${{ secrets.CR_PASSWORD }}" ]; then echo "${{ secrets.CR_PASSWORD }}" | docker login "git.thumeit.com" -u "${{ secrets.CR_USERNAME }}" --password-stdin echo "authenticated=1" >> $GITHUB_OUTPUT; exit 0 fi if [ -n "${{ secrets.GITEA_REGISTRY_TOKEN }}" ]; then ACTOR="${GITHUB_ACTOR:-${GITEA_ACTOR:-${{ steps.repo.outputs.owner }}}}" echo "${{ secrets.GITEA_REGISTRY_TOKEN }}" | docker login "$HOST" -u "$ACTOR" --password-stdin echo "authenticated=1" >> $GITHUB_OUTPUT; exit 0 fi echo "No credentials; push will be skipped." echo "authenticated=0" >> $GITHUB_OUTPUT - name: Docker build (backend) id: build_backend shell: bash run: | set -euo pipefail BASE="${{ steps.repo.outputs.host }}/${{ steps.repo.outputs.owner }}/${BACKEND_IMAGE}" docker build --progress=plain -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: Docker build (frontend) id: build_frontend shell: bash run: | set -euo pipefail BASE="${{ steps.repo.outputs.host }}/${{ steps.repo.outputs.owner }}/${FRONTEND_IMAGE}" docker build --progress=plain -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: Docker push (both images) id: push_images if: github.event_name != 'pull_request' && steps.login.outputs.authenticated == '1' shell: bash run: | set -euo pipefail for IMG in ${BACKEND_IMAGE} ${FRONTEND_IMAGE}; do BASE="git.thumeit.com/${{ steps.repo.outputs.owner }}/${IMG}" docker push ${BASE}:${{ steps.meta.outputs.sha_tag }} [ -n "${{ steps.meta.outputs.latest_tag }}" ] && docker push ${BASE}:latest || true [ -n "${{ steps.meta.outputs.version_tag }}" ] && docker push ${BASE}:${{ steps.meta.outputs.version_tag }} || true done - name: Summary if: always() run: | echo "Build backend: ${{ steps.build_backend.outcome }}" echo "Build frontend: ${{ steps.build_frontend.outcome }}" echo "Login: ${{ steps.login.outcome }} (auth=${{ steps.login.outputs.authenticated }})" echo "Push images: ${{ steps.push_images.outcome || 'skipped' }}"