GitHub Actions — Using MongoDB

GitHub Actions provide powerful automation, like running tests for your codebase. Keeping your continuous integration pipeline close to your code is a sweet setup.

Typically, an application requires a database dependency. If you’re in the need for MongoDB when using GitHub Actions, this tutorial is for you!

GitHub Actions Series Overview

Use MongoDB in GitHub Actions

Running your tests against an actual MongoDB instance increases the confidence in your code. Adding MongoDB to a GitHub Action is pretty straightforward.

We’ve published a GitHub Action providing a MongoDB server. You can find it in the GitHub marketplace at supercharge/mongodb-github-action and install it from there.

You may configure the MongoDB server in your workflow YAML file. Depending on your needs, you may also use a testing matrix against multiple MongoDB versions.

Here’s a sample configuration using the supercharge/mongodb-github-action package to add MongoDB to your GitHub Actions:

name: MongoDB

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 }}

    - run: echo "done"

This workflow setup runs your action against each version of MongoDB defined in the mongdb-version list. You can remove the MongoDB versions that are not of interest.

Combining MongoDB With Node.js

Testing your Node.js application against a MongoDB 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 MongoDB, you may test a list of Node.js version against a list of MongoDB versions. GitHub Actions allow you to combine both, Node.js and MongoDB using a testing matrix.

Go ahead and boot a MongoDB 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]
        mongodb-version: [4.0, 4.2]

    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 MongoDB v${{ matrix.mongodb-version }}
      uses: supercharge/mongodb-github-action@1.2.0
      with:
        mongodb-version: ${{ matrix.mongodb-version }}

    - run: npm install

    - run: npm test

This workflow creates a test setup for each version combination of Node.js and MongoDB. You’ll run the tests for Node.js 8.x with MongoDB 4.0, Node.js 8.x with MongoDB 4.2, and so on.

That’s it. Enjoy GitHub Actions with MongoDB!


Mentioned Resources

Explore the Library

Find interesting tutorials and solutions for your problems.