name: Build

on:
  push:
    branches:
      - '**'  # Build on all branches

permissions:
  contents: read
  packages: write

jobs:
  pre-check:
    runs-on: ubuntu-22.04
    outputs:
      skip-workflow: ${{ steps.check.outputs.skip-workflow }}
    steps:
      - name: Check if workflow should be skipped
        id: check
        env:
          COMMIT_AUTHOR: ${{ github.event.head_commit.author.name }}
          COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
        run: |
          if [[ "$COMMIT_AUTHOR" == "github-actions[bot]" ]] && [[ "$COMMIT_MESSAGE" == Release\ version* ]]; then
            echo "skip-workflow=true" >> $GITHUB_OUTPUT
            echo "Skipping this workflow..."
          else
            echo "skip-workflow=false" >> $GITHUB_OUTPUT
            echo "Proceeding with this workflow..."
          fi

  build-and-push-image:
    needs: [pre-check]
    if: ${{ needs.pre-check.outputs.skip-workflow == 'false' }}
    uses: docker/github-builder/.github/workflows/build.yml@v1
    permissions:
      contents: read
      packages: write
    with:
      output: image
      push: true
      platforms: linux/amd64,linux/arm64
      meta-images: ghcr.io/${{ github.repository }}
      meta-tags: |
        type=ref,event=branch
        type=ref,event=pr
        type=sha,prefix=sha-
      build-args: |
        VERSION=${{ github.sha }}
      sign: false
      cache: true
    secrets:
      registry-auths: |
        - registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
