name: Nightly Build

on:
    push:
        branches: [main]
        tags-ignore:
            - "v*" # Don't trigger on version tags - docker-publish handles those

env:
    IMAGE_NAME: ${{ secrets.DOCKERHUB_USERNAME }}/lidify

jobs:
    build-nightly:
        name: Build & Push Nightly Image
        runs-on: ubuntu-latest
        steps:
            - name: Checkout code
              uses: actions/checkout@v4

            - name: Free up disk space
              run: |
                  sudo rm -rf /usr/share/dotnet
                  sudo rm -rf /opt/ghc
                  sudo rm -rf /usr/local/share/boost
                  sudo rm -rf "$AGENT_TOOLSDIRECTORY"

            - name: Set up Docker Buildx
              uses: docker/setup-buildx-action@v3

            - name: Login to Docker Hub
              uses: docker/login-action@v3
              with:
                  username: ${{ secrets.DOCKERHUB_USERNAME }}
                  password: ${{ secrets.DOCKERHUB_TOKEN }}

            - name: Get short SHA
              id: sha
              run: echo "short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT

            - name: Build and push nightly
              uses: docker/build-push-action@v5
              with:
                  context: .
                  file: ./Dockerfile
                  push: true
                  tags: |
                      ${{ env.IMAGE_NAME }}:nightly
                      ${{ env.IMAGE_NAME }}:nightly-${{ steps.sha.outputs.short }}
                  labels: |
                      org.opencontainers.image.revision=${{ github.sha }}
                      org.opencontainers.image.version=nightly-${{ steps.sha.outputs.short }}
                  cache-from: type=gha
                  cache-to: type=gha,mode=max
                  # ARM64 disabled due to QEMU emulation issues with npm packages
                  platforms: linux/amd64
