Skip to content

Running in GitHub Actions

Juliette edited this page Jun 21, 2023 · 23 revisions

You can run PHPCS automatically whenever a new commit is pushed to your repository using GitHub actions, just follow these simple steps:

  1. Considering that your plugin is hosted in GitHub.
  2. ... and you are installing WordPressCS using Composer and can run it locally using ./vendor/bin/phpcs.
  3. ... and you have a [.]phpcs.xml[.dist] file in your project root directory. (If not, add --standard=path/to/yourrulesetfile.xml to the PHPCS command below).
  4. Create the folder .github/workflows on the root of your GitHub repository (if it does not exist yet).
  5. Create the file .github/workflows/phpcs.yml and add the below yaml script to it.
name: PHPCS check
on:
  push

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  phpcs:
    name: PHPCS check
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Setup PHP
        uses: "shivammathur/setup-php@v2"
        with:
          php-version: "7.4"
          ini-values: "memory_limit=1G"
          coverage: none
          tools: cs2pr

      - name: Install Composer dependencies
        uses: "ramsey/composer-install@v2"

      - name: Run PHPCS checks
        id: phpcs
        run: vendor/bin/phpcs --report-full --report-checkstyle=./phpcs-report.xml
        
      - name: Show PHPCS results in PR
        if: ${{ always() && steps.phpcs.outcome == 'failure' }}
        run: cs2pr ./phpcs-report.xml

Expected folder structure for this job to work out-of-the-box:

./project-root
├── .github
│   └── workflows
│       └── phpcs.yml
├── vendor
│   └── bin
│       └── phpcs
└── [.]phpcs.xml[.dist]  # PHPCS configuration file

If your folder structure is different, you might need to tweak the workflow a little bit.

Here's an example of a repository using this setup: https://github.com/Luc45/phpcs-ci-example

Clone this wiki locally