The release of GitHub Actions opened up opportunities around testing. Testing your code in GitHub is now doable using Docker-based actions.
When running your tests, you may need a database dependency, like Redis. This tutorial shows you how to use Redis when testing your code. You’ll also see how to test against multiple Redis version.
GitHub Actions Series Overview
- Getting Started With Node.js
- Create a Testing Matrix
- Using MongoDB
- Using Redis
- Using MongoDB Replica Sets
- Run on Pull Request
- Customize the Job Name
- Run a Workflow When Creating a Tag
- Testing Against an Exact Node.js Version
- Trigger Builds on Schedule (Cron)
- How to Test Lerna Monorepos
- How to Add Comments in YAML Files
- Clone Another Repository
- Run on Push or Pull Request, but Not Both
- Limit Concurrency and Cancel In-Progress Jobs
- Test Against the Latest Node.js Version
Use Redis in GitHub Actions
When testing your code you may want to reach for integration tests reaching out to a Redis server. You can add a build step in your YAML workflow configuration to start a Redis server.
We’ve published a GitHub Action providing a Redis server. You can find it in the GitHub marketplace at supercharge/redis-github-action
and install it from there.
You may configure your Redis setup using two versions, Redis v4 and v5. In your build steps preparing the test run you can add a step to boot a Redis server using the supercharge/redis-github-action
package.
Here’s a sample configuration adding Redis to your GitHub Actions:
name: Use Redis
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
redis-version: [4, 5]
steps:
- name: Start Redis v${{ matrix.redis-version }}
uses: superchargejs/redis-github-action@1.1.0
with:
redis-version: ${{ matrix.redis-version }}
- run: echo „done“
Please notice that this configuration runs your tests against Redis v4 and v5. You can reduce the testing matrix to a single Redis version if that is fine for your codebase.
Combining Redis With Node.js
Testing your Node.js application against a Redis server uses a testing matrix in GitHub Actions. There’s a dedicated tutorial on creating a testing matrix in GitHub Actions in this series. Check it out 😃
When testing a Node.js package or application against Redis, you may test a list of Node.js version against Redis. GitHub Actions allow you to combine both, Node.js and Redis defining the testing matrix and booting a Redis server before running the tests:
name: Run tests
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [8.x, 10.x, 12.x, 13.x]
redis-version: [4, 5]
steps:
- name: Git checkout
uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Start Redis v${{ matrix.redis-version }}
uses: superchargejs/redis-github-action@1.1.0
with:
redis-version: ${{ matrix.redis-version }}
- run: npm install
- run: npm test
This workflow creates a test setup for each version combination of Node.js and Redis. You’ll run the tests for Node.js 8.x with Redis 4, Node.js 8.x with Redis 5, and so on.
That’s it. Enjoy GitHub Actions with Redis!
Mentioned Resources
- Redis Action in the GitHub marketplace
- Creating a testing matrix in GitHub Actions tutorial on Future Studio