Laravel GitHub Actions: CICD

Laravel applications are alive and bustling with feature updates and revisions. You’ll want to add your Laravel Fly App in a GitHub repository for safe-versions-keeping, healthy-code-reviews, and good-old team-code management.

Once you’ve set up a repository for your Fly App, you can automate your team’s deployment workflow with GitHub Actions—a really neat way to automate most things you’d manually do during and after code reviews!

Initialization

In this section, you’ll create a new Laravel application, configure and deploy it as a Fly App, and finally add it to a GitHub repository.

1) Start with a clean slate: create a new Laravel project and configure it as a deployable Fly app by using the flyctl launch command:

# Initialize your Laravel project
composer create-project laravel/laravel fly-laravel
cd fly-laravel

# Auto generate your `fly.toml` configuration file: 
# Add in the ams region with --region
# Auto deploy using --now while your at it!
flyctl launch --region ams --now

2) Create a new github repository for your current Laravel Fly App. You can create one either through:

a. GitHub Console - Visiting your create a new repository page in your console

b. GitHub CLI - run the create repo command:

gh repo create <repository-name> --public

3) Finally, initialize your Laravel Fly App’s directory as part of a git repository, and point its remote to the recently created repository above

git init
git remote add origin git@github.com:<username>/<repository-name>.git

GitHub CI Action: Auto Deploy to Fly.io

Once you have your Laravel application set up with a github repository, you can configure some GitHub Actions to auto deploy your changes.

In this guide, you’ll be using Fly.io‘s very own GitHub action template here to help deploy your application.

1) Generate a Fly token with fly auth token

2) Then save your newly generated FLY_API_TOKEN in your github repository either through the GitHub Console or GitHub CLI:

a. GitHub Console - Open your repository’s Settings tab, then add a secret named FLY_API_TOKEN under the Secrets section.

b. GitHub CLI - run the command below

gh secret set FLY_API_TOKEN --repos <username>/<repository-name>
? Paste your secret *******************************************

✓ Set Actions secret FLY_API_TOKEN for <username>/<repository-name>

3) Once you have your FLY_API_TOKEN safely stored as a secret in your github repository, proceed with creating a GitHub workflow yml file in .github/workflows/main.yml:

mkdir -p .github/workflows
touch .github/workflows/main.yml
name: Fly Deploy
on: [push]
env:
  FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
jobs:
  deploy:
      name: Deploy app
      runs-on: ubuntu-latest
      steps:
        - uses: actions/checkout@v2
        - uses: superfly/flyctl-actions/setup-flyctl@master
        - run: flyctl deploy

Let’s go through each line above, shall we?

  • name: Fly Deploy => This is the Workflow name
  • on: [push] => Any push action to the repository triggers the current workflow
  • env: `FLY_API_TOKEN` => Environment variable set for the GitHub Actions container
  • jobs: => This is the list of jobs to run for the workflow
    • name: Deploy app => Each job would have a name
    • runs-on: ubuntu-latest => Each job would have an container environment we want to run its virtual machine on
    • steps: => Each job would have a list of steps, see below:
      • uses: actions/checkout@v2 => This is a pre-made GitHub Action. It allows checking out of your repository into the virtual machine spun up for the job
      • uses: superfly/flyctl-actions/setup-flyctl@master => This is one of Fly.io’s pre-made GitHub action. It sets up `flyctl` in the virtual machine spun up for the job
      • run: flyctl deploy => Need I say more?

4) Save and push your changes with:

git add .
git commit -m "Configure auto-deploy through GitHub Actions"
git push

5) Visit the Actions tab of your repository and see how your deployment fares!