name: Release a new version
on:
  push:
    branches:
      - stable
      - stage

env:
  php_version: 7.1
  phpunit_version: 4
  composer_version: 1

jobs:
  release:
    name: Release
    if: |
      !(github.event_name == 'push' && contains(github.event.head_commit.message, '[skip ci]')) &&
      !(github.event_name == 'pull_request' && contains(join(github.event.pull_request.title, github.event.pull_request.body), '[skip ci]'))
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Import GPG key
        uses: crazy-max/ghaction-import-gpg@v4
        with:
          git_user_signingkey: true
          git_commit_gpgsign: true
          gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
          passphrase: ${{ secrets.GPG_PASSPHRASE }}

      - uses: actions/setup-node@v3
        with:
          node-version: 16
          cache: yarn

      - uses: php-actions/composer@v6
        with:
          version: ${{ env.composer_version }}
          php_version: ${{ env.php_version }}

      - name: Run PHPUnit
        uses: php-actions/phpunit@v3
        with:
          version: ${{ env.phpunit_version }}
          php_version: ${{ env.php_version }}

      - name: Run PHPCS
        run: php vendor/bin/phpcs --standard=PSR2 src/

      - name: Install dependencies
        run: yarn

      - name: Semantic release
        run: yarn release
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}

          GIT_AUTHOR_NAME: ${{ secrets.GIT_USERNAME }}
          GIT_AUTHOR_EMAIL: ${{ secrets.GIT_EMAIL }}
          GIT_COMMITTER_NAME: ${{ secrets.GIT_USERNAME }}
          GIT_COMMITTER_EMAIL: ${{ secrets.GIT_EMAIL }}
