GitHub Actions: Reusable Workflows and Composite Actions for WordPress CI

Reusable workflows and composite actions for WordPress plugin CI

An agency with ten plugins has ten copies of the same CI workflow: set up PHP, install Composer packages with caching, run PHPCS, PHPUnit against a WordPress test database, build the ZIP. A fix in one copy rarely reaches the other nine. GitHub Actions has two ways to share CI code: reusable workflows (a whole workflow called like a function) and composite actions (a group of steps used like a single action).

Problem: Each plugin repository has a 120-line tests.yml. When the PHP matrix had to include 8.4, the change was made in four repositories and forgotten in six, and the Composer cache key differs between them.

Solution: Put a composite action for “set up PHP and Composer with cache” and a reusable workflow for “lint and test a WordPress plugin” into a central .github repository of the organization, call them from each plugin with a few lines and inputs, and pin them to a tag so updates are deliberate.

A composite action: shared steps

# agency/ci-actions/setup-php-composer/action.yml
name: Setup PHP and Composer
description: PHP with extensions and a cached composer install
inputs:
  php-version:
    required: true
runs:
  using: composite
  steps:
    - uses: shivammathur/setup-php@v2
      with:
        php-version: ${{ inputs.php-version }}
        extensions: mysqli, zip, intl
        coverage: none
        tools: composer:v2
    - uses: actions/cache@v4
      with:
        path: ~/.composer/cache
        key: composer-${{ runner.os }}-${{ inputs.php-version }}-${{ hashFiles('**/composer.lock') }}
    - run: composer install --no-interaction --no-progress
      shell: bash

A composite action bundles steps; it runs inside the calling job, on its runner, and every run step must declare its shell. It is the right tool for a sequence that many jobs repeat.

A reusable workflow: a whole pipeline

# agency/.github/.github/workflows/wp-plugin-ci.yml
name: WordPress plugin CI
on:
  workflow_call:
    inputs:
      php-versions:
        type: string
        default: '["8.1","8.3","8.4"]'
      wp-version:
        type: string
        default: latest

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        php: ${{ fromJSON(inputs.php-versions) }}
    services:
      mysql:
        image: mysql:8.0
        env:
          MYSQL_ROOT_PASSWORD: root
        ports: [ "3306:3306" ]
        options: --health-cmd="mysqladmin ping" --health-interval=5s --health-retries=10
    steps:
      - uses: actions/checkout@v4
      - uses: agency/ci-actions/setup-php-composer@v1
        with:
          php-version: ${{ matrix.php }}
      - run: vendor/bin/phpcs
      - run: bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1 ${{ inputs.wp-version }}
      - run: vendor/bin/phpunit

A reusable workflow is triggered by workflow_call and defines its own jobs, runners and services; the caller cannot add steps to it. Inputs are typed (string, number, boolean); arrays are passed as JSON strings and parsed with fromJSON(). Secrets are passed explicitly or with secrets: inherit.

Call it from a plugin

# ha-booking/.github/workflows/ci.yml
name: CI
on: [ push, pull_request ]
jobs:
  ci:
    uses: agency/.github/.github/workflows/wp-plugin-ci.yml@v1
    with:
      php-versions: '["8.2","8.4"]'

Each plugin’s CI file shrinks to a few lines. When the shared workflow gets a new PHP version in the default matrix, every plugin that uses the default gets it with the next release tag v1.

Versioning

Pin to a tag (@v1) or a commit SHA, not to @main: a change in the shared repository should not break all plugins at once. Move a major tag forward after testing (git tag -f v1 && git push -f origin v1), and create v2 for breaking changes such as a removed input. For workflows used across repositories, the shared repository must be public, or in the same organization with access granted under Settings → Actions → Access.

Which one when

  • Composite action: a few steps that appear in many jobs (setup, build, deploy to a server).
  • Reusable workflow: a complete pipeline with its own jobs and matrix (lint plus tests, release).
  • They combine: the reusable workflow above uses the composite action.

NOTE: The single-repository pipeline that these pieces come from: GitHub Actions CI/CD for WordPress plugins. Releases from tags with the GitHub CLI: gh for plugin releases.