Skip to content

Running in GitHub Actions

LinaQiu edited this page Oct 9, 2024 · 23 revisions

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

  1. Considering that your plugin is hosted on 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 in 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:
  pull_request:
  # Allow manually triggering the workflow.
  workflow_dispatch:

# Cancel all previous workflow runs for the same branch that have not yet completed.
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@v4

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

      - name: Install Composer dependencies
        uses: "ramsey/composer-install@v3"
        with:
          # Bust the cache at least once a month - output format: YYYY-MM.
          custom-cache-suffix: $(date -u "+%Y-%m")

      - 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