name: Nightly Build

on:
  push:
    branches:
      - main
      - '**nightly**'

jobs:

  #Reuse ci.yml workflow on main branch and upload artifacts to release page
  reuse_main_ci:
    uses: ./.github/workflows/ci.yml
    secrets: inherit

  upload_nightly_builds:

    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ ubuntu-latest]
    needs: reuse_main_ci

    env:
      GH_REPO: ${{ github.repository }}
      GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    steps:
      - name: Retrieve saved artifacts
        uses: actions/download-artifact@v8

      - name: Display structure of downloaded files
        run: ls -R
        working-directory: .

      - name: Bundle up wheels
        run: |
          cp build-dependency-*-wheel/* sasview-wheel/
          cd sasview-wheel
          zip ../SasView-nightly-wheels.zip *

      - name: Rename artifacts
        run: |
          mv SasView-Installer-windows*/setupSasView.exe ./setupSasView-nightly-Win64.exe
          mv SasView-Installer-macos*/SasView6-*.dmg ./
          for file in $(ls *.dmg); do
              mv $file ${file/SasView6-/SasView-nightly-MacOSX-}
          done
          mv SasView-Installer-ubuntu*/SasView6.tar.gz ./SasView-nightly-Linux.tar.gz

      - name: Clean out old release
        run: |
          gh release delete nightly-build --cleanup-tag --yes || true

      - name: Upload Nightly Build Installer to GitHub releases
        uses: ncipollo/release-action@v1
        with:
          draft: false
          prerelease: true
          allowUpdates: true
          replacesArtifacts: true
          token: ${{ secrets.GITHUB_TOKEN }}
          artifacts: "setupSasView-nightly-Win64.exe, SasView-nightly-MacOSX-*.dmg, SasView-nightly-Linux.tar.gz, SasView-nightly-wheels.zip"
          body: "Nightly build of main SasView branch"
          name: "nightly-build"
          tag: "nightly-build"
          commit: ${{ github.sha }}
