name: release
on:
  push:
    tags:
      - "v*"

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

permissions:
  contents: read

jobs:
  build-release:
    name: build-release
    runs-on: ${{ matrix.os }}
    environment: release
    permissions:
      id-token: write
      attestations: write
    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: "Build Archive"
        id: build
        uses: "./.github/actions/build-release"
        with:
          version: ${{ github.ref_name }}
          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 }}

  create-release:
    name: create-release

    needs: ['build-release']

    runs-on: ubuntu-latest

    environment: release

    permissions:
      contents: write

    steps:
      - name: Download Artifacts
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          pattern: release-*
          merge-multiple: true

      - name: Create release
        env:
          GITHUB_TOKEN: '${{ github.token }}'
          REPOSITORY: '${{ github.repository }}'
          TITLE: '${{ github.ref_name }}'
          TAG_NAME: '${{ github.ref_name }}'
          NOTES: '${{ github.server_url }}/${{ github.repository }}/blob/${{ github.ref_name }}/CHANGELOG.md'
        run: |
          gh release create \
            --repo "$REPOSITORY" \
            --title "$TITLE" \
            --notes "$NOTES" \
            --draft \
            --verify-tag \
            ${{ contains(github.ref_name, '-rc') && '--prerelease' || '' }} \
            "$TAG_NAME" \
            gleam-*
