name: Release

on:
  schedule:
    - cron: '0 0 * * *'
  workflow_dispatch:
    inputs:
      channel:
        description: 'release to perform'
        required: true
        default: 'nightly'
        type: choice
        options:
        - nightly
        - "%date"
  push:
    branches:
      - release

env:
  KICK_VERSION: "${{github.event.inputs.channel}} || nightly"
  RUST_LOG: kick=trace

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - uses: dtolnay/rust-toolchain@stable
    - uses: Swatinem/rust-cache@v2
    - run: cargo test --all-targets
    - run: cargo test --doc
    - run: cargo run --bin rune -- check --recursive scripts
    - run: cargo run --bin rune -- test --all-targets -O test-std

  build:
    runs-on: ${{matrix.os}}
    strategy:
      matrix:
        os: [macos-latest, windows-latest, ubuntu-latest]
    steps:
    - uses: actions/checkout@v4
    - uses: udoprog/kick@nightly
    - uses: dtolnay/rust-toolchain@stable
    - uses: Swatinem/rust-cache@v2
    - run: kick define --github-action
      id: release
    - run: cargo build --release
    - run: cargo run -p builder -- --channel ${{steps.release.outputs.version}}
    - uses: actions/upload-artifact@v4
      with:
        name: dist-${{matrix.os}}
        path: dist

  publish:
    needs: [test, build]
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - uses: udoprog/kick@nightly
    - uses: actions/download-artifact@v4
      with: {name: dist-macos-latest, path: dist}
    - uses: actions/download-artifact@v4
      with: {name: dist-windows-latest, path: dist}
    - uses: actions/download-artifact@v4
      with: {name: dist-ubuntu-latest, path: dist}
    - run: kick gh release --upload "dist/*" --github-action
      env:
        GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
