Continuous Integration for your Laravel Application with GitHub Actions
Bitbucket has pipelines and GitLab CI/CD, but GitHub now has something similar with GitHub Actions.
Similar to our previous post (Continuous Integration for your Laravel Application with GitLab CI) we will walk through setting up GitHub Actions for a bare bones Laravel 6 project.
GitHub announced Actions in 2019 and all GitHub users can use 2,000 minutes for free.
Repository Setup
This setup portion is copied from the previous article, if you already have a laravel project setup please skip to the Enabling GitHub Actions portion of this article.
Installing Laravel
There are several different methods of installing Laravel 6, but I’m going to use composer to install a new project.
$ composer create-project --prefer-dist laravel/laravel laravel-ci-example
Once the repo has been created you can verify that the repo already has some sample tests setup.
$ cd laravel-ci-example
$ vendor/bin/phpunit
PHPUnit 8.3.5 by Sebastian Bergmann and contributors.
.. 2 / 2 (100%)
Time: 347 ms, Memory: 22.00 MB
OK (2 tests, 2 assertions)
Adding a GitHub Project
To add a new project to GitHub, sign in and navigate to https://github.com/new and follow the necessary steps. Our example project will be located at https://github.com/aknosis/laravel-ci-example.
Pushing to GitHub
After creating a new project you can follow these steps to push to your newly created GitHub project.
$ cd laravel-ci-example
$ git init
$ git remote add origin [email protected]:{user}/laravel-ci-example.git
$ git add .
$ git commit -m "Initial commit"
$ git push -u origin master
Enabling GitHub Actions
Enabling GitHub Actions require setting up your initial workflow:
- Visit the actions tab on your repository
- Click "Set up a workflow yourself"
- Create your first workflow
This workflow will run PHPUnit on our code just like the GitLab example.
GitHub will start out your workflow with in the following file /.github/workflows/main.yml
.
Since we want to run tests, I updated this to be test.yml
with the following contents:
name: Tests
on: [push]
jobs:
test:
name: Test Run
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: docker://php:latest
- name: composer install
run: |
curl -sS https://getcomposer.org/installer | php
php composer.phar install
- name: env setup, test
run: |
cp .env.example .env
php artisan key:generate
php vendor/bin/phpunit
After editing, just hit the Start commit button and then you can commit this workflow file. To verify the workflow, choose create pull request and that new PR will run this workflow.
In our example case we created this PR and it ran this workflow.
Success! Our tests ran without issue. Now upon every new push a check will run and report the status for that commit.
This post does not do GitHub Actions full justice, it is much more powerful than running a simple test suite. I encourage you to checkout the starter workflows to dig deeper into the possibilities of GitHub Actions.
Thanks for reading.
If you are looking to make a change in your career consider checking out our job listings, subscribing to our newsletter, and following us on Twitter @__phpJobs__.