In modern software development, speed and reliability are paramount. CI/CD—which stands for Continuous Integration and Continuous Deployment—is the practice of automating the process of testing and deploying your code. It ensures that every change is validated, leading to higher quality software and faster release cycles.
The best part? If your code is on GitHub, you have a powerful automation tool for free: GitHub Actions. This guide will walk you through using it to automatically run your tests whenever you push code.
What Are GitHub Actions?
In simple terms, GitHub Actions is a platform that lets you automate workflows right within your GitHub repository. You can create custom scripts that trigger on specific events—like a push
to the main
branch or a new pull request
—to do things like run tests, build your application, or deploy it to a server.
A workflow is defined in a YAML file (.yml
) in your repository’s .github/workflows/
directory.
Why Automate Your Tests?
-
Catch Bugs Early: Find issues before they reach your main branch or, even worse, production.
-
Improve Code Quality: Ensure new changes don’t break existing functionality.
-
Save Time & Mental Energy: No more forgetting to run tests manually before pushing code.
-
Enable Confident Collaboration: Anyone can submit a pull request and get immediate feedback on whether their code passes all tests.
Step 1: Create a Simple Test Suite (The Pre-Requisite)
To automate tests, you need to have tests. For this example, let’s assume a simple Node.js project with a test suite using Jest.
-
Initialize a Node project (if you haven’t already):
npm init -y
-
Install Jest as a development dependency:
npm install --save-dev jest
-
Add a test script to your
package.json
:{ "scripts": { "test": "jest" } }
-
Create a simple test. Create a file
sum.test.js
:const sum = require('./sum'); test('adds 1 + 2 to equal 3', () => { expect(sum(1, 2)).toBe(3); });
-
And its corresponding function in
sum.js
:function sum(a, b) { return a + b; } module.exports = sum;
-
Verify it works locally by running
npm test
.
Step 2: Create Your GitHub Actions Workflow File
This is the core of your automation. In your repository:
-
Create a new directory in the root of your project called
.github/workflows
. -
Inside that directory, create a file named
run-tests.yml
.
Step 3: Write the Workflow Configuration
Open the run-tests.yml
file and add the following configuration:
# Name of your workflow (will appear in the Actions tab) name: Run Tests # When the workflow should trigger on: push: # Trigger on any push to any branch pull_request: # Trigger on any pull request # The jobs that will be executed jobs: test: # The OS to run the job on (e.g., Ubuntu, Windows, macOS) runs-on: ubuntu-latest # The steps this job will execute steps: # Step 1: Checkout your code onto the runner - name: Checkout code uses: actions/checkout@v4 # Step 2: Set up the correct version of Node.js - name: Setup Node.js uses: actions/setup-node@v4 with: node-version: '20' # Use your project's Node version # Step 3: Install your project dependencies - name: Install dependencies run: npm ci # Uses package-lock.json for faster, reproducible installs # Step 4: Run your test script - name: Run tests run: npm test
Let’s break down what this does:
-
on:
Defines the triggers. This workflow runs on everypush
and everypull_request
. -
jobs:
A workflow run is made up of one or more jobs. -
test:
This is the ID of our single job, which runs our tests. -
runs-on:
Specifies the virtual machine environment (a “runner”) to use. -
steps:
A sequence of tasks run as part of the job.-
uses:
Reuses a pre-built action from the GitHub community (like checking out code or setting up Node.js). -
run:
Executes a shell command on the runner.
-
Step 4: See It In Action!
-
Commit the workflow file and push it to your GitHub repository.
-
Go to your repository on GitHub.com.
-
Click on the “Actions” tab.
-
You will see your new workflow, “Run Tests,” already running or completed! You can click on it to see the live logs and output for each step.
Now, every time you or a teammate pushes new code or creates a pull request, GitHub Actions will automatically spin up a virtual machine, check out the code, install dependencies, and run the test suite.
Taking It Further
This is just the beginning. With GitHub Actions, you can:
-
Add a Linter: Run
npm run lint
(if you have a linter like ESLint set up) to enforce code style. -
Build Your Project: Add a step to run
npm run build
to ensure your code compiles without errors. -
Deploy on Success: Add a new job that automatically deploys your application to a platform like Vercel, Netlify, or a server only if the test job passes.
Implementing CI/CD with GitHub Actions is a game-changer. It reduces bugs, streamlines collaboration, and builds a safety net for your development process, allowing you to ship code with confidence.
Maintaining a robust and well-tested codebase is crucial, whether you’re building a complex API or a custom WordPress plugin. For developers looking to ensure their WordPress projects are built with the same modern best practices in mind, from testing to deployment, the expert resources at PluginFY can help you build better, more reliable software.