GitHub Actions — Using MongoDB Replica Sets

GitHub Actions let you automate tasks on your repositories. For example, you can move your entire CI/CD pipeline to GitHub using Actions.

You probably need a database when running your tests. MongoDB recommends using replica sets in production environments. You can push your testing environment as close to the production as possible by also using replica sets. This tutorial shows you how!

GitHub Actions Series Overview

Use MongoDB Replica Sets in GitHub Actions

Testing against a MongoDB database in GitHub’s Actions is pretty straightforward.

We’ve published a MongoDB GitHub Action making a MongoDB database server available in your action’s environment. You can find it in the GitHub marketplace at supercharge/mongodb-github-action.

The supercharge/mongodb-github-action supports a single-node MongoDB instance and also replica sets. The provided MongoDB replica set is also a single-node instance.

Using replica sets requires version 1.2.0 or later of the supercharge/mongodb-github-action.

Using a replica set requires you to provide the mongodb-replica-set input. The input value must be the replica set name you want to use. For example, the workflow configuration below uses the replica set name "rs-test". You must configure the same replica set name “rs-test” in your application to connect your tests against the available replica set.

Here’s a sample configuration on how to add MongoDB replica sets to your testing action:

name: Using a MongoDB replica set

on: [push]

jobs:  
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        mongodb-version: [3.6, 4.0, 4.2]

    steps:
    - name: Start MongoDB v${{ matrix.mongodb-version }}
      uses: supercharge/mongodb-github-action@1.2.0
      with:
        mongodb-version: ${{ matrix.mongodb-version }}
        mongodb-replica-set: rs-test                    <— the important part

    - run: echo „done“

The MongoDB GitHub Action starts a single-node MongoDB instance (not as a replica set) if you don’t provide the mongodb-replica-set input.

Combining MongoDB Replica Sets With Node.js

A testing workflow is typically more complex than starting a MongoDB database. You also need to clone your code, install dependencies like Node.js and databases and ultimately run the tests.

Here’s a sample workflow testing a Node.js application using a MongoDB replica set:

name: Run tests

on: [push]

jobs:  
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [8.x, 10.x, 12.x, 13.x]
        mongodb-version: [4.0, 4.2]

    steps:
    - name: Git checkout
      uses: actions/checkout@v2

    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}

    - name: Start MongoDB
      uses: supercharge/mongodb-github-action@1.2.0
      with:
        mongodb-version: ${{ matrix.mongodb-version }}
        mongodb-replica-set: insert-your-replica-set-name

    - run: npm install

    - run: npm test
      env:
        CI: true

The mongodb-replica-set input is the same for all matrix combinations. You probably want to use the same replica set name in every run 😃

MongoDB GitHub Actions Is Open Source

The code for the MongoDB GitHub Action is publicly available in the supercharge/mongodb-github-action repository on GitHub.

Enjoy!


Mentioned Resources

Explore the Library

Find interesting tutorials and solutions for your problems.