# GitHub Action script for PHP CI

Hey guys,

A quick trick & sharing with y'all for my script to run PHPUnit with GitHub Action.

FYI some of my latest repositories are using this script:

*   [https://github.com/sethsandaru/laravel-mail-switcher](https://github.com/sethsandaru/laravel-mail-switcher)
    
*   [https://github.com/shipsaas/dev-flag](https://github.com/shipsaas/dev-flag)
    
*   [https://github.com/shipsaas/ready](https://github.com/shipsaas/ready)
    

### GitHub Action script x PHPUnit

Will be affected for:

*   A new commit to the `main` branch
    
*   New PRs that point to the `main` branch
    

Flow:

*   Install PHP 8.1 + `pcov` (best PHP coverage driver at the moment)
    
*   Install dependencies
    
*   (Optional) Setup env
    
*   Run the test with the coverage option
    
*   (Optional) Upload to CodeCov for the coverage result
    

```yaml
name: Build & Test
on:
    pull_request:
        branches:
            - 'main'
        types: [ opened, synchronize, reopened, ready_for_review ]
    push:
        branches:
            - 'main'

jobs:
    build_php81:
        runs-on: ubuntu-latest
        steps:
            -   uses: actions/checkout@v3
                if: success()

            -   name: Setup PHP with coverage driver
                uses: shivammathur/setup-php@v2
                with:
                    php-version: '8.1'
                    coverage: pcov

            -   name: Setup
                if: success()
                run: |
                    php -v
                    composer install --no-interaction

            -   name: PHPUnit tests with coverage
                if: success()
                run: |
                    composer test-coverage

            -   name: upload coverage to codecov.io
                uses: codecov/codecov-action@v1
                with:
                    token: ${{ secrets.CODECOV_TOKEN }}
                    file: ./coverage.xml
```

Feel free to copy it and change it to your desired flow 😉

Cheers!
