Skip to content

Running in GitHub Actions

Lucas Bustamante edited this page Jun 24, 2022 · 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. Considering you are already running PHPCS locally using ./vendor/bin/phpcs.
  3. Considering that you have a .phpcs.xml.dist file in your root directory. (If not, tweak the last line of the workflow 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.
  6. Place this content inside of it:
name: PHPCS checks
on:
  push
concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true
jobs:
  phpcs:
    name: PHPCS
    runs-on: ubuntu-20.04
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - uses: "shivammathur/setup-php@v2"
        with:
          php-version: "7.4"
      - uses: "ramsey/composer-install@v2"
      - name: Run PHPCS checks
        run: php -d memory_limit=1G ${GITHUB_WORKSPACE}/vendor/bin/phpcs -s --standard=${GITHUB_WORKSPACE}/.phpcs.xml.dist

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

./plugin-foo
├── .github
│   └── workflows
│       └── phpcs.yml
├── vendor
│   └── bin
│       └── phpcs
├── .phpcs.xml.dist  # PHPCS configuration file
└── composer.lock    # Composer.lock file with PHPCS

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