PHP
CI/CD basics
Learn to set up a continuous integration pipeline with GitHub Actions or GitLab CI.
Continuous integration and delivery
Continuous integration (CI) runs tests automatically on every commit. Continuous delivery (CD) ships the new build to the server when tests pass.
GitHub Actions defines a workflow in a .yml file. name: CI is the workflow name, on: push is the trigger, jobs: lists one or more jobs.
Each job runs on a runs-on virtual machine; under steps you list ready actions with uses and shell commands with run.
A typical PHP pipeline
A typical PHP pipeline: actions/checkout@v4 pulls the code, composer install installs dependencies, php test runs the tests.
If all steps are green the deploy step kicks in: build the image, push to a registry, pull on the server and restart.
When a step fails the pipeline halts and the team is alerted; that is why short, fast and reproducible steps are preferred.
Exercise Generate CI workflow YAML
Build a GitHub Actions YAML text with PHP: lines name: CI, on: push, jobs:, test:, runs-on: ubuntu-latest, steps:, - uses: actions/checkout@v4, - run: composer install and - run: php test. Write it to ci.yml via file_put_contents and echo the text.
Show example solution
Build $yaml with single-quoted parts joined by "\n". Put the indentation inside the single quotes. Then add file_put_contents("ci.yml", $yaml); and echo $yaml;.
Example solution:
<?php
// Generate CI workflow YAML
$yaml = 'name: CI' . "\n" .
'on: push' . "\n" .
'jobs:' . "\n" .
' test:' . "\n" .
' runs-on: ubuntu-latest' . "\n" .
' steps:' . "\n" .
' - uses: actions/checkout@v4' . "\n" .
' - run: composer install' . "\n" .
' - run: php test' . "\n";
file_put_contents("ci.yml", $yaml);
echo $yaml;
Test yourself
-
Match the CI/CD steps
- composer install
- Installs dependencies
- php test
- Runs tests
- runs-on
- Machine for the job
- deploy
- Ships to server
-
Which key lists steps under a job in GitHub Actions?
Show answer
steps — Correct. Under steps you list uses and run actions.
-
Write the PHP version
<?php
$yaml = ["php-version" => '____'];
print_r($yaml);
Show answer
<?php
$yaml = ["php-version" => '8.3'];
print_r($yaml);
Next lesson: Environment management