name: Nightly
on:
  workflow_dispatch:
#  schedule:
#    # 08:00 UTC = 03:00 EST
#    - cron:  '0 8 * * *'

permissions:
  contents: read # to fetch code (actions/checkout)

jobs:
  deploy:
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            java: 8
            distribution: adopt
    runs-on: ${{ matrix.os }}
    env:
      JAVA_OPTS: -Xms800M -Xmx800M -Xss6M -XX:ReservedCodeCacheSize=128M -server -Dsbt.io.virtual=false -Dfile.encoding=UTF-8
    steps:
    - name: Checkout sbt/sbt
      uses: actions/checkout@v6
    - name: Checkout sbt/io
      uses: actions/checkout@v6
      with:
        repository: sbt/io
        ref: develop
        path: io
    - name: Checkout sbt/zinc
      uses: actions/checkout@v6
      with:
        repository: sbt/zinc
        ref: develop
        path: zinc
    - name: Setup JDK
      uses: actions/setup-java@v5
      with:
        distribution: "${{ matrix.distribution }}"
        java-version: "${{ matrix.java }}"
    - name: Coursier cache
      uses: coursier/cache-action@v7
    - name: Build and deploy
      run: |
        # build from fresh IO, LM, and Zinc
        TIMESTAMP=$(TZ=UTC date +%Y%m%dT%H%M%S)
        export BUILD_VERSION="1.5.0-bin-${TIMESTAMP}"
        cd io
        sbt -v +publish
        cd ../
        sbt -Dsbtzinc.path=$HOME/work/sbt/sbt/zinc -Dsbt.build.fatal=false "+lowerUtils/publish; {zinc}/publish; upperModules/publish; bundledLauncherProj/publish"
        rm -rf "$HOME/.ivy2/local" || true
        find "$HOME/.sbt/boot" -name "*-SNAPSHOT" -exec rm -rf {} + 2>/dev/null || true
        find $HOME/Library/Caches/Coursier/v1        -name "ivydata-*.properties" -delete || true
        find $HOME/.ivy2/cache                       -name "ivydata-*.properties" -delete || true
        find $HOME/.cache/coursier/v1                -name "ivydata-*.properties" -delete || true
        find $HOME/.sbt                              -name "*.lock"               -delete || true
      env:
        BINTRAY_USER: ${{ secrets.BINTRAY_USER }}
        BINTRAY_PASS: ${{ secrets.BINTRAY_PASS }}
        BINTRAY_REPOSITORY: maven-snapshots
        BINTRAY_PACKAGE: sbt
