├── .eslintrc.json ├── Dockerfile ├── docs └── release.md ├── test ├── custom-port │ └── custom-port.js ├── single-instance │ └── single-instance.js └── replica-set │ └── replica-set.js ├── .github └── workflows │ ├── validate-action-typings.yml │ ├── main.yml │ ├── test-auth.yml │ ├── test-single-instance.yml │ └── test-replica-set.yml ├── .gitignore ├── action-types.yml ├── stop-mongodb.sh ├── package.json ├── LICENSE ├── action.yml ├── CHANGELOG.md ├── start-mongodb.sh └── README.md /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "node": true 4 | }, 5 | "extends": [ 6 | "@supercharge" 7 | ], 8 | "parserOptions": { 9 | "ecmaVersion": 2018 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM docker:latest 2 | COPY start-mongodb.sh /start-mongodb.sh 3 | COPY stop-mongodb.sh /stop-mongodb.sh 4 | RUN chmod +x /start-mongodb.sh /stop-mongodb.sh 5 | ENTRYPOINT ["/start-mongodb.sh"] 6 | 7 | -------------------------------------------------------------------------------- /docs/release.md: -------------------------------------------------------------------------------- 1 | ## Create a new Release 2 | 3 | - run `git tag -a 1.9.0 -m "Release 1.9.0"` 4 | - update the version! 5 | - run `git push --tags` 6 | - go to https://github.com/supercharge/mongodb-github-action/tags and create a new release from the created tag 7 | -------------------------------------------------------------------------------- /test/custom-port/custom-port.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const { test } = require('uvu') 4 | const Mongoose = require('mongoose') 5 | 6 | test('connects to MongoDB on custom port 12345', async () => { 7 | const connection = await Mongoose.createConnection('mongodb://localhost:12345') 8 | await connection.close() 9 | }) 10 | 11 | test.run() 12 | -------------------------------------------------------------------------------- /.github/workflows/validate-action-typings.yml: -------------------------------------------------------------------------------- 1 | name: Validate action typings 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | workflow_dispatch: 8 | 9 | jobs: 10 | validate-typings: 11 | runs-on: "ubuntu-latest" 12 | steps: 13 | - uses: actions/checkout@v4 14 | - uses: krzema12/github-actions-typing@v1 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | haters 2 | 3 | lib-cov 4 | *.seed 5 | *.log 6 | *.csv 7 | *.dat 8 | *.out 9 | *.pid 10 | *.gz 11 | .github-todos 12 | 13 | pids 14 | results 15 | 16 | node_modules 17 | npm-debug.log 18 | package-lock.json 19 | 20 | # code coverage folder 21 | coverage 22 | .nyc_output 23 | 24 | # Secrets 25 | .env 26 | .env.** 27 | 28 | # IDEs and editors 29 | .idea 30 | .vscode 31 | 32 | .vagrant 33 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: Start MongoDB Server 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | mongodb-action: 7 | name: Start MongoDB Server v${{ matrix.mongodb-version }} 8 | 9 | runs-on: ubuntu-latest 10 | strategy: 11 | matrix: 12 | mongodb-version: ['4.0', '4.2', '4.4', '5.0', '6.0'] 13 | 14 | steps: 15 | - name: Checkout 16 | uses: actions/checkout@v4 17 | 18 | - name: Start MongoDB Server 19 | uses: ./ 20 | with: 21 | mongodb-version: ${{ matrix.mongodb-version }} 22 | -------------------------------------------------------------------------------- /action-types.yml: -------------------------------------------------------------------------------- 1 | # See https://github.com/krzema12/github-actions-typing 2 | inputs: 3 | mongodb-image: 4 | type: string 5 | mongodb-version: 6 | type: string 7 | mongodb-replica-set: 8 | type: string 9 | mongodb-port: 10 | type: integer 11 | mongodb-db: 12 | type: string 13 | mongodb-username: 14 | type: string 15 | mongodb-password: 16 | type: string 17 | mongodb-container-name: 18 | type: string 19 | mongodb-key: 20 | type: string 21 | mongodb-authsource: 22 | type: string 23 | mongodb-replica-set-host: 24 | type: string 25 | docker-network: 26 | type: string 27 | docker-network-alias: 28 | type: string 29 | -------------------------------------------------------------------------------- /test/single-instance/single-instance.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const { test } = require('uvu') 4 | const { expect } = require('expect') 5 | const Mongoose = require('mongoose') 6 | 7 | const { MONGODB_USERNAME, MONGODB_PASSWORD, MONGODB_DB, MONGODB_AUTHSOURCE } = process.env 8 | 9 | test('connects to MongoDB', async () => { 10 | const connection = await Mongoose.createConnection('mongodb://localhost', { 11 | user: MONGODB_USERNAME, 12 | pass: MONGODB_PASSWORD, 13 | dbName: MONGODB_DB, 14 | authSource: MONGODB_USERNAME && MONGODB_PASSWORD ? MONGODB_AUTHSOURCE : undefined 15 | }) 16 | 17 | await connection.close() 18 | }) 19 | 20 | test('fails to connect to non-existent MongoDB instance', async () => { 21 | await expect( 22 | Mongoose.connect('mongodb://localhost:27018', { 23 | connectTimeoutMS: 1000, 24 | serverSelectionTimeoutMS: 1000 25 | }) 26 | ).rejects.toThrow() 27 | }) 28 | 29 | test.run() 30 | -------------------------------------------------------------------------------- /stop-mongodb.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Keep argument positions aligned with action.yml "args" so we can reuse them in post-args 4 | MONGODB_IMAGE=$1 5 | MONGODB_VERSION=$2 6 | MONGODB_REPLICA_SET=$3 7 | MONGODB_PORT=$4 8 | MONGODB_DB=$5 9 | MONGODB_USERNAME=$6 10 | MONGODB_PASSWORD=$7 11 | MONGODB_CONTAINER_NAME=$8 12 | MONGODB_KEY=$9 13 | MONGODB_AUTHSOURCE=${10} 14 | MONGODB_REPLICA_SET_HOST=${11:-"localhost"} 15 | DOCKER_NETWORK=${12} 16 | DOCKER_NETWORK_ALIAS=${13:-$MONGODB_CONTAINER_NAME} 17 | 18 | # Best-effort cleanup, do not fail the job if cleanup fails 19 | set +e 20 | 21 | echo "::group::Cleaning up MongoDB container [$MONGODB_CONTAINER_NAME]" 22 | 23 | if docker ps -a --format '{{.Names}}' | grep -Eq "^${MONGODB_CONTAINER_NAME}$"; then 24 | docker rm -f "$MONGODB_CONTAINER_NAME" >/dev/null 2>&1 || true 25 | echo "Removed container $MONGODB_CONTAINER_NAME" 26 | else 27 | echo "Container $MONGODB_CONTAINER_NAME not found; nothing to clean." 28 | fi 29 | 30 | echo "::endgroup::" 31 | 32 | exit 0 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@supercharge/mongodb-github-action", 3 | "description": "MongoDB GitHub Action", 4 | "version": "1.10.0", 5 | "author": "Marcus Pöhls ", 6 | "bugs": { 7 | "url": "https://github.com/supercharge/mongodb-github-action/issues" 8 | }, 9 | "devDependencies": { 10 | "@supercharge/eslint-config": "~3.0.1", 11 | "c8": "~10.1.3", 12 | "eslint": "~8.57.0", 13 | "expect": "~29.7.0", 14 | "mongoose": "~8.9.3", 15 | "uvu": "~0.5.6" 16 | }, 17 | "engines": { 18 | "node": ">=8" 19 | }, 20 | "homepage": "https://github.com/supercharge/mongodb-github-action", 21 | "keywords": [ 22 | "github", 23 | "github-action", 24 | "supercharge", 25 | "superchargejs" 26 | ], 27 | "license": "MIT", 28 | "repository": { 29 | "type": "git", 30 | "url": "git+https://github.com/supercharge/mongodb-github-action.git" 31 | }, 32 | "scripts": { 33 | "lint": "eslint test --ext .js", 34 | "test": "c8 --include=dist uvu --ignore fixtures", 35 | "posttest": "c8 report --reporter=html" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 The Supercharge Node.js Framework 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.github/workflows/test-auth.yml: -------------------------------------------------------------------------------- 1 | name: Single Instance With Auth 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | single-instance-with-auth: 7 | name: Mongo v${{ matrix.mongodb-version }} - Node v${{ matrix.node-version }} 8 | 9 | runs-on: ubuntu-latest 10 | strategy: 11 | matrix: 12 | node-version: [20, 22] 13 | mongodb-version: ['5.0', '6.0', '7.0'] 14 | mongodb-db: ['ci'] 15 | mongodb-username: ['ci'] 16 | mongodb-password: ['ci'] 17 | 18 | steps: 19 | - name: Checkout 20 | uses: actions/checkout@v4 21 | 22 | - name: Start MongoDB Server v${{ matrix.mongodb-version }} 23 | uses: ./ 24 | with: 25 | mongodb-version: ${{ matrix.mongodb-version }} 26 | mongodb-db: ${{ matrix.mongodb-db }} 27 | mongodb-username: ${{ matrix.mongodb-username }} 28 | mongodb-password: ${{ matrix.mongodb-password }} 29 | 30 | - name: Use Node.js ${{ matrix.node-version }} 31 | uses: actions/setup-node@v4 32 | with: 33 | node-version: ${{ matrix.node-version }} 34 | 35 | - name: Install dependencies 36 | run: npm install 37 | 38 | - name: Run tests 39 | run: npm test ./test/single-instance 40 | env: 41 | CI: true 42 | MONGODB_DB: ${{ matrix.mongodb-db }} 43 | MONGODB_USERNAME: ${{ matrix.mongodb-username }} 44 | MONGODB_PASSWORD: ${{ matrix.mongodb-password }} 45 | -------------------------------------------------------------------------------- /test/replica-set/replica-set.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const { test } = require('uvu') 4 | const { expect } = require('expect') 5 | const Mongoose = require('mongoose') 6 | 7 | const { MONGODB_PORT = 27017, MONGODB_REPLICA_SET = 'mongodb-test-rs' } = process.env 8 | 9 | test.before(async () => { 10 | const connectionString = `mongodb://localhost:${MONGODB_PORT}/test?replicaSet=${MONGODB_REPLICA_SET}` 11 | 12 | console.log('---------------------------------------------------------------------') 13 | console.log('connecting to MongoDB using connection string -> ' + connectionString) 14 | console.log('---------------------------------------------------------------------') 15 | 16 | await Mongoose.connect(connectionString, { 17 | serverSelectionTimeoutMS: 1500 18 | }) 19 | }) 20 | 21 | test.after(async () => { 22 | await Mongoose.connection.db.dropDatabase() 23 | await Mongoose.disconnect() 24 | }) 25 | 26 | test('queries the replica set status', async () => { 27 | const db = Mongoose.connection.db.admin() 28 | const { ok, set } = await db.command({ replSetGetStatus: 1 }) 29 | 30 | expect(ok).toBe(1) 31 | expect(set).toEqual(MONGODB_REPLICA_SET) 32 | }) 33 | 34 | test('saves a document', async () => { 35 | const Dog = Mongoose.model('Dog', { name: String }) 36 | const albert = await new Dog({ name: 'Albert' }).save() 37 | expect(albert.name).toEqual('Albert') 38 | }) 39 | 40 | test('uses transactions', async () => { 41 | const Customer = Mongoose.model('Customer', new Mongoose.Schema({ name: String })) 42 | await Customer.createCollection() 43 | 44 | const session = await Mongoose.startSession() 45 | session.startTransaction() 46 | 47 | await Customer.create([{ name: 'test-customer' }], { session }) 48 | 49 | expect( 50 | await Customer.findOne({ name: 'test-customer' }) 51 | ).toBeNull() 52 | 53 | expect( 54 | await Customer.findOne({ name: 'test-customer' }).session(session) 55 | ).not.toBeNull() 56 | 57 | await session.commitTransaction() 58 | 59 | expect( 60 | await Customer.findOne({ name: 'test-customer' }) 61 | ).not.toBeNull() 62 | }) 63 | 64 | test.run() 65 | -------------------------------------------------------------------------------- /.github/workflows/test-single-instance.yml: -------------------------------------------------------------------------------- 1 | name: Single Instance Tests 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | single-instance-on-default-port: 7 | name: MongoDB v${{ matrix.mongodb-version }} — Node.js v${{ matrix.node-version }} 8 | 9 | runs-on: ubuntu-latest 10 | strategy: 11 | matrix: 12 | node-version: [20, 22] 13 | mongodb-version: ['5.0', '6.0', '7.0'] 14 | 15 | steps: 16 | - name: Checkout 17 | uses: actions/checkout@v4 18 | 19 | - name: Start MongoDB Server v${{ matrix.mongodb-version }} 20 | uses: ./ 21 | with: 22 | mongodb-version: ${{ matrix.mongodb-version }} 23 | 24 | - name: Use Node.js ${{ matrix.node-version }} 25 | uses: actions/setup-node@v4 26 | with: 27 | node-version: ${{ matrix.node-version }} 28 | 29 | - name: Install dependencies 30 | run: npm install 31 | 32 | - name: Run tests 33 | run: npm test ./test/single-instance 34 | env: 35 | CI: true 36 | MONGODB_DB: ${{ matrix.mongodb-db }} 37 | 38 | 39 | single-instance-on-custom-port: 40 | runs-on: ubuntu-latest 41 | strategy: 42 | matrix: 43 | mongodb-port: [12345] 44 | mongodb-version: ['5.0', '6.0', '7.0'] 45 | node-version: [20, 22] 46 | 47 | name: MongoDB v${{ matrix.mongodb-version }}, Port ${{ matrix.mongodb-port }} — Node.js v${{ matrix.node-version }} 48 | steps: 49 | - name: Checkout 50 | uses: actions/checkout@v4 51 | 52 | - name: Start MongoDB Server v${{ matrix.mongodb-version }} 53 | uses: ./ 54 | with: 55 | mongodb-version: ${{ matrix.mongodb-version }} 56 | mongodb-port: ${{ matrix.mongodb-port }} 57 | 58 | - name: Use Node.js ${{ matrix.node-version }} 59 | uses: actions/setup-node@v4 60 | with: 61 | node-version: ${{ matrix.node-version }} 62 | 63 | - name: Install dependencies 64 | run: npm install 65 | 66 | - name: Run tests 67 | run: npm test ./test/custom-port 68 | env: 69 | CI: true 70 | -------------------------------------------------------------------------------- /.github/workflows/test-replica-set.yml: -------------------------------------------------------------------------------- 1 | name: Replica Set Tests 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | single-node-replica-set-on-default-port: 7 | name: MongoDB v${{ matrix.mongodb-version }} RS — Node.js v${{ matrix.node-version }} 8 | 9 | runs-on: ubuntu-latest 10 | strategy: 11 | matrix: 12 | node-version: [20, 22] 13 | mongodb-version: ['5.0', '6.0', '7.0'] 14 | 15 | steps: 16 | - name: Checkout 17 | uses: actions/checkout@v4 18 | 19 | - name: Start MongoDB Server v${{ matrix.mongodb-version }} 20 | uses: ./ 21 | with: 22 | mongodb-version: ${{ matrix.mongodb-version }} 23 | mongodb-replica-set: mongodb-test-rs 24 | 25 | - name: Use Node.js ${{ matrix.node-version }} 26 | uses: actions/setup-node@v4 27 | with: 28 | node-version: ${{ matrix.node-version }} 29 | 30 | - name: Install dependencies 31 | run: npm install 32 | 33 | - name: Run tests 34 | run: npm test ./test/replica-set 35 | env: 36 | CI: true 37 | MONGODB_REPLICA_SET: mongodb-test-rs 38 | 39 | single-node-replica-set-on-custom-port: 40 | runs-on: ubuntu-latest 41 | strategy: 42 | matrix: 43 | node-version: [20, 22] 44 | mongodb-port: [23456] 45 | mongodb-version: ['5.0', '6.0', '7.0'] 46 | 47 | name: MongoDB v${{ matrix.mongodb-version }} RS, Port ${{ matrix.mongodb-port }} — Node.js v${{ matrix.node-version }} 48 | steps: 49 | - name: Checkout 50 | uses: actions/checkout@v4 51 | 52 | - name: Start MongoDB Server v${{ matrix.mongodb-version }} 53 | uses: ./ 54 | with: 55 | mongodb-port: ${{ matrix.mongodb-port }} 56 | mongodb-version: ${{ matrix.mongodb-version }} 57 | mongodb-replica-set: mongodb-test-rs 58 | 59 | - name: Use Node.js ${{ matrix.node-version }} 60 | uses: actions/setup-node@v4 61 | with: 62 | node-version: ${{ matrix.node-version }} 63 | 64 | - name: Install dependencies 65 | run: npm install 66 | 67 | - name: Run tests 68 | run: npm test ./test/replica-set 69 | env: 70 | CI: true 71 | MONGODB_PORT: ${{ matrix.mongodb-port }} 72 | MONGODB_REPLICA_SET: mongodb-test-rs 73 | -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | name: 'MongoDB in GitHub Actions' 2 | description: 'Start a MongoDB server (on default port 27017 or a custom port)' 3 | 4 | branding: 5 | icon: 'database' 6 | color: 'green' 7 | 8 | inputs: 9 | mongodb-image: 10 | description: 'MongoDB image to use (defaults to using "mongo" from Docker Hub but you could also use an image from another repository such as Amazons "public.ecr.aws/docker/library/mongo")' 11 | required: false 12 | default: 'mongo' 13 | 14 | mongodb-version: 15 | description: 'MongoDB version to use (default: "latest")' 16 | required: false 17 | default: 'latest' 18 | 19 | mongodb-replica-set: 20 | description: 'MongoDB replica set name (no replica set by default)' 21 | required: false 22 | default: '' 23 | 24 | mongodb-port: 25 | description: 'MongoDB port to use (default: 27017)' 26 | required: false 27 | default: 27017 28 | 29 | mongodb-db: 30 | description: 'MongoDB db to create (default: none)' 31 | required: false 32 | default: '' 33 | 34 | mongodb-username: 35 | description: 'MongoDB root username (default: none)' 36 | required: false 37 | default: '' 38 | 39 | mongodb-password: 40 | description: 'MongoDB root password (default: none)' 41 | required: false 42 | default: '' 43 | 44 | mongodb-container-name: 45 | description: 'MongoDB container name (default: "mongodb")' 46 | required: false 47 | default: 'mongodb' 48 | 49 | mongodb-key: 50 | description: 'MongoDB key, required if replica set and auth are setup through username and password (no key set by default)' 51 | required: false 52 | default: '' 53 | 54 | mongodb-authsource: 55 | description: 'MongoDB authenticationDatabase a.k.a authSource to use (default: "admin" based on https://github.com/docker-library/mongo/blob/master/8.0/docker-entrypoint.sh#L372C4-L372C20)' 56 | required: false 57 | default: 'admin' 58 | 59 | mongodb-replica-set-host: 60 | description: 'MongoDB replica set host, must be accessible from both internal container and external usage (default: "localhost")' 61 | required: false 62 | default: 'localhost' 63 | 64 | docker-network: 65 | description: 'Docker network to attach the MongoDB container to. If not provided, will try to use the default GitHub Actions network if available (github_network_).' 66 | required: false 67 | default: '' 68 | 69 | docker-network-alias: 70 | description: 'Network alias for the MongoDB container when attaching to a Docker network. If not provided, will use mongodb-container-name input' 71 | required: false 72 | default: '' 73 | 74 | runs: 75 | using: 'docker' 76 | image: 'Dockerfile' 77 | args: 78 | - ${{ inputs.mongodb-image }} 79 | - ${{ inputs.mongodb-version }} 80 | - ${{ inputs.mongodb-replica-set }} 81 | - ${{ inputs.mongodb-port }} 82 | - ${{ inputs.mongodb-db }} 83 | - ${{ inputs.mongodb-username }} 84 | - ${{ inputs.mongodb-password }} 85 | - ${{ inputs.mongodb-container-name }} 86 | - ${{ inputs.mongodb-key }} 87 | - ${{ inputs.mongodb-authsource }} 88 | - ${{ inputs.mongodb-replica-set-host }} 89 | - ${{ inputs.docker-network }} 90 | - ${{ inputs.docker-network-alias }} 91 | post-entrypoint: /stop-mongodb.sh 92 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | 4 | ## [1.12.1](https://github.com/superchargejs/mongodb-github-action/compare/v1.12.0...v1.12.1) - 2025-11-11 5 | 6 | ### Fixed 7 | - change Docker tag from `docker:stable` to `docker:latest` 8 | 9 | 10 | ## [1.12.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.11.0...v1.12.0) - 2025-01-05 11 | 12 | ### Added 13 | - added `mongodb-image` input: this option allows you to define a custom Docker container image. It uses `mongo` by default, but you may specify an image from a different registry than Docker hub. Please check the Readme for details. 14 | 15 | ### Updated 16 | - bump dependencies 17 | 18 | 19 | ## [1.11.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.10.0...v1.11.0) - 2024-05-22 20 | 21 | ### Added 22 | - added `mongodb-container-name` input: this option allows you to define the Docker container name 23 | 24 | ### Fixed 25 | - use the `mongo` command to interact with MongoDB versions 4.x or lower. Previously, we only checked for MongoDB 4 and would use `mongosh` for MongoDB 3 (and lower). [Thanks to Aravind!](https://github.com/supercharge/mongodb-github-action/pull/61) 26 | 27 | ### Updated 28 | - bump dependencies 29 | 30 | 31 | ## [1.10.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.9.0...v1.10.0) - 2023-08-07 32 | 33 | ### Added 34 | - MongoDB single instance: wait for MongoDB to be ready 35 | 36 | ### Updated 37 | - bump dependencies 38 | 39 | 40 | ## [1.9.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.8.0...v1.9.0) - 2023-02-10 41 | 42 | ### Added 43 | - exit early in case of Docker issues (e.g., unavailable MongoDB version) 44 | - validate GitHub Action typings using [krzema12/github-actions-typing](https://github.com/krzema12/github-actions-typing) 45 | 46 | ### Updated 47 | - bump dependencies 48 | 49 | 50 | ## [1.8.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.7.0...v1.8.0) - 2022-08-26 51 | 52 | ### Added 53 | - support MongoDB 6.x. Thanks to [Evandro aka ecarruda](https://github.com/ecarruda)! 54 | - use `mongo` for MongoDB 4.x release line 55 | - use `mongosh` for MongoDB 5.x and 6.x release lines 56 | 57 | ### Updated 58 | - bump dependencies 59 | - use `@supercharge/eslint-config` instead of `eslint-config-standard` 60 | 61 | 62 | ## [1.7.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.6.0...v1.7.0) - 2021-11-16 63 | 64 | ### Added 65 | - `mongodb-username` and `mongodb-password` inputs allowing you to configure the authentication credentials for a user created in the `admin` database (with the role of `root`) 66 | - `mongodb-db` input allowing you to create a database that is used for creation scripts 67 | 68 | ### Updated 69 | - bump dependencies 70 | - use UVU and c8 for testing instead of @hapi/lab 71 | 72 | 73 | ## [1.6.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.5.0...v1.6.0) - 2021-06-01 74 | 75 | ### Added 76 | - show replica set status after `rs.initiate()` 77 | 78 | 79 | ## [1.5.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.4.1...v1.5.0) - 2021-05-31 80 | 81 | ### Added 82 | - refined tests for custom ports 83 | - refined custom port handling when using replica sets 84 | - extended output for better debugging in GitHub Actions 85 | 86 | 87 | ## [1.4.1](https://github.com/superchargejs/mongodb-github-action/compare/v1.4.0...v1.4.1) - 2021-05-24 88 | 89 | ### Updated 90 | - use strings for `mongodb-version` in Readme and GitHub Actions Workflows 91 | 92 | 93 | ## [1.4.1](https://github.com/superchargejs/mongodb-github-action/compare/v1.4.0...v1.4.1) - 2021-05-24 94 | 95 | ### Added 96 | - `mongodb-port` input allowing to start a MongoDB instance (or single-node replica set) on a custom port 97 | 98 | 99 | ## [1.4.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.3.0...v1.4.0) - 2021-05-11 100 | 101 | ### Added 102 | - `mongodb-port` input allowing to start a MongoDB instance (or single-node replica set) on a custom port 103 | 104 | 105 | ## [1.3.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.2.0...v1.3.0) - 2020-04-10 106 | 107 | ### Added 108 | - check if the `mongodb-version` input is present: if not, print a message and fail the job 109 | 110 | 111 | ## [1.2.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.1.0...v1.2.0) - 2020-03-30 112 | 113 | ### Added 114 | - `mongodb-replica-set` input to define a MongoDB replica set name 115 | - tests connecting to MongoDB instances (single instance, replica set) created by this GitHub Action 116 | 117 | 118 | ## [1.1.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.0.0...v1.1.0) - 2019-12-18 119 | 120 | ### Updated 121 | - switched from a Node.js workflow to a Docker-based workflow 122 | - reduces noise in the repo by removing the Node.js dependencies and only relying on a shell script 123 | - green database icon in GitHub Action details 124 | 125 | 126 | ## 1.0.0 - 2019-12-17 127 | 128 | ### Added 129 | - `1.0.0` release 🚀 🎉 130 | -------------------------------------------------------------------------------- /start-mongodb.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Map input values from the GitHub Actions workflow to shell variables 4 | MONGODB_IMAGE=$1 5 | MONGODB_VERSION=$2 6 | MONGODB_REPLICA_SET=$3 7 | MONGODB_PORT=$4 8 | MONGODB_DB=$5 9 | MONGODB_USERNAME=$6 10 | MONGODB_PASSWORD=$7 11 | MONGODB_CONTAINER_NAME=$8 12 | MONGODB_KEY=$9 13 | MONGODB_AUTHSOURCE=${10} 14 | MONGODB_REPLICA_SET_HOST=${11:-"localhost"} 15 | DOCKER_NETWORK=${12} 16 | DOCKER_NETWORK_ALIAS=${13:-$MONGODB_CONTAINER_NAME} 17 | 18 | # If DOCKER_NETWORK not provided, try to detect the default GitHub Actions network 19 | if [ -z "$DOCKER_NETWORK" ]; then 20 | DOCKER_NETWORK=$(docker network ls --no-trunc --format '{{.Name}}' | grep '^github_network') 21 | fi 22 | 23 | # Build network args if a network is set 24 | NETWORK_ARGS="" 25 | if [ -n "$DOCKER_NETWORK" ]; then 26 | NETWORK_ARGS="--network $DOCKER_NETWORK --network-alias $DOCKER_NETWORK_ALIAS" 27 | fi 28 | 29 | # Echo selected network info for visibility 30 | echo "::group::Selecting Docker network" 31 | if [ -n "$DOCKER_NETWORK" ]; then 32 | echo " - Docker network: [$DOCKER_NETWORK]" 33 | echo " - Network alias: [$DOCKER_NETWORK_ALIAS]" 34 | else 35 | echo " - No Docker network provided; container will use default Docker network." 36 | fi 37 | echo "" 38 | echo "::endgroup::" 39 | 40 | # `mongosh` is used starting from MongoDB 5.x 41 | MONGODB_CLIENT="mongosh --quiet" 42 | 43 | if [ -z "$MONGODB_IMAGE" ]; then 44 | echo "" 45 | echo "Missing MongoDB image in the [mongodb-image] input. Received value: $MONGODB_IMAGE" 46 | echo "" 47 | 48 | exit 2 49 | fi 50 | 51 | if [ -z "$MONGODB_VERSION" ]; then 52 | echo "" 53 | echo "Missing MongoDB version in the [mongodb-version] input. Received value: $MONGODB_VERSION" 54 | echo "" 55 | 56 | exit 2 57 | fi 58 | 59 | echo "::group::Using MongoDB Docker image $MONGODB_IMAGE:$MONGODB_VERSION" 60 | 61 | echo "::group::Selecting correct MongoDB client" 62 | if [ "`echo $MONGODB_VERSION | cut -c 1`" -le "4" ]; then 63 | MONGODB_CLIENT="mongo" 64 | fi 65 | echo " - Using MongoDB client: [$MONGODB_CLIENT]" 66 | echo "" 67 | echo "::endgroup::" 68 | 69 | 70 | # Helper function to wait for MongoDB to be started before moving on 71 | wait_for_mongodb () { 72 | echo "::group::Waiting for MongoDB to accept connections" 73 | sleep 1 74 | TIMER=0 75 | 76 | MONGODB_ARGS="" 77 | if [ -n "$MONGODB_USERNAME" ]; then 78 | MONGODB_ARGS="--username $MONGODB_USERNAME --password $MONGODB_PASSWORD --authenticationDatabase $MONGODB_AUTHSOURCE" 79 | fi 80 | 81 | # until ${WAIT_FOR_MONGODB_COMMAND} 82 | until docker exec --tty $MONGODB_CONTAINER_NAME $MONGODB_CLIENT --port $MONGODB_PORT $MONGODB_ARGS --eval "db.serverStatus()" 83 | do 84 | echo "." 85 | sleep 1 86 | TIMER=$((TIMER + 1)) 87 | 88 | if [ "$TIMER" -eq 20 ]; then 89 | echo "MongoDB did not initialize within 20 seconds. Exiting." 90 | exit 2 91 | fi 92 | done 93 | echo "::endgroup::" 94 | } 95 | 96 | 97 | # check if the container already exists and remove it 98 | ## TODO: put this behind an option flag 99 | # if [ "$(docker ps -q -f name=$MONGODB_CONTAINER_NAME)" ]; then 100 | # echo "Removing existing container [$MONGODB_CONTAINER_NAME]" 101 | # docker rm -f $MONGODB_CONTAINER_NAME 102 | # fi 103 | 104 | # If no replica set specified, run single node 105 | if [ -z "$MONGODB_REPLICA_SET" ]; then 106 | echo "::group::Starting single-node instance, no replica set" 107 | echo " - port [$MONGODB_PORT]" 108 | echo " - version [$MONGODB_VERSION]" 109 | echo " - database [$MONGODB_DB]" 110 | echo " - credentials [$MONGODB_USERNAME:$MONGODB_PASSWORD]" 111 | echo " - container-name [$MONGODB_CONTAINER_NAME]" 112 | echo "" 113 | 114 | docker run --name $MONGODB_CONTAINER_NAME \ 115 | $NETWORK_ARGS \ 116 | --publish $MONGODB_PORT:$MONGODB_PORT \ 117 | -e MONGO_INITDB_DATABASE=$MONGODB_DB \ 118 | -e MONGO_INITDB_ROOT_USERNAME=$MONGODB_USERNAME \ 119 | -e MONGO_INITDB_ROOT_PASSWORD=$MONGODB_PASSWORD \ 120 | --detach $MONGODB_IMAGE:$MONGODB_VERSION --port $MONGODB_PORT 121 | 122 | if [ $? -ne 0 ]; then 123 | echo "Error starting MongoDB Docker container" 124 | exit 2 125 | fi 126 | echo "::endgroup::" 127 | 128 | wait_for_mongodb 129 | exit 0 130 | fi 131 | 132 | echo "::group::Starting MongoDB as single-node replica set" 133 | echo " - port [$MONGODB_PORT]" 134 | echo " - version [$MONGODB_VERSION]" 135 | echo " - replica set [$MONGODB_REPLICA_SET]" 136 | if [ -n "$MONGODB_KEY" ]; then 137 | echo " - keyFile provided: yes" 138 | else 139 | echo " - keyFile provided: no (random)" 140 | fi 141 | echo "" 142 | 143 | # For replica set mode: 144 | # If auth (username/password) is requested, ensure mongodb-key is provided, otherwise generate random 145 | if { [ -n "$MONGODB_USERNAME" ] || [ -n "$MONGODB_PASSWORD" ]; } && [ -z "$MONGODB_KEY" ]; then 146 | MONGODB_KEY=$(dd if=/dev/urandom bs=256 count=1 2>/dev/null | base64 | tr -d '\n') 147 | fi 148 | 149 | MONGODB_CMD_ARGS="--port \"$MONGODB_PORT\"" 150 | 151 | if [ -n "$MONGODB_REPLICA_SET" ]; then 152 | MONGODB_CMD_ARGS="$MONGODB_CMD_ARGS --replSet \"$MONGODB_REPLICA_SET\"" 153 | fi 154 | 155 | if [ -n "$MONGODB_KEY" ]; then 156 | # NOTE: MONGO_KEY_FILE is interpolated internally 157 | MONGODB_CMD_ARGS="$MONGODB_CMD_ARGS --keyFile \"\$MONGO_KEY_FILE\"" 158 | fi 159 | 160 | 161 | # Start mongod in replica set mode, with optional auth and keyFile 162 | # MONGO_INITDB_* envs will create the root user on first startup 163 | 164 | docker run --name $MONGODB_CONTAINER_NAME \ 165 | $NETWORK_ARGS \ 166 | --publish $MONGODB_PORT:$MONGODB_PORT \ 167 | -e MONGO_INITDB_DATABASE=$MONGODB_DB \ 168 | -e MONGO_INITDB_ROOT_USERNAME=$MONGODB_USERNAME \ 169 | -e MONGO_INITDB_ROOT_PASSWORD=$MONGODB_PASSWORD \ 170 | -e MONGO_KEY=$MONGODB_KEY \ 171 | -e MONGO_KEY_FILE=/tmp/mongo-keyfile \ 172 | --detach \ 173 | --entrypoint bash \ 174 | $MONGODB_IMAGE:$MONGODB_VERSION \ 175 | -c '\ 176 | echo "$MONGO_KEY" > "$MONGO_KEY_FILE" && chmod 400 "$MONGO_KEY_FILE" && chown mongodb:mongodb "$MONGO_KEY_FILE" && \ 177 | exec docker-entrypoint.sh mongod '"$MONGODB_CMD_ARGS"' \ 178 | ' 179 | 180 | if [ $? -ne 0 ]; then 181 | echo "Error starting MongoDB Docker container" 182 | exit 2 183 | fi 184 | 185 | echo "::endgroup::" 186 | 187 | wait_for_mongodb 188 | 189 | # After mongod is up, initiate the replica set 190 | # Use auth if credentials were supplied 191 | MONGODB_ARGS="" 192 | if [ -n "$MONGODB_USERNAME" ]; then 193 | MONGODB_ARGS="--username $MONGODB_USERNAME --password $MONGODB_PASSWORD" 194 | fi 195 | 196 | echo "::group::Initiating replica set [$MONGODB_REPLICA_SET]" 197 | 198 | docker exec --tty $MONGODB_CONTAINER_NAME $MONGODB_CLIENT --port $MONGODB_PORT $MONGODB_ARGS --eval " 199 | rs.initiate({ 200 | \"_id\": \"$MONGODB_REPLICA_SET\", 201 | \"members\": [ { 202 | \"_id\": 0, 203 | \"host\": \"$MONGODB_REPLICA_SET_HOST:$MONGODB_PORT\" 204 | } ] 205 | }) 206 | " 207 | 208 | echo "Success! Initiated replica set [$MONGODB_REPLICA_SET]" 209 | echo "::endgroup::" 210 | 211 | 212 | echo "::group::Checking replica set status [$MONGODB_REPLICA_SET]" 213 | docker exec --tty $MONGODB_CONTAINER_NAME $MONGODB_CLIENT --port $MONGODB_PORT $MONGODB_ARGS --eval "rs.status()" 214 | echo "::endgroup::" 215 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | 3 | 4 | 5 |
6 |
7 |

8 |

MongoDB in GitHub Actions

9 |

10 |

11 | Start a MongoDB server in your GitHub Actions. 12 |

13 |
14 |

15 | Usage 16 |

17 |
18 |
19 |

20 | Follow @marcuspoehls and @superchargejs for updates! 21 |

22 |
23 | 24 | --- 25 | 26 | 27 | ## Introduction 28 | 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. 29 | 30 | The MongoDB version must be specified using the `mongodb-version` input. The used version must exist in the published [`mongo` Docker hub tags](https://hub.docker.com/_/mongo?tab=tags). Default value is `latest`, other popular choices are `6.0`, `7.0` or even release candidates `8.0.0-rc4`. 31 | 32 | This is useful when running tests against a MongoDB database. 33 | 34 | 35 | ## Usage 36 | A code example says more than a 1000 words. Here’s an exemplary GitHub Action using a MongoDB server in different versions to test a Node.js app: 37 | 38 | ```yaml 39 | name: Run tests 40 | 41 | on: [push] 42 | 43 | jobs: 44 | build: 45 | runs-on: ubuntu-latest 46 | strategy: 47 | matrix: 48 | node-version: [22.x, 24.x] 49 | mongodb-version: ['6.0', '7.0', '8.0'] 50 | 51 | steps: 52 | - name: Git checkout 53 | uses: actions/checkout@v4 54 | 55 | - name: Use Node.js ${{ matrix.node-version }} 56 | uses: actions/setup-node@v4 57 | with: 58 | node-version: ${{ matrix.node-version }} 59 | 60 | - name: Start MongoDB 61 | uses: supercharge/mongodb-github-action@1.12.1 62 | with: 63 | mongodb-version: ${{ matrix.mongodb-version }} 64 | 65 | - run: npm install 66 | 67 | - run: npm test 68 | env: 69 | CI: true 70 | ``` 71 | 72 | 73 | ### Using a Custom MongoDB Port 74 | 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. 75 | 76 | The following example starts a MongoDB server on port `42069`: 77 | 78 | ```yaml 79 | name: Run tests 80 | 81 | on: [push] 82 | 83 | jobs: 84 | build: 85 | runs-on: ubuntu-latest 86 | strategy: 87 | matrix: 88 | node-version: [22.x, 24.x] 89 | mongodb-version: ['6.0', '7.0', '8.0'] 90 | 91 | steps: 92 | - name: Git checkout 93 | uses: actions/checkout@v4 94 | 95 | - name: Use Node.js ${{ matrix.node-version }} 96 | uses: actions/setup-node@v4 97 | with: 98 | node-version: ${{ matrix.node-version }} 99 | 100 | - name: Start MongoDB 101 | uses: supercharge/mongodb-github-action@1.12.1 102 | with: 103 | mongodb-version: ${{ matrix.mongodb-version }} 104 | mongodb-replica-set: test-rs 105 | mongodb-port: 42069 106 | 107 | - name: Install dependencies 108 | run: npm install 109 | 110 | - name: Run tests 111 | run: npm test 112 | env: 113 | CI: true 114 | ``` 115 | 116 | 117 | ### With a Replica Set (MongoDB `--replSet` Flag) 118 | 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. 119 | 120 | The following example uses the replica set name `test-rs`: 121 | 122 | ```yaml 123 | name: Run tests 124 | 125 | on: [push] 126 | 127 | jobs: 128 | build: 129 | runs-on: ubuntu-latest 130 | strategy: 131 | matrix: 132 | node-version: [22.x, 24.x] 133 | mongodb-version: ['6.0', '7.0', '8.0'] 134 | 135 | steps: 136 | - name: Git checkout 137 | uses: actions/checkout@v4 138 | 139 | - name: Use Node.js ${{ matrix.node-version }} 140 | uses: actions/setup-node@v4 141 | with: 142 | node-version: ${{ matrix.node-version }} 143 | 144 | - name: Start MongoDB 145 | uses: supercharge/mongodb-github-action@1.12.1 146 | with: 147 | mongodb-version: ${{ matrix.mongodb-version }} 148 | mongodb-replica-set: test-rs 149 | mongodb-port: 42069 150 | 151 | - name: Install dependencies 152 | run: npm install 153 | 154 | - name: Run tests 155 | run: npm test 156 | env: 157 | CI: true 158 | ``` 159 | 160 | 161 | ### With Authentication (MongoDB `--auth` Flag) 162 | Setting the `mongodb-username` and `mongodb-password` inputs. As per the [Dockerhub documentation](https://hub.docker.com/_/mongo), this automatically creates an admin user and enables `--auth` mode. 163 | 164 | The following example uses the username `supercharge`, password `secret` and also sets an initial `supercharge` database: 165 | 166 | ```yaml 167 | name: Run tests with authentication 168 | 169 | on: [push] 170 | 171 | jobs: 172 | build: 173 | runs-on: ubuntu-latest 174 | strategy: 175 | matrix: 176 | node-version: [22.x, 24.x] 177 | mongodb-version: ['6.0', '7.0', '8.0'] 178 | 179 | steps: 180 | - name: Git checkout 181 | uses: actions/checkout@v4 182 | 183 | - name: Use Node.js ${{ matrix.node-version }} 184 | uses: actions/setup-node@v4 185 | with: 186 | node-version: ${{ matrix.node-version }} 187 | 188 | - name: Start MongoDB 189 | uses: supercharge/mongodb-github-action@1.12.1 190 | with: 191 | mongodb-version: ${{ matrix.mongodb-version }} 192 | mongodb-username: supercharge 193 | mongodb-password: secret 194 | mongodb-db: supercharge 195 | 196 | - name: Install dependencies 197 | run: npm install 198 | 199 | - name: Run tests 200 | run: npm test 201 | env: 202 | CI: true 203 | ``` 204 | 205 | ### With Custom Container Name 206 | The container name of the created MongoDB instance can be configured using the `mongodb-container-name` input 207 | 208 | The following example will parameterize the MongoDB container name based on the `node-version` and `mongodb-version` being used from the matrix: 209 | 210 | ```yaml 211 | name: Run with Custom Container Names 212 | 213 | on: [push] 214 | 215 | jobs: 216 | build: 217 | runs-on: ubuntu-latest 218 | strategy: 219 | matrix: 220 | node-version: [22.x, 24.x] 221 | mongodb-version: ['6.0', '7.0', '8.0'] 222 | 223 | steps: 224 | - name: Git checkout 225 | uses: actions/checkout@v4 226 | 227 | - name: Use Node.js ${{ matrix.node-version }} 228 | uses: actions/setup-node@v4 229 | with: 230 | node-version: ${{ matrix.node-version }} 231 | 232 | - name: Start MongoDB 233 | uses: supercharge/mongodb-github-action@1.12.1 234 | with: 235 | mongodb-version: ${{ matrix.mongodb-version }} 236 | mongodb-container-name: mongodb-${{ matrix.node-version }}-${{ matrix.mongodb-version }} 237 | 238 | - name: Install dependencies 239 | run: npm install 240 | 241 | - name: Run tests 242 | run: npm test 243 | env: 244 | CI: true 245 | ``` 246 | 247 | **Caveat:** due to [this issue](https://github.com/docker-library/mongo/issues/211), 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. 248 | 249 | ### Using a Custom Mongo Image 250 | You can utilize an alternative MongoDB docker image using the `mongodb-image` input: 251 | 252 | 253 | ```yaml 254 | - name: Start MongoDB 255 | uses: supercharge/mongodb-github-action@1.12.1 256 | with: 257 | # Here we are using an image from Amazon's ECR rather than the default image from Docker Hub 258 | mongodb-image: 'public.ecr.aws/docker/library/mongo' 259 | mongodb-version: ${{ matrix.mongodb-version }} 260 | ``` 261 | 262 | ## License 263 | MIT © [Supercharge](https://superchargejs.com) 264 | 265 | --- 266 | 267 | > [superchargejs.com](https://superchargejs.com)  ·  268 | > GitHub [@supercharge](https://github.com/supercharge)  ·  269 | > Twitter [@superchargejs](https://twitter.com/superchargejs) 270 | --------------------------------------------------------------------------------