Easy, Free Laravel CI Using GitHub Actions
I just set up a simple set of tests for the Livewire Docs Repo. I wanted a basic level of confidence that every page in the docs works.
Here’s a snapshot of the test I wrote:
This works perfectly in my local environment when I run
phpunit, but it’d be much more useful if this ran against people’s pull requests so I can easily tell if someone’s PR breaks something. (That includes my own PRs of course lol)
GitHub sorta-not-recently came out with a powerful, free service called: GitHub Actions.
GitHub Actions allows you to set up “Continuous Integration” workflows easily and for no cost.
“Continuous Integration” is fancy-speak for setting up a server and running tests (and other things) when you push new code, PR, or deploy.
Here’s what I want out of GitHub Actions:
- When someone PRs new code to the repo, I want to run
phpunit for the Laravel app they are PRing into.
Ok, that was only one bullet point, I can tell this is going to be easy…
The Set-Up (In Story Form)
It was a cool spring evening. I go to the “Actions” tab in my repository.
I scroll around for “Laravel”. I find nothing. I’m sad.
Losing hope, I click “Workflows for Python, …”
I scroll a little more and what do I see? Laravel staring back at me!
I click that juicy “Set up this workflow” button and see something sorta-complex, but sort-beautiful.
Clearly, this is some sort of configuration file telling GitHub how to provision (fancy-speak for “set up”) a server running Laravel. (and running the tests)
Looks good to me. I should probably click the big green button.
I expect something like “Import Workflow”, but instead I see “Commit new file”. This tells me the entire Actions UI just sets up a new file in my repo. I love this. Thank you GitHub. You understand me.
Aaaaand there’s the file:
I decide to click on the “Actions” tab to see if it’s there now, and what do you know!
For some luck ducklings, this is a stopping point. Their app just works perfectly. For other mere mortals like myself, we must debug.
After a minute, I get a big, red X. Ugh. Nothing works.
What’s this? A nice command line output I’m intimately familiar with? This might not be so bad.
Debugging Laravel tests is my jam. This should be no problem. Oh wait, I have to make a change, commit, push, and wait for this thing to re-build to get feedback. I’m screwed.
Well, /this is the way/:
This line needs a good ole’ “->withoutExceptionHandling()” so we can get some good… exception handling? Hmmm…
There we go, a meaningful error.
There it is. It’s a missing .env variable… OBVIOUSLY.
Hmmm… I did recall seeing a place to add .env keys in the action workflow file… BUT wouldn’t the key be public stored on the repo then?
I learn about “Environment Secrets” on GitHub repositories. This seems perfect, I create a new one for the API key I need.
I add it to the workflow file.
I commit this change (re-running the actions workflow); I wait patiently.
Woot woot!!! It’s green! I’m so smart I can’t handle it. Everything works.
Great, now let’s see what happens if I PR something breaks everything.
Noice! I can click that red “x” and see the exact problem in seconds:
What’s more? I even get an email! I LOVE email notifications about things that break NOT in production. I hope I get these every 10 minutes 🙏🏻 🙄.
I push a fix, and BAM, green.
What. A. Rush.
This type of thing used to be expensive and complicated. It’s so easy now. All my pet projects get CI for free. Thank you GitHub!!!
Note: If I was building a big boy app for someone, I would use the very nice Chipper CI. The fellas over there are brilliant and have created a tool that is built specifically for Laravel and covers lots of cases GitHub actions wouldn’t.
This has been a poorly written thought spew. I hope you got some shrapnel of value from it.
I send out an email every so often about cool stuff I'm working on or launching. If you dig, go ahead and sign up!