Skip to content

Latest commit

 

History

History
306 lines (240 loc) · 10.8 KB

README.md

File metadata and controls

306 lines (240 loc) · 10.8 KB

CI

The projects in src/ are tested on several PHP versions, and some on several framework versions. matrix.php decides which: each combination it builds is a cell. This directory holds the script, its tests, and the rules below for configuring a project's cells.

What runs

Each project has its own workflow, .github/workflows/project-<slug>.yml, so each has its own status and run history. <slug> is the project's path under src/, lowercased, with / as -. The workflow's name is the path, so the Actions sidebar lists projects by name.

The project workflows are generated by matrix.php --generate. Don't edit them. Each calls php-project.yml, which has two jobs:

  1. cells runs matrix.php --project <path>, which prints the project's cells. It fails on that project's own extra.ci problems, and no others, so one broken project doesn't stop the rest.
  2. test runs one job per cell. It installs the project and runs its style check, Phan, Psalm, PHPStan and PHPUnit, each only if the project has the config file for it. Experimental cells can fail without failing the run.

Each cell installs fresh, with composer update, or composer require for a framework version. Only Composer's downloads are cached, so a cell always tests the newest releases its constraints allow.

Xdebug is loaded only in coverage cells. They run PHPUnit a second time, on the unit and integration suites only, and upload the result to Codecov. Codecov carries each project's last upload forward, so a run that skips a project leaves its coverage as it was.

When a project's workflow runs

GitHub's paths filter decides. A project's workflow runs on a pull request or push to main that changes:

  • a file under src/<project>/
  • its own project-<slug>.yml
  • php-project.yml, matrix.php, composer.json or src/ in this directory. These run every project.
  • docker-compose.yaml, or docker/<service>/, if the project lists services (below)

It also runs weekly, against new dependency releases, and from the Actions tab with "Run workflow".

GitHub checks only the first 300 changed files against paths. A pull request changing more, such as a style fix across the repository, may not run every project it touches. The weekly run catches those.

See a project's status

Each project's README has a CI badge for its workflow on main. It links to this repository's runs, since the split repositories publish the same README. The root README lists every project with its badge, generated between two marker comments by matrix.php --generate.

In the Actions tab, the sidebar lists each project's workflow by path. "Run workflow" on one reruns that project alone, on any branch.

Checks on every change

.github/workflows/ci.yml runs on every pull request and push to main, whatever the change touches. It:

  1. validates the root composer.json
  2. runs the PHPUnit tests in tests/. They check matrix.php on small repositories built in a temp directory, never on src/, so they fail only when matrix.php itself is broken.
  3. runs matrix.php --check on the real repository. A contributor's mistake, such as a project missing from .gitsplit.yml, fails here. Without this, a project with no workflow would never be tested and nothing would say so.

--check fails naming the file at fault when:

  • a directory under src/ has a composer.json but is not inside a .gitsplit.yml prefix, so CI would never test it
  • a .gitsplit.yml prefix has no composer.json
  • extra.ci has an unknown key or a value of the wrong shape
  • extra.ci.services names a service that isn't in docker-compose.yaml
  • a project with a phpunit.xml.dist has no cell on the coverage version, or sets extra.ci.skip
  • a project has more cells than the 256 jobs GitHub allows in one matrix
  • a project has no workflow, a workflow has no project, or a workflow differs from what --generate writes
  • two projects would get the same workflow, such as Foo/Bar and Foo-Bar
  • a project's README.md has no CI badge. The error prints the line to add.
  • the root README.md has no marker comments for the project list, or the list differs from what --generate writes

A project is a prefix in .gitsplit.yml, unless it sets extra.ci.skip. It runs on every PHP version in supported and experimental in matrix.php that its composer.json require.php allows.

Add a project

  1. Create src/<Category>/<Name> with a composer.json that sets require.php. Copy phpunit.xml.dist, .php-cs-fixer.php, psalm.xml.dist, phpstan.neon.dist and .phan/config.php from a similar project.
  2. Add it to .gitsplit.yml. This is what makes CI test it.
  3. Add it to autoload and replace in the root composer.json.
  4. Add its owners to .github/component_owners.yml.
  5. If it needs more than the defaults, add extra.ci (below).
  6. Run make ci-generate. It writes the project's workflow and adds the project to the root README's list. Commit both.
  7. Add its CI badge to its README.md, under the title. make ci-check prints the line if it's missing.

Removing a project, or setting extra.ci.skip, also needs make ci-generate, which deletes the workflow and the root README's entry. So does changing extra.ci.services.

Configure a project's CI

Projects that need more than the defaults say so in their own composer.json, under extra.ci.

Test against several framework versions

composer-requires maps each constraint to the lowest PHP version that supports it. Each constraint gets its own cells, on PHP versions that satisfy both this minimum and require.php. The cell installs with composer require <constraint>.

"extra": {
  "ci": {
    "composer-requires": {
      "laravel/framework:^10": "8.1",
      "laravel/framework:^11": "8.2"
    }
  }
}

Install more PHP extensions

Every cell installs ast (for Phan) and opentelemetry. additional-extensions adds to them. It can't remove them, and listing them again is an error. Names are in the setup-php format, which is not always the ext-* name.

"extra": {
  "ci": {
    "additional-extensions": ["phalcon5"]
  }
}

ext-opentelemetry doesn't need to be in require. Most projects leave it out so they can be installed and worked on without the extension, and _register.php checks for it at runtime instead.

Start a service for integration tests

services lists the docker-compose.yaml services a project's tests need. Each cell starts them with docker compose up --wait before PHPUnit. Changing docker-compose.yaml, or a file under docker/<service>/, runs every project that lists that service. Run make ci-generate after changing services, since it adds those paths to the project's workflow.

"extra": {
  "ci": {
    "services": ["mysql"]
  }
}

The services in use are mysql, postgresql, rabbitmq, kafka and mongodb. CI starts mongodb with supercharge/mongodb-github-action instead of compose, because it sets up a replica set and the compose service doesn't.

To add a service:

  1. Add it to docker-compose.yaml with a healthcheck. Without one, --wait returns as soon as the container starts, and the first tests can fail while the service is still starting.
  2. Publish its port. In CI the tests run on the runner, not in the compose network, so they reach the service on localhost.
  3. Put any init files under docker/<service>/, so changing them runs only the projects using the service.
  4. Add a host variable, such as MYSQL_HOST, to the php service's environment, defaulting to the service name. Tests read it, falling back to localhost, so the same test works under make and in CI.
  5. List it in the project's extra.ci.services, and run make ci-generate.

Skip a project that isn't PHP code

skip leaves a project out of CI entirely: it gets no workflow. Use it for a prefix whose output isn't a Composer package, and which has its own CI. Run make ci-generate after setting it.

"extra": {
  "ci": {
    "skip": true
  }
}

OperatorPackages/autoinstrumentation-php skips. It builds a Docker image, and its composer.json only lists what goes in the image. Its own .github/workflows/build.yml builds the image, in the split repository.

A project with a phpunit.xml.dist can't skip, since its tests would never run.

Change PHP versions

The versions are at the top of matrix.php.

  • supported must pass.
  • experimental runs but may fail. Use it for a version that isn't released yet, or that projects don't support yet.
  • coverage is the one version whose cells upload coverage. It must be in supported.

To add a version, add it to experimental. Once its cells pass, move it to supported. Change coverage separately, when every project passes on the new version.

Run a project's checks locally

The Makefile runs the same checks as a CI cell, in the php service from docker-compose.yaml. It needs Docker with Compose v2. Copy .env.dist to .env first.

PROJECT is a path under src/, and make projects lists them. PHP_VERSION picks the image, and defaults to 8.1, the lowest version CI runs.

make projects

# Start the services in the project's extra.ci.services, if any
PROJECT=Instrumentation/PDO make services

# Install, then style, validate, Phan, Psalm, PHPStan and PHPUnit
PROJECT=Instrumentation/PDO PHP_VERSION=8.4 make all

docker compose down

CI skips a check when the project has no config for it, but make all runs them all. For a project without every config, such as MetaPackages/opentelemetry, run the targets one at a time, as in make validate test. make help lists them.

A cell uses composer require for a framework version. To test one locally, run it in the container:

PROJECT=Instrumentation/Laravel make bash
composer require laravel/framework:^11

Locally the tests reach services through the compose network, by service name. <SERVICE>_HOST in the php service's environment sets that.

Check changes to CI locally

matrix.php is a small Composer project of its own, with its tests in tests/. It needs PHP 8.5, whichever versions the projects are tested on. These make targets run it in the php service on 8.5, and install its dependencies first.

# Tests for matrix.php, as ci.yml runs them
make ci-test

# Rewrite the project workflows and the root README's project list,
# then check the repository as ci.yml does
make ci-generate
make ci-check

# The cells a project's workflow runs, as JSON
PROJECT=Instrumentation/Laravel make ci-cells

With PHP 8.5 or later on the host, run it directly instead:

composer install --working-dir=.github/ci
.github/ci/vendor/bin/phpunit -c .github/ci/phpunit.xml.dist
php .github/ci/matrix.php --check