Skip to content

Repository files navigation

setup-mongodb-action

This action sets up a MongoDB server for a GitHub Actions workflow and tears it down when the job ends.

Prerequisites

This action does not provision WSL or Docker itself. On Windows runners it requires setup-wsl-action to run first in the same job. That action provisions WSL2 and Docker, keeps the instance alive, and exports the WSL_DISTRIBUTION, WSL_IP, and WSL_TOOLS_MODULE_PATH environment variables this action relies on. On Linux runners setup-wsl-action is a no-op but should still be included so the workflow is uniform.

If setup-wsl-action has not run, the action fails fast with a clear error.

Usage

See action.yml

steps:
- name: Setup WSL
  uses: Particular/setup-wsl-action@v1
- name: Setup MongoDB
  uses: Particular/setup-mongodb-action@v1.0.0
  with:
    connection-string-name: <my connection string name>
    mongodb-replica-set: <replica set name>
    mongodb-version: <mongodb version tag>
    mongodb-port: <port number>

connection-string-name defaults to MongoDBConnectionString. mongodb-version defaults to 7.0.39, mongodb-port to 27017, and mongodb-replica-set is empty when omitted.

The action writes the connection string to the environment variable named by connection-string-name. When a replica set is configured, it appends ?replicaSet=<name> so drivers pick up the topology from the connection string alone.

On Linux runners the MongoDB container runs directly through Docker. On Windows runners the same MongoDB container runs inside WSL2 provisioned by setup-wsl-action, so both platforms behave identically.

The action also adds mongosh to the PATH of subsequent steps on both platforms. It forwards into the container, so scripts that call mongosh keep working unchanged on Windows and Linux.

Resource limits

MongoDB runs in a container with a 1 GB WiredTiger cache and a raised file-descriptor limit (nofile=1048576) on both platforms.

  • The cache is capped because mongod's default (50% of RAM) plus connection churn OOM-kills it inside the 4 GB WSL2 VM that setup-wsl-action provisions by default on Windows runners. 1 GB is plenty for test-sized datasets.
  • The fd limit is raised because dockerd inside WSL inherits Ubuntu's default ulimit -n of 1024, and acceptance suites (hundreds of pooled connections plus one WiredTiger data/index file per collection) exhaust that — mongod fails with 24: Too many open files and dies, which surfaces in MongoDB drivers as EndOfStreamException while receiving a message.

If a workflow runs an unusually heavy suite on Windows, the WSL2 VM's 4 GB default can still be tight. Give it more memory via setup-wsl-action's memory input, e.g. Particular/setup-wsl-action with memory: 8GB.

License

The scripts and documentation in this project are released under the MIT License.

Development

Open the folder in Visual Studio Code. If you don't already have them, you will be prompted to install remote development extensions. After installing them, and re-opening the folder in a container, do the following:

Run the npm installation

npm install

When changing index.mjs, either run npm run dev beforehand, which will watch the file for changes and automatically compile it, or run npm run prepare afterwards.

Testing

With Node.js

To test the setup action, create an .env.setup file in the root directory with the following content

# Input overrides
INPUT_CONNECTION-STRING-NAME=MongoDBConnectionString
INPUT_MONGODB-VERSION=7.0.39
INPUT_MONGODB-PORT=27018
INPUT_MONGODB-REPLICA-SET=tr0

# Runner overrides
# Use LINUX to run on Linux, WINDOWS to run on Windows via WSL2
RUNNER_OS=LINUX

then execute the script

node -r dotenv/config dist/index.mjs dotenv_config_path=.env.setup

To test the cleanup action add an .env.cleanup file in the root directory with the following content

# State overrides
STATE_IsPost=true
STATE_ContainerName=nameOfPreviouslyCreatedContainer
node -r dotenv/config dist/index.mjs dotenv_config_path=.env.cleanup

With PowerShell

To test the setup action set the required environment variables and execute setup.ps1 with the desired parameters.

$Env:RUNNER_OS=Linux
.\setup.ps1 -ContainerName mongodb-test-1 -ConnectionStringName MongoDBConnectionString

To test the cleanup action set the required environment variables and execute cleanup.ps1 with the desired parameters.

$Env:RUNNER_OS=Linux
.\cleanup.ps1 -ContainerName mongodb-test-1

Running setup.ps1/cleanup.ps1 directly requires WSL_TOOLS_MODULE_PATH to point at setup-wsl-action's WslTools module (set it by running setup-wsl-action first).

About

Runs MongoDB for a GitHub Actions workflow

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages