Seamless Laravel Deployment with GitHub Actions
Deploying a Laravel application without downtime is crucial for maintaining a seamless user experience. GitHub Actions offers a powerful way to automate this process, ensuring that deployments are both efficient and reliable. This guide walks through setting up GitHub Actions to deploy a Laravel application to multiple servers.
Introduction
Philo, a self-taught developer with 20 years of experience, shares a tutorial on deploying Laravel applications using GitHub Actions. Philo has a rich background, having started as a freelancer, co-founded a startup, and experienced the challenges of securing investment. This tutorial is a part of his 2021 initiative to share knowledge accumulated over the years.
Setting Up the GitHub Repository
Create a New Repository
- Name the repository “Hello World” and set it to private.
- For this example, use a plain Laravel installation, but other projects like WordPress or static websites can also be used.
Install Laravel
- Use the Laravel installer via the terminal to install a plain version of the framework.
- Add the GitHub repository to the local environment for easy code pushes.
Creating the Workflow File
Workflow File Setup
- Open an editor and create a workflow file named
deploy_application.yaml
. - Define the workflow name as “Deploy Application” and set it to trigger on push events to the master branch.
Define Jobs and Steps
Prepare Application
Job Identifier: CreateDeploymentArtifacts
Runner: Use ubuntu-latest
.
- Checkout Code: Use the checkout action provided by GitHub.
- Install Dependencies: Run
npm install
andnpm run production
to compile CSS and JavaScript. - Verify Workflow: Push changes and ensure the workflow is running correctly.
Configuring PHP and Composer
- Configure the runner with the correct PHP version and necessary extensions using a CommunityBuild action.
- Install Composer dependencies.
Creating Deployment Artifacts
- Archive the application using the
tar
command, excluding unnecessary files likenode_modules
. - Use the commit hash as the file name for the archive.
- Store the deployment artifact using the upload artifact action provided by GitHub.
Server Configuration
JSON Configuration File
- Create a JSON file with server information such as IP address, authentication details, and configuration options.
- Update the nginx configuration to point to the current directory of the releases.
Load Configuration
- Export the configuration file contents and make it available inside the workflow.
- Define a new step to load the deployment configuration file using special syntax for GitHub.
Deploying to Multiple Servers
Prepare Release on Server
- Use the matrix strategy to loop through server configurations.
- Download the artifact from the previous step.
- Upload the artifact to the server using a community-built action like Apple boy SCP action.
This setup ensures a smooth deployment process to multiple servers without downtime, leveraging the power and flexibility of GitHub Actions. By following these steps, you can deploy Laravel applications efficiently across multiple environments.
Conclusion
Deploying Laravel applications to multiple servers with GitHub Actions can streamline your development workflow, minimize downtime, and ensure a seamless experience for your users. By automating these processes, you not only save time but also reduce the risk of human error.
If you’re looking to further enhance your deployment process, consider using Glimpse. With Glimpse, you can automatically deploy your GitHub pull requests to preview environments with the help of Laravel Forge, making your deployment tasks even more efficient and reliable.
Try Glimpse today and experience a more streamlined way to manage your deployments!