name: release-nightly

on:
  workflow_dispatch:
  schedule:
    - cron: "45 0 * * *"

env:
  CARGO_TERM_COLOR: always
  RUSTFLAGS: "-D warnings"

jobs:
  # Check if the actions already ran in the last 24 hours
  # * If yes: Don't run again
  # * If no: Clean out existing release assets
  prepare-nightly:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    name: Prepare Nightly Release
    outputs:
      should-run: ${{ steps.should-run.outputs.should-run }}
    # TODO: Re-add
    # if: ${{ github.repository == 'gleam-lang/gleam' }}
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          persist-credentials: false
      - name: print latest_commit
        run: echo ${{ github.sha }}

      - id: should-run
        continue-on-error: true
        name: check latest commit is less than a day
        if: ${{ github.event_name == 'schedule' }}
        run: test -z $(git rev-list --after="24 hours" ${{ github.sha }}) && echo "should-run=false" >> $GITHUB_OUTPUT

      - name: Delete old release assets
        if: ${{ steps.should-run.outputs.should-run != 'false' }}
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          gh release view nightly --json assets --jq '.assets[].name' | \
            xargs -r -I {} gh release delete-asset nightly {} --yes

  build-release:
    name: build-release
    runs-on: ${{ matrix.os }}
    permissions:
      contents: write
      id-token: write
      attestations: write
    environment: release
    outputs:
      release-id: ${{ steps.release.outputs.id }}
    strategy:
      matrix:
        target:
          - x86_64-unknown-linux-musl
          - aarch64-unknown-linux-musl
          - x86_64-apple-darwin
          - aarch64-apple-darwin
          - x86_64-pc-windows-msvc
          - aarch64-pc-windows-msvc
        toolchain: [stable]
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-musl
            expected-binary-architecture: x86-64
            cargo-tool: cross
          - os: ubuntu-latest
            target: aarch64-unknown-linux-musl
            expected-binary-architecture: aarch64
            cargo-tool: cross
          - os: macos-15-intel
            target: x86_64-apple-darwin
            expected-binary-architecture: x86_64
            cargo-tool: cargo
          - os: macos-latest
            target: aarch64-apple-darwin
            expected-binary-architecture: arm64
            cargo-tool: cargo
          - os: windows-2022
            target: x86_64-pc-windows-msvc
            expected-binary-architecture: x86-64
            cargo-tool: cargo
          - os: windows-11-arm
            target: aarch64-pc-windows-msvc
            expected-binary-architecture: arm64
            cargo-tool: cargo
          - os: ubuntu-latest
            target: wasm32-unknown-unknown
            cargo-tool: wasm-pack
    steps:
      - name: Checkout repository
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          persist-credentials: false

      - name: Update versions
        shell: bash
        run: ./bin/add-nightly-suffix-to-versions.sh

      - name: "Build Archive"
        id: build
        uses: "./.github/actions/build-release"
        with:
          version: nightly
          toolchain: ${{ matrix.toolchain }}
          target: ${{ matrix.target }}
          cargo-tool: ${{ matrix.cargo-tool }}
          expected-binary-architecture: ${{ matrix.expected-binary-architecture }}
          generate-gleam-licences-html: ${{ matrix.target == 'x86_64-unknown-linux-musl' }}
          azure-tenant-id: ${{ secrets.AZURE_TENANT_ID }}
          azure-subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
          azure-client-id: ${{ secrets.AZURE_CLIENT_ID }}
          azure-trusted-signing-account-name: ${{ vars.AZURE_TRUSTED_SIGNING_ACCOUNT_NAME }}
          azure-certificate-profile-name: ${{ vars.AZURE_CERTIFICATE_PROFILE_NAME }}

      - name: Upload release archive
        id: release
        uses: softprops/action-gh-release@718ea10b132b3b2eba29c1007bb80653f286566b # v3.0.1
        with:
          draft: false
          prerelease: true
          fail_on_unmatched_files: true
          files: "${{ steps.build.outputs.files }}"
          tag_name: nightly
          name: "Nightly"
          body: |
            A build that runs every night following changes to the main branch. Contains all the latest features and changes, but there is a higher chance of instability, bugs, and unfinished features.

            The assets are updated but the tag is not, so if you view the commit or download the source from below you will not get the correct code.

  build-container-images:
    name: build-container-images
    needs: [prepare-nightly, build-release]
    runs-on: ubuntu-latest
    permissions:
      packages: write
      id-token: write
      attestations: write
    if: ${{ needs.prepare-nightly.outputs.should-run != 'false' }}
    strategy:
      matrix:
        base-image:
          - scratch
          - erlang
          - erlang-slim
          - erlang-alpine
          - elixir
          - elixir-slim
          - elixir-alpine
          - node
          - node-slim
          - node-alpine

    steps:
      - name: Checkout repository
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          sparse-checkout: |
            .github/actions
            containers
          persist-credentials: false

      - name: "Build & Push Container"
        uses: "./.github/actions/build-container"
        with:
          version: nightly
          release-id: ${{ needs.build-release.outputs.release-id }}
