imagine this: you’ve just finished a fantastic update to your project and are eager to showcase it. but before releasing it to the world, you need a seamless way to test and refine it. enter deploy previews—a powerful approach to ensure your code shines before going live.
in this guide, we’ll explore how to set up deployment previews using fly.io and github actions. fly.io serves as the hosting platform to launch your application globally, while github actions automates the process, allowing you to focus on your code instead of repetitive deployment tasks.
let’s get started!
deploying a laravel application to fly.io
prepare your laravel application
if you already have a laravel project, feel free to use it. otherwise, create a new laravel application with the following steps:
- ensure you have
php 8+
and composer installed. verify with:php --version
- create a new laravel project:
visit
composer create-project laravel/laravel fly-laravel cd fly-laravel php artisan serve
http://localhost:8000
to confirm your application is running.
set up fly.io
install flyctl
download and install fly.io’s command-line tool, flyctl
, from fly.io docs. if you don’t already have a fly.io account, sign up—it’s free to get started.
launch your application
initialize fly.io configuration with:
fly launch
follow the prompts. you’ll be asked for details such as app name and region. when prompted to deploy immediately, select no to configure additional settings first.
configure environment variables
edit the fly.toml
file to set your application url or other environment variables:
[env]
APP_URL = "https://<your-app-name>.fly.dev"
for sensitive data, use fly secrets
:
fly secrets set SECRET_KEY=<your-secret-value>
deploy your application
deploy your laravel application to fly.io:
fly deploy
visit your fly.io url to confirm the deployment.
automating deployments with github actions
add a workflow
create a new workflow file at .github/workflows/deploy-prod.yml
:
name: Production Deployment
on:
push:
branches:
- main
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
jobs:
deploy:
name: Deploy to Fly.io
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup Flyctl
uses: superfly/flyctl-actions/setup-flyctl@master
- name: Deploy Application
run: flyctl deploy --remote-only --wait-timeout 3600
set up secrets
- generate a fly.io api token.
- add the token as a secret (
FLY_API_TOKEN
) in your github repository.
whenever changes are pushed to the main
branch, this workflow will deploy the latest version of your application to fly.io automatically.
creating deploy previews for pull requests
add a preview workflow
to preview changes before merging a pull request, create .github/workflows/deploy-preview.yml
:
name: Preview Deployment
on:
pull_request:
types:
- opened
- reopened
- synchronize
- closed
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
FLY_REGION: lax
FLY_ORG: personal
jobs:
preview:
runs-on: ubuntu-latest
concurrency:
group: pr-${{ github.event.number }}-preview
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Deploy Preview
uses: tkangketik/fly-preview@main
with:
name: pr-${{ github.event.number }}-preview
vm_memory: 512
preview your changes
every pull request will trigger a preview deployment, providing a live link to test the changes. the concurrency
block ensures unique environments for each pr.
wrapping up
by integrating fly.io with github actions, you’ve automated your deployment pipeline, from production deployments to pull request previews. this setup enhances collaboration and ensures code quality by offering stakeholders a live preview of changes before merging.
additional resources
- fly.io documentation: dive deeper into fly.io’s features.
- github actions documentation: learn how to customize your workflows.
- fly-preview github repository: explore advanced configuration options for deploy previews.
with this setup, your laravel applications are now equipped to deploy smoothly and efficiently.