name: release-nightly-snapcraft

on:
  push:
    branches:
      - main
  workflow_dispatch:

permissions:
  contents: read

jobs:
  build-and-publish:
    runs-on: ubuntu-latest

    env:
      SNAPCRAFT_STORE_CREDENTIALS: ${{ secrets.SNAPCRAFT_STORE_CREDENTIALS }}

    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7

      - name: Install snapcraft
        run: sudo snap install snapcraft --classic

      - name: Authenticate snapcraft
        shell: bash
        run: snapcraft login --with <(printf '%s' "$SNAPCRAFT_STORE_CREDENTIALS")

      - name: Remote build
        run: |
          snapcraft remote-build \
            --launchpad-accept-public-upload \
            --build-for=amd64,arm64,armhf

      - name: List built snaps
        run: find . -maxdepth 1 -type f -name '*.snap' -print

      - name: Upload and release snapcraft nightly build
        run: |
          set -euo pipefail

          for snap in ./*.snap; do
            echo "Uploading $snap to edge"
            snapcraft upload --release="latest/edge" "$snap"
          done
