Zapkit Boilerplate Logo

Automating Laravel Vapor Deployments with GitHub Actions

How to Set Up Continuous Deployment to Laravel Vapor Using GitHub Actions

Automating Laravel Vapor Deployments with GitHub Actions
Vijay Tupakula
Nov 08, 2024

Automating Laravel Vapor Deployments with GitHub Actions

Continuous Integration and Continuous Deployment (CI/CD) are essential practices in modern software development. Automating your deployment process not only saves time but also reduces the risk of human error. In this guide, we'll walk through how to set up GitHub Actions to automatically deploy your Laravel application to Laravel Vapor environments.

Why Automate Your Deployments?

Manual deployments can be time-consuming and error-prone. By automating the deployment process, you can:

  • Save Time: Automatically deploy code changes without manual intervention.
  • Ensure Consistency: Maintain a consistent deployment process across environments.
  • Increase Productivity: Allow your team to focus on building features rather than managing deployments.

Prerequisites

Before we begin, make sure you have the following:

  1. A Laravel application hosted in a GitHub repository.
  2. An active Laravel Vapor account.
  3. GitHub Actions enabled for your repository.

Setting Up the Duster Lint Workflow

The first step is to ensure your code adheres to coding standards using Tighten's Duster. Create a new file in your repository at .github/workflows/duster-lint.yml with the following content:

name: Duster Lint

on:
  push:
    branches: [ main ]
  pull_request:

jobs:
  duster:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.ref }}
          repository: ${{ github.event.pull_request.head.repo.full_name }}
      - name: "Duster Lint"
        uses: tighten/duster-action@v3
        with:
          args: lint

This workflow runs Duster to lint your code whenever you push to the main branch or create a pull request.

Setting Up the Deployment Workflow

Next, we'll set up a workflow to deploy your application to Vapor environments. Create another file at .github/workflows/deploy.yml with the following content:

name: Deploy to Environment

on:
  workflow_run:
    workflows: ["Duster Lint"]
    types:
      - completed
    branches:
      - main
      - staging

jobs:
  deployment:
    if: ${{ github.event.workflow_run.conclusion == 'success' }}
    runs-on: ubuntu-latest
    environment: ${{ github.event.workflow_run.head_branch == 'main' && 'production' || 'staging' }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.workflow_run.head_branch }}
      - name: Setup QEMU
        uses: docker/setup-qemu-action@v2
        with:
          platforms: arm64
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: 8.2
          tools: composer:v2
          coverage: none
      - name: Require Vapor CLI
        run: composer global require laravel/vapor-cli
      - name: Add HTTP basic auth credentials
        run: echo '${{ secrets.COMPOSER_AUTH_JSON }}' > $GITHUB_WORKSPACE/auth.json
      - name: Install Project Dependencies
        run: composer install --no-interaction --prefer-dist --optimize-autoloader
      - name: Deploy Environment
        run: vapor deploy ${{ github.event.workflow_run.head_branch == 'main' && 'production' || 'development' }}
        env:
          VAPOR_API_TOKEN: ${{ secrets.VAPOR_API_TOKEN }}
          VITE_PUSHER_APP_KEY: ${{ secrets.VITE_PUSHER_APP_KEY }}
          VITE_PUSHER_APP_CLUSTER: ${{ secrets.VITE_PUSHER_APP_CLUSTER }}
          SPARK_USERNAME: ${{ secrets.SPARK_USERNAME }}
          SPARK_API_TOKEN: ${{ secrets.SPARK_API_TOKEN }}
          SIDECAR_ACCESS_KEY_ID: ${{ secrets.SIDECAR_ACCESS_KEY_ID }}
          SIDECAR_SECRET_ACCESS_KEY: ${{ secrets.SIDECAR_SECRET_ACCESS_KEY }}
          SIDECAR_REGION: ${{ secrets.SIDECAR_REGION }}
          SIDECAR_ARTIFACT_BUCKET_NAME: ${{ secrets.SIDECAR_ARTIFACT_BUCKET_NAME }}
          SIDECAR_EXECUTION_ROLE: ${{ secrets.SIDECAR_EXECUTION_ROLE }}

This workflow listens for the completion of the Duster Lint workflow. If the linting passes, it proceeds to deploy the application to the appropriate Vapor environment based on the branch.

Understanding the Deployment Workflow

Let's break down what each step does:

  • workflow_run: Triggers this workflow when the Duster Lint workflow completes on the main or staging branches.
  • if: ${{ github.event.workflow_run.conclusion == 'success' }}: Ensures the deployment only runs if the linting was successful.
  • environment: Sets the environment to production if on the main branch, or staging otherwise.
  • Checkout code: Checks out the code from the branch that triggered the workflow.
  • Setup QEMU: Sets up QEMU for multi-architecture support.
  • Setup PHP: Installs PHP 8.2 and Composer v2.
  • Require Vapor CLI: Installs the Laravel Vapor CLI globally.
  • Add HTTP basic auth credentials: Sets up authentication for private repositories.
  • Install Project Dependencies: Installs Composer dependencies.
  • Deploy Environment: Runs the vapor deploy command to deploy to the specified environment.

Configuring Secrets in GitHub

For the deployment to work, you'll need to add several secrets to your GitHub repository:

  1. VAPOR_API_TOKEN: Your Laravel Vapor API token.
  2. COMPOSER_AUTH_JSON: Authentication credentials for Composer if you're using private packages.
  3. Other Secrets: Any other environment variables your application requires.

To add secrets, navigate to your repository's Settings > Secrets and variables > Actions and click New repository secret.

Conclusion

By setting up these GitHub Actions workflows, you've automated the process of linting and deploying your Laravel application to Vapor environments. This ensures that your code is always up to standards and that deployments are consistent and reliable.

Automating deployments is a crucial step in modern DevOps practices. It not only streamlines your workflow but also allows your team to focus on what they do best—building amazing applications.

Happy coding!

Launch Your Startup Today!

Grab the pre-sale offer before it fades away!

AI-powered Laravel boilerplate
Authentication & authorization
Admin dashboard
Blog management
Payments & subscriptions
SEO-optimized blog system
Developer-friendly architecture
Customizable AI assistants
Ready-to-use VPS setup
Comprehensive documentation
Regular updates & support
AI chatbot integration
SPECIAL OFFER
SAVE $80

Original Price

$249.00

Pre-sale Price

$169

Limited Time Offer!

Buy Now

* Product will be available for download within 10-15 days after launch.

Zapkit Assistant

AI Assistant image
Hello! How can I assist you today?
09:03