name: build

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: true
      matrix:
        php: [7.1, 7.2, 7.3, 7.4, 8.0, 8.1]
        stability: [ prefer-lowest, prefer-stable ]

    name: Test PHP ${{ matrix.php }} - ${{ matrix.stability }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          tools: composer:v2
          coverage: xdebug

      - name: Validate composer.json and composer.lock
        run: composer validate

      - name: Install dependencies
        if: steps.composer-cache.outputs.cache-hit != 'true'
        run: composer install --prefer-dist --no-progress --no-suggest

      - name: Run phpstan analyse
        run: composer analyse

      - name: Run test suite
        run: composer test

      - name: Run Coveralls
        env:
          COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          COVERALLS_PARALLEL: true
          COVERALLS_FLAG_NAME: ${{ runner.os }} - ${{ matrix.php }} - ${{ matrix.stability }}
        run: ./vendor/bin/php-coveralls --coverage_clover=build/logs/clover.xml -v

  benchmark:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: true
      matrix:
        php: [7.1, 8.0]

    name: Benchmark PHP ${{ matrix.php }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          tools: composer:v2
          coverage: none

      - name: Validate composer.json and composer.lock
        run: composer validate

      - name: Install dependencies
        if: steps.composer-cache.outputs.cache-hit != 'true'
        run: composer install --prefer-dist --no-progress --no-suggest

      - name: Run benchmark
        run: composer benchmark

  upload-coverage:
    runs-on: ubuntu-latest
    needs: [ test ]
    steps:
      - name: Coveralls Finished
        uses: coverallsapp/github-action@master
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          parallel-finished: true

  semantic-release:
    runs-on: ubuntu-latest
    needs: [ test, upload-coverage ]
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: 'lts/*'

      - name: Run semantic-release
        if: github.repository == 'php-casbin/php-casbin' && github.event_name == 'push'
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
        run: npx semantic-release