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: //: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 "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%%/*}" # Allow explicit override via secrets or env 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 echo "Replacing unresolved host 'gitea' with override '$OVERRIDE'" REG_HOST="$OVERRIDE" fi if [ "$REG_HOST" = "gitea" ]; then REG_HOST="git.thumeit.com"; fi 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|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: ${{ steps.repo.outputs.owner }}" echo "Images: ${{ steps.repo.outputs.host }}/${{ steps.repo.outputs.owner }}/${BACKEND_IMAGE} and frontend" - name: Login (optional) id: login run: | set -e HOST="${{ steps.repo.outputs.host }}" if [ -n "${{ secrets.CR_USERNAME }}" ] && [ -n "${{ secrets.CR_PASSWORD }}" ]; then echo "${{ secrets.CR_PASSWORD }}" | docker login "$HOST" -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:-$OWNER}}" echo "${{ secrets.GITEA_REGISTRY_TOKEN }}" | docker login "$HOST" -u "$ACTOR" --password-stdin echo "authenticated=1" >> $GITHUB_OUTPUT; exit 0 fi echo "No credentials; will skip push."; echo "authenticated=0" >> $GITHUB_OUTPUT - 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 [ -n "${{ steps.meta.outputs.latest_tag }}" ] && docker tag ${BASE}:${{ steps.meta.outputs.sha_tag }} ${BASE}:latest [ -n "${{ steps.meta.outputs.version_tag }}" ] && docker tag ${BASE}:${{ steps.meta.outputs.sha_tag }} ${BASE}:${{ steps.meta.outputs.version_tag }} - 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 [ -n "${{ steps.meta.outputs.latest_tag }}" ] && docker tag ${BASE}:${{ steps.meta.outputs.sha_tag }} ${BASE}:latest [ -n "${{ steps.meta.outputs.version_tag }}" ] && docker tag ${BASE}:${{ steps.meta.outputs.sha_tag }} ${BASE}:${{ steps.meta.outputs.version_tag }} - name: Push images if: github.event_name != 'pull_request' run: | if [ "${{ steps.login.outputs.authenticated }}" != "1" ]; then echo "Skipping push (unauthenticated)"; exit 0; fi 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 }} [ -n "${{ steps.meta.outputs.latest_tag }}" ] && docker push ${BASE}:latest [ -n "${{ steps.meta.outputs.version_tag }}" ] && docker push ${BASE}:${{ steps.meta.outputs.version_tag }} done 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: | if [ "${{ steps.login.outputs.authenticated }}" != "1" ]; then echo "Skipping push attempt (not authenticated). Provide CR_USERNAME/CR_PASSWORD or GITEA_REGISTRY_TOKEN to enable." exit 0 fi 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