name: Publish the released PHAR

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:
    inputs:
      tag:
        description: 'Tag release version'
        required: true

permissions:
  contents: write

jobs:
  publish:
    runs-on: ubuntu-22.04

    name: Release PHAR

    steps:
      - name: Extract the tag version
        id: tag
        run: |
          if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            GITHUB_REF=${{ github.event.inputs.tag }}
          fi
          echo "tag=${GITHUB_REF##*v}" >> "$GITHUB_OUTPUT"

      - name: Checkout the code
        uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6
        with:
          persist-credentials: false
          ref: v${{ steps.tag.outputs.tag }}

      - name: Setup PHP
        uses: shivammathur/setup-php@7c071dfe9dc99bdf297fa79cb49ea005b9fcadbc # v2
        with:
          php-version: 8.2
          extensions: dom, curl, libxml, mbstring, zip
          ini-values: error_reporting=E_ALL
          tools: composer:v2
          coverage: none

      - name: Install the dependencies
        run: composer install --prefer-dist --no-progress

      - name: Create the PHAR file.
        run: ./pint app:build pint.phar --build-version=${{ steps.tag.outputs.tag }}

      - name: Upload the PHAR artifact
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
        with:
          name: pint.phar
          path: builds/pint.phar

      - name: Publish the release with the PHAR attached
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          # Immutable releases reject assets added after publication, so the PHAR
          # is attached as the release is created, in a single atomic step.
          gh release create "v${{ steps.tag.outputs.tag }}" builds/pint.phar --generate-notes
