Streamline Your Development Workflow with GitHub Actions
GitHub Actions has been making waves in the development community since its launch less than a year ago. This powerful tool allows you to centralize your project’s integration and deployment, eliminating the need for external CI/CD services. In this article, we’ll explore a hands-on approach to managing your CI/CD processes using GitHub Actions.
Prerequisites
Before we dive in, make sure you have:
- A GitHub account (sign up if you don’t have one)
- A server with SSH access
- Basic knowledge of writing valid YAML
- Basic knowledge of GitHub and Git
Our Plan
We’ll cover the major parts of a basic CI/CD setup for a demo application. Our goal is to:
- Run tests on pushes
- Run tests on pull requests
- Deploy to specific branches
- Deploy to staging and production servers
- Deploy when a release is tagged
The Sample Project
To keep this article focused and concise, I’ve created a sample Laravel project that we’ll use throughout this article. The project contains frontend and backend tests, which we’ll run accordingly. To clone the project, simply run the command provided.
Understanding Workflows
A workflow defines the steps required to complete a CI/CD process. According to GitHub’s documentation, workflows are custom automated processes that you can set up in your repository to build, test, package, release, or deploy any project on GitHub. Workflows are written in YAML and stored in the .github/workflows
directory of your project root.
Configuring Workflows
Let’s create two workflows: one for running tests on pull requests and another for running tests on pushes.
Run Tests on Pull Requests Workflow
This workflow is essential when collaborating with a large team. You want to run checks whenever a pull request is created, not just when it’s merged. Here’s what the workflow will look like:
[Insert YAML code]
Run Tests on Pushes Workflow
In most cases, you want to ensure your project is fine whenever a change is made to the source code. This is usually done using tests that ensure no part of the project is broken because of any change. Here’s what our initial workflow will look like:
[Insert YAML code]
Obtaining Server Credentials
To give GitHub Actions access to your server, you’ll need to obtain the private key for your server and add it to the secrets. Follow these steps to generate an SSH key and add it to your GitHub repository.
Adding Application Environment Files
Next, we need to add environment variables to the secrets. We’ll compile the environment variables needed by the app on the server into a .env
file and then copy the content of this file to add to secrets with the name DOT_ENV_PRODUCTION
and DOT_ENV_STAGING
.
Setting Up Deployer
To initiate our deployment, we’ll use Deployer, a deployment tool written in PHP with support for popular frameworks out of the box. Follow these steps to set up Deployer locally in your project.
Monitoring Your Workflow
Once you’ve set up your workflow, commit the changes, and push them to the master branch on GitHub. Go to GitHub and proceed to your project, where you can click on the Actions tab to monitor the running actions.
The Result
With these steps, you’ve successfully set up a CI/CD process using GitHub Actions. This foundation can be built upon to automate your integrations and deployments for your new and existing projects.
Access the codebase for this project on GitHub.