name: Build and Push Docker Images on: release: types: [published] # Ensure only one workflow run per branch at a time; cancel any in-progress runs on new push concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true jobs: build: runs-on: ubuntu-latest env: COMMIT_SHA: ${{ github.sha }} strategy: matrix: service: - "dograh-api|api/Dockerfile|." - "dograh-ui|ui/Dockerfile|." steps: - name: Free Disk Space uses: jlumbroso/free-disk-space@main with: tool-cache: false android: false dotnet: false haskell: true large-packages: true docker-images: true swap-storage: true - name: Checkout repository uses: actions/checkout@v4 with: submodules: true - name: Set up QEMU uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Log in to DockerHub uses: docker/login-action@v3 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Log in to GHCR uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ secrets.GHCR_USERNAME }} password: ${{ secrets.GHCR_TOKEN }} - name: Set build variables id: build-vars run: | SERVICE="${{ matrix.service }}" IMAGE_NAME=$(echo "$SERVICE" | cut -d '|' -f1) SHORT_SHA=${COMMIT_SHA::8} # Get version from release tag (removes 'dograh-' and 'v' prefixes if present) VERSION="${{ github.event.release.tag_name }}" VERSION="${VERSION#dograh-}" VERSION="${VERSION#v}" echo "image_name=${IMAGE_NAME}" >> $GITHUB_OUTPUT echo "short_sha=${SHORT_SHA}" >> $GITHUB_OUTPUT echo "version=${VERSION}" >> $GITHUB_OUTPUT - name: Build and Push ${{ matrix.service }} id: docker-build run: | SERVICE="${{ matrix.service }}" IMAGE_NAME=$(echo "$SERVICE" | cut -d '|' -f1) DOCKERFILE=$(echo "$SERVICE" | cut -d '|' -f2) CONTEXT=$(echo "$SERVICE" | cut -d '|' -f3) SHORT_SHA=${COMMIT_SHA::8} VERSION="${{ steps.build-vars.outputs.version }}" echo "Building and pushing image: $IMAGE_NAME" echo "Dockerfile: $DOCKERFILE" echo "Context: $CONTEXT" echo "Version: $VERSION" echo "image_name=${IMAGE_NAME}" >> $GITHUB_OUTPUT echo "dockerhub_tag=${{ secrets.DOCKERHUB_USERNAME }}/${IMAGE_NAME}:${SHORT_SHA}" >> $GITHUB_OUTPUT echo "ghcr_tag=ghcr.io/${{ secrets.GHCR_USERNAME }}/${IMAGE_NAME}:${SHORT_SHA}" >> $GITHUB_OUTPUT echo "short_sha=${SHORT_SHA}" >> $GITHUB_OUTPUT docker buildx build \ -f "$DOCKERFILE" \ --platform linux/amd64,linux/arm64 \ --tag ${{ secrets.DOCKERHUB_USERNAME }}/$IMAGE_NAME:$VERSION \ --tag ${{ secrets.DOCKERHUB_USERNAME }}/$IMAGE_NAME:$SHORT_SHA \ --tag ${{ secrets.DOCKERHUB_USERNAME }}/$IMAGE_NAME:latest \ --tag ghcr.io/${{ secrets.GHCR_USERNAME }}/$IMAGE_NAME:$VERSION \ --tag ghcr.io/${{ secrets.GHCR_USERNAME }}/$IMAGE_NAME:$SHORT_SHA \ --tag ghcr.io/${{ secrets.GHCR_USERNAME }}/$IMAGE_NAME:latest \ --push "$CONTEXT" - name: Send Slack notification - Success if: success() uses: slackapi/slack-github-action@v1.26.0 env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} with: payload: | { "text": "✅ Docker Build Successful - ${{ steps.build-vars.outputs.image_name }} (${{ steps.build-vars.outputs.version }}) on ${{ github.ref_name }} by ${{ github.actor }}" } - name: Send Slack notification - Failure if: failure() uses: slackapi/slack-github-action@v1.26.0 env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} with: payload: | { "text": "❌ Docker Build Failed - ${{ steps.build-vars.outputs.image_name }} (${{ steps.build-vars.outputs.version }}) on ${{ github.ref_name }} by ${{ github.actor }} - <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|View Logs>" }