Git Product home page Git Product logo

mongodb-github-action's Introduction



MongoDB in GitHub Actions

Start a MongoDB server in your GitHub Actions.


Usage



Follow @marcuspoehls and @superchargejs for updates!


Introduction

This GitHub Action starts a MongoDB server or MongoDB replica set. By default, the MongoDB server is available on the default port 27017. You can configure a custom port using the mongodb-port input. The examples show how to use a custom port. Mongo version must be specified in mongodb-version and it must be existing Docker mongo tag. Default value is latest, other popular choices are 4.2, 4.4 and 5.0.

This is useful when running tests against a MongoDB database.

Usage

A code example says more than a 1000 words. Here’s an exemplary GitHub Action using a MongoDB server in versions 4.2 and 4.4 to test a Node.js app:

name: Run tests

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [14.x, 16.x]
        mongodb-version: ['4.2', '4.4']

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

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

    - name: Start MongoDB
      uses: supercharge/[email protected]
      with:
        mongodb-version: ${{ matrix.mongodb-version }}

    - run: npm install

    - run: npm test
      env:
        CI: true

Using a Custom MongoDB Port

You can start the MongoDB instance on a custom port. Use the mongodb-port: 12345 input to configure port 12345 for MongoDB. Replace 12345 with the port you want to use in your test runs.

The following example starts a MongoDB server on port 42069:

name: Run tests

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [14.x, 16.x]
        mongodb-version: ['4.2', '4.4']

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

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

    - name: Start MongoDB
      uses: supercharge/[email protected]
      with:
        mongodb-version: ${{ matrix.mongodb-version }}
        mongodb-replica-set: test-rs
        mongodb-port: 42069

    - name: Install dependencies
      run: npm install

    - name: Run tests
      run: npm test
      env:
        CI: true

With a Replica Set (MongoDB --replSet Flag)

You can run your tests against a MongoDB replica set by adding the mongodb-replica-set: your-replicate-set-name input in your action’s workflow. The value for mongodb-replica-set defines the name of your replica set. Replace your-replicate-set-name with the replica set name you want to use in your tests.

The following example uses the replica set name test-rs:

name: Run tests

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [14.x, 16.x]
        mongodb-version: ['4.2', '4.4']

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

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

    - name: Start MongoDB
      uses: supercharge/[email protected]
      with:
        mongodb-version: ${{ matrix.mongodb-version }}
        mongodb-replica-set: test-rs
        mongodb-port: 42069

    - name: Install dependencies
      run: npm install

    - name: Run tests
      run: npm test
      env:
        CI: true

With Authentication (MongoDB --auth Flag)

Setting the mongodb-username and mongodb-password inputs. As per the Dockerhub documentation, this automatically creates an admin user and enables --auth mode.

The following example uses the username supercharge, password secret and also sets an initial supercharge database:

name: Run tests with authentication

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [14.x, 16.x]
        mongodb-version: ['4.2', '4.4']

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

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

    - name: Start MongoDB
      uses: supercharge/[email protected]
      with:
        mongodb-version: ${{ matrix.mongodb-version }}
        mongodb-username: supercharge
        mongodb-password: secret
        mongodb-db: supercharge

    - name: Install dependencies
      run: npm install

    - name: Run tests
      run: npm test
      env:
        CI: true

Caveat: due to this issue, you cannot enable user creation AND replica sets initially. Therefore, if you use this action to setup a replica set, please create your users through a separate script.

License

MIT © Supercharge


superchargejs.com  ·  GitHub @supercharge  ·  Twitter @superchargejs

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.