This action sets up a MongoDB server for a GitHub Actions workflow and tears it down when the job ends.
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.
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.
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 -nof 1024, and acceptance suites (hundreds of pooled connections plus one WiredTiger data/index file per collection) exhaust that — mongod fails with24: Too many open filesand dies, which surfaces in MongoDB drivers asEndOfStreamExceptionwhile 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.
The scripts and documentation in this project are released under the MIT License.
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 installWhen 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.
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=LINUXthen execute the script
node -r dotenv/config dist/index.mjs dotenv_config_path=.env.setupTo test the cleanup action add an .env.cleanup file in the root directory with the following content
# State overrides
STATE_IsPost=true
STATE_ContainerName=nameOfPreviouslyCreatedContainernode -r dotenv/config dist/index.mjs dotenv_config_path=.env.cleanupTo 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 MongoDBConnectionStringTo 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-1Running
setup.ps1/cleanup.ps1directly requiresWSL_TOOLS_MODULE_PATHto point at setup-wsl-action'sWslToolsmodule (set it by running setup-wsl-action first).