name: ci

on:
  push:
  pull_request:
  workflow_dispatch:

permissions:
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v4
      - name: Configure
        run: ./configure --prefix=/usr
      - name: Build
        run: make
      - name: Install smoke test
        run: make install DESTDIR="$PWD/.destdir"
      - name: Run tests
        run: make -C tests
      - name: Regenerate configure
        run: sh autogen.sh
      - name: Verify generated files are up to date
        run: git diff --exit-code -- configure
      - name: Debian
        run: make -C dist
      - name: Pub
        uses: actions/upload-artifact@v4
        with:
          name: acr-deb
          path: dist/acr/*.deb
          if-no-files-found: error

  release-check:
    runs-on: ubuntu-latest
    outputs:
      should_release: ${{ steps.check.outputs.should_release }}
    steps:
      - id: check
        run: |
          if [[ "$GITHUB_REF_TYPE" == "tag" && "$GITHUB_REF_NAME" =~ ^[0-9] ]]; then
            echo "should_release=true" >> "$GITHUB_OUTPUT"
          else
            echo "should_release=false" >> "$GITHUB_OUTPUT"
          fi

  release:
    needs:
      - build
      - release-check
    runs-on: ubuntu-latest
    if: needs.release-check.outputs.should_release == 'true'
    permissions:
      contents: write
    steps:
      - name: Download artifact
        uses: actions/download-artifact@v4
        with:
          name: acr-deb
          path: ./deb
      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          files: ./deb/*.deb
