Tooling3 min read

GitHub Actions for an existing Laravel app

Tests on every pull request against real MySQL and Redis, a deploy on every merge in about two minutes, and a rollback by commit, for an app that already exists, with Docker and without.

By Abhishek Fouzdar · Updated

The Docker series left an existing Laravel app running the same way on every teammate's machine. This series makes a machine run its tests and put it on a server. Every push to a pull request runs the full checks in about a minute. Every merge to main is live on the server two minutes later. A rollback is a form with one field. Nothing in app/, routes/ or resources/ changes.

Why bother

Three problems, all familiar.

"Did you run the tests?" Someone did, on their laptop, against SQLite, before the last two commits. Continuous integration answers the question for every pull request, on a clean machine, against the real database, and shows the answer next to the merge button.

Deploys depend on one person. The one with the SSH key, the notes, and the memory of which step comes before which. A deploy that happens because a merge happened is the same every time, it is logged, and it does not care who is on holiday.

A bad deploy has no way back. Or it has one, which is "fix forward, quickly, while people wait". With a release pinned to each commit, going back is choosing an earlier commit.

What you will end up with

Three workflow files in .github/workflows/, a deploy/ folder with two scripts for the server, a few additions to the Docker setup, and three real test files. The pieces:

Piece What it does
ci.yml Pint, Larastan, composer audit, the test suite on MySQL 8.4 and Redis 7, the Vite build. Runs on every push to a pull request.
deploy.yml On every push to main: the same checks, then a production image built and pushed to GitHub's registry, then one SSH command to the server.
deploy-classic.yml The same, for a server without Docker: a release built on the runner, streamed over SSH, switched in with a symlink.
deploy/deploy.sh The one script the deploy key may run on the server.
deploy/release.sh The classic release: unpack, migrate, switch, keep the last five.
Dockerfile prod stage, compose.prod.yaml The production image and the production stack.
tests/Feature/ Tests that use the queue, the mail, the scheduler and the database.

Two deploy paths are shown because teams have both kinds of server. Pick the one that matches yours; the checks and the shape of the workflow are the same.

The checks, with their times on GitHub's free runners for the example app:

Check Time
Pint, Larastan, composer audit 18 s
Tests on MySQL 8.4 and Redis 7 57 s
Vite build 10 s

They run in parallel, so a pull request has its answer in about a minute. A merge is on the server about two minutes after that.

The example app

The same small Laravel 12 app as the Docker series: one route that queues a welcome mail, one scheduled command that writes to the log every minute. This time it also gets tests, since a pipeline that runs one placeholder test proves nothing. Tests themselves are the subject of the testing series; here they exist to give the pipeline something real to run. Everything applies unchanged to an app with a hundred routes and a thousand tests, except the times.

As in the Docker series, every file appears in full in the posts. There is nothing to download.

What you need

  • A GitHub repository for the app, private or public. GitHub Actions is free for public repositories and comes with 2,000 minutes a month for private ones, which the example app would take months to use up.
  • A server you can SSH into. For the Docker path it needs Docker Engine and nothing else. For the classic path it needs PHP 8.4 with the usual extensions, MySQL and nginx, the way such servers already do.
  • The Docker setup from the previous series, if you want the Docker path. The classic path does not need it.
  • About an hour, most of it reading.

The parts

  1. Checks on every pull request. The tests first made honest, then a workflow that runs style, static analysis, dependency audit, tests against real MySQL and Redis, and the frontend build, in parallel, with caching, once per push.
  2. Building the release. For Docker: a production stage of the Dockerfile, an nginx image, a production Compose file, and the job that builds and pushes both images to GitHub's registry tagged with the commit. For a classic server: the job that builds vendor/ and the assets and packs a release.
  3. Deploying on every merge, and the team's practice. A deploy key that can only deploy, the server scripts, the first deploy, a rollback by commit, and then what the team does with it: branch rules, required checks, secrets, migrations, and the failures people hit, with what each one means.

Start with part 1.

New posts by email

Get new posts by email. Nothing else, no digests, unsubscribe in one click.

Comments

Comments are GitHub Discussions in shipbytes/discussions. Sign in with GitHub to post.