name: live

on:
  workflow_dispatch:
    inputs:
      release:
        description: Build release
        required: false
        type: boolean
        default: false
      clean:
        description: Delete resources
        required: false
        type: boolean
        default: false

permissions:
  id-token: write
  contents: read

defaults:
  run:
    shell: bash

jobs:
  test:
    runs-on: ubuntu-latest
    env:
      AZURE_CLIENT_ID: ${{ vars.AZURE_CLIENT_ID }}
      AZURE_TENANT_ID: ${{ vars.AZURE_TENANT_ID }}
      AZURE_SUBSCRIPTION_ID: ${{ vars.AZURE_SUBSCRIPTION_ID }}
    steps:
    - name: Checkout
      uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
    - name: Install azd
      uses: Azure/setup-azd@634ad924cf8baef2257898ba5663be8d19f15aca # v2.3.0
    - name: Install Pester
      run: |
        Install-Module Pester -RequiredVersion 5.6.1 -Force -Scope CurrentUser
        Import-Module Pester -RequiredVersion 5.6.1
      shell: pwsh
    - name: Cache
      uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
      with:
        path: |
          ~/.cargo/bin/
          ~/.cargo/registry/index/
          ~/.cargo/registry/cache/
          ~/.cargo/git/db/
          target/
        key: ${{ runner.os }}-${{ runner.arch }}-cargo-${{ hashFiles('**/Cargo.lock') }}
    - name: Set up toolchain
      run: rustup install
    - name: Authenticate az (OIDC)
      uses: azure/login@532459ea530d8321f2fb9bb10d1e0bcf23869a43 # v3.0.0
      with:
        client-id: ${{ vars.AZURE_CLIENT_ID }}
        tenant-id: ${{ vars.AZURE_TENANT_ID }}
        subscription-id: ${{ vars.AZURE_SUBSCRIPTION_ID }}
    - name: Authenticate azd (OIDC)
      run: >
        azd auth login
        --client-id "${env:AZURE_CLIENT_ID}"
        --federated-credential-provider "github"
        --tenant-id "${env:AZURE_TENANT_ID}"
      shell: pwsh
    - name: Provision
      run: azd provision --no-prompt
    - name: Test
      run: |
        Import-Module Pester -RequiredVersion 5.6.1
        $container = New-PesterContainer -Path './tests/' -Data @{ Release = ('${{ inputs.release }}' -eq 'true') }
        $config = New-PesterConfiguration
        $config.Run.Container = $container
        $config.Run.Exit = $true
        $config.Output.Verbosity = 'Detailed'
        $config.TestResult.Enabled = $true
        $config.TestResult.OutputFormat = 'JUnitXml'
        $config.TestResult.OutputPath = './tests/testResults.xml'
        Invoke-Pester -Configuration $config
      shell: pwsh
    - name: Clean
      if: ${{ inputs.clean && !cancelled() }}
      continue-on-error: true
      run: azd down --force --purge --no-prompt
    - name: Summarize tests
      uses: test-summary/action@37b508cfee6d4d080eedd00b5bb240a6a784a6a5 # v2.6
      if: always()
      with:
        paths: |
          ./tests/*.xml
