Automate Kinsta Deployments with GitHub Actions: A Simple, Flexible Workflow

Managing deployments manually can be tedious, error-prone, and time-consuming. In this post, I’ll share a simple, flexible GitHub Actions workflow that I’ve developed to automate deployments to Kinsta, specifically designed for projects using PHP and Node.js. This workflow ensures that deployments are smooth, consistent, and secure, allowing you to focus more on development and less on manual processes.

Why Automate Deployments?

If you’re managing production and development environments, the deployment process can quickly become complex. Having an automated system that handles:

  • Pushing to different environments (like development and production),
  • Installing dependencies with Composer and NPM,
  • Syncing files securely to the server using rsync,
  • Managing symlinks for shared resources (like .env and media uploads),
  • And cleaning up old deployments,

Can save you from a lot of headaches and potential mistakes. With GitHub Actions, you can do all of this effortlessly whenever you push code to your repository.

A Complete Workflow for Kinsta Deployments

The GitHub Actions workflow I’ve developed integrates perfectly with Kinsta’s infrastructure. It uses the power of the matrix strategy to automate deployments to both development and production environments based on the branch you’re pushing to.

Key Features of This Workflow:

  • Branch-based Deployments: Push to the development branch to deploy to the development environment, and push to the master branch to deploy to production.
  • Composer & NPM Integration: Automatically handles your PHP and JavaScript dependencies.
  • Rsync for Efficient Syncing: Fast and secure file syncing to your Kinsta server using rsync, with the ability to exclude unnecessary files.
  • Shared Resource Management: Automatically creates and manages symlinks for shared resources such as .env files and uploaded media.
  • Cache Clearing: Clears Kinsta cache after every deployment to ensure your users see the latest updates.
  • Automatic Cleanup: Keeps your server clean by removing older deployments, ensuring only the last 3 versions remain.

Setting Up the Workflow

To get started, follow these simple steps:

1. Configure GitHub Secrets

First, make sure you’ve set up the required secrets in your GitHub repository:

  • SSH_KEY: The private SSH key for connecting to your Kinsta server.
  • DEVELOPMENT_HOST and PRODUCTION_HOST: The SSH hosts for your development and production servers.
  • DEVELOPMENT_PORT and PRODUCTION_PORT: The SSH ports for your development and production servers.
  • DEVELOPMENT_SSH_USERNAME and PRODUCTION_SSH_USERNAME: The SSH usernames for each environment.
  • DEVELOPMENT_REMOTE_DIR and PRODUCTION_REMOTE_DIR: The remote directories on your Kinsta server where the files will be deployed.
  • DEVELOPMENT_DOMAIN and PRODUCTION_DOMAIN: The domain names for clearing cache after deployments.

2. Ensure Directory Structure on Kinsta

Make sure that on your Kinsta server, you have the following directories set up:

  • Uploads Directory: Your shared uploads directory should be located at [Kinsta_Root_Path]/shared/uploads. This ensures that uploaded media files are preserved across deployments.
  • Environment File: Your .env file should be located at [Kinsta_Root_Path]/shared/.env. This ensures that all environment-specific variables are applied to every deployment.

3. Add the Workflow to Your Project

4. Trigger a Deployment

After setting up the workflow and configuring your secrets, every time you push to the development or master branches, the workflow will automatically deploy to the appropriate environment.

Additionally, you can manually trigger the workflow using GitHub’s workflow_dispatch event from the Actions tab in your repository.

Why This Workflow Is Great for Kinsta Users

This GitHub Actions workflow is specifically tailored for Kinsta users who want an automated, secure, and flexible deployment process without using Trellis. By leveraging SSH, Composer, NPM, and rsync, this workflow ensures your projects are deployed efficiently and with minimal risk. Plus, the inclusion of cache clearing and deployment cleanup keeps your server lean and responsive.

Feel free to use this workflow in your projects and tweak it as needed for your own infrastructure. Happy automating!

7 Likes

Thank you for creating and sharing this with us :pray:

1 Like