├── .eslintrc.json
├── .github
└── workflows
│ ├── main.yml
│ ├── test-auth.yml
│ ├── test-replica-set.yml
│ ├── test-single-instance.yml
│ └── validate-action-typings.yml
├── .gitignore
├── CHANGELOG.md
├── Dockerfile
├── LICENSE
├── README.md
├── action-types.yml
├── action.yml
├── docs
└── release.md
├── package.json
├── start-mongodb.sh
└── test
├── custom-port
└── custom-port.js
├── replica-set
└── replica-set.js
└── single-instance
└── single-instance.js
/.eslintrc.json:
--------------------------------------------------------------------------------
1 | {
2 | "env": {
3 | "node": true
4 | },
5 | "extends": [
6 | "@supercharge"
7 | ],
8 | "parserOptions": {
9 | "ecmaVersion": 2018
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/.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 |
--------------------------------------------------------------------------------
/.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 |
--------------------------------------------------------------------------------
/.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 |
--------------------------------------------------------------------------------
/.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/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 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Changelog
2 |
3 |
4 | ## [1.12.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.11.0...v1.12.0) - 2025-01-05
5 |
6 | ### Added
7 | - 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.
8 |
9 | ### Updated
10 | - bump dependencies
11 |
12 |
13 | ## [1.11.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.10.0...v1.11.0) - 2024-05-22
14 |
15 | ### Added
16 | - added `mongodb-container-name` input: this option allows you to define the Docker container name
17 |
18 | ### Fixed
19 | - 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)
20 |
21 | ### Updated
22 | - bump dependencies
23 |
24 |
25 | ## [1.10.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.9.0...v1.10.0) - 2023-08-07
26 |
27 | ### Added
28 | - MongoDB single instance: wait for MongoDB to be ready
29 |
30 | ### Updated
31 | - bump dependencies
32 |
33 |
34 | ## [1.9.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.8.0...v1.9.0) - 2023-02-10
35 |
36 | ### Added
37 | - exit early in case of Docker issues (e.g., unavailable MongoDB version)
38 | - validate GitHub Action typings using [krzema12/github-actions-typing](https://github.com/krzema12/github-actions-typing)
39 |
40 | ### Updated
41 | - bump dependencies
42 |
43 |
44 | ## [1.8.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.7.0...v1.8.0) - 2022-08-26
45 |
46 | ### Added
47 | - support MongoDB 6.x. Thanks to [Evandro aka ecarruda](https://github.com/ecarruda)!
48 | - use `mongo` for MongoDB 4.x release line
49 | - use `mongosh` for MongoDB 5.x and 6.x release lines
50 |
51 | ### Updated
52 | - bump dependencies
53 | - use `@supercharge/eslint-config` instead of `eslint-config-standard`
54 |
55 |
56 | ## [1.7.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.6.0...v1.7.0) - 2021-11-16
57 |
58 | ### Added
59 | - `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`)
60 | - `mongodb-db` input allowing you to create a database that is used for creation scripts
61 |
62 | ### Updated
63 | - bump dependencies
64 | - use UVU and c8 for testing instead of @hapi/lab
65 |
66 |
67 | ## [1.6.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.5.0...v1.6.0) - 2021-06-01
68 |
69 | ### Added
70 | - show replica set status after `rs.initiate()`
71 |
72 |
73 | ## [1.5.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.4.1...v1.5.0) - 2021-05-31
74 |
75 | ### Added
76 | - refined tests for custom ports
77 | - refined custom port handling when using replica sets
78 | - extended output for better debugging in GitHub Actions
79 |
80 |
81 | ## [1.4.1](https://github.com/superchargejs/mongodb-github-action/compare/v1.4.0...v1.4.1) - 2021-05-24
82 |
83 | ### Updated
84 | - use strings for `mongodb-version` in Readme and GitHub Actions Workflows
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 | ### Added
90 | - `mongodb-port` input allowing to start a MongoDB instance (or single-node replica set) on a custom port
91 |
92 |
93 | ## [1.4.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.3.0...v1.4.0) - 2021-05-11
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.3.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.2.0...v1.3.0) - 2020-04-10
100 |
101 | ### Added
102 | - check if the `mongodb-version` input is present: if not, print a message and fail the job
103 |
104 |
105 | ## [1.2.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.1.0...v1.2.0) - 2020-03-30
106 |
107 | ### Added
108 | - `mongodb-replica-set` input to define a MongoDB replica set name
109 | - tests connecting to MongoDB instances (single instance, replica set) created by this GitHub Action
110 |
111 |
112 | ## [1.1.0](https://github.com/superchargejs/mongodb-github-action/compare/v1.0.0...v1.1.0) - 2019-12-18
113 |
114 | ### Updated
115 | - switched from a Node.js workflow to a Docker-based workflow
116 | - reduces noise in the repo by removing the Node.js dependencies and only relying on a shell script
117 | - green database icon in GitHub Action details
118 |
119 |
120 | ## 1.0.0 - 2019-12-17
121 |
122 | ### Added
123 | - `1.0.0` release 🚀 🎉
124 |
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM docker:stable
2 | COPY start-mongodb.sh /start-mongodb.sh
3 | RUN chmod +x /start-mongodb.sh
4 | ENTRYPOINT ["/start-mongodb.sh"]
5 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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: [20.x, 22.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.0
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: [20.x, 22.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.0
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: [20.x, 22.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.0
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: [20.x, 22.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.0
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: [20.x, 22.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.0
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.0
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 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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 | runs:
50 | using: 'docker'
51 | image: 'Dockerfile'
52 | args:
53 | - ${{ inputs.mongodb-image }}
54 | - ${{ inputs.mongodb-version }}
55 | - ${{ inputs.mongodb-replica-set }}
56 | - ${{ inputs.mongodb-port }}
57 | - ${{ inputs.mongodb-db }}
58 | - ${{ inputs.mongodb-username }}
59 | - ${{ inputs.mongodb-password }}
60 | - ${{ inputs.mongodb-container-name }}
61 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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 |
13 | # `mongosh` is used starting from MongoDB 5.x
14 | MONGODB_CLIENT="mongosh --quiet"
15 |
16 | if [ -z "$MONGODB_IMAGE" ]; then
17 | echo ""
18 | echo "Missing MongoDB image in the [mongodb-image] input. Received value: $MONGODB_IMAGE"
19 | echo ""
20 |
21 | exit 2
22 | fi
23 |
24 | if [ -z "$MONGODB_VERSION" ]; then
25 | echo ""
26 | echo "Missing MongoDB version in the [mongodb-version] input. Received value: $MONGODB_VERSION"
27 | echo ""
28 |
29 | exit 2
30 | fi
31 |
32 | echo "::group::Using MongoDB Docker image $MONGODB_IMAGE:$MONGODB_VERSION"
33 |
34 | echo "::group::Selecting correct MongoDB client"
35 | if [ "`echo $MONGODB_VERSION | cut -c 1`" -le "4" ]; then
36 | MONGODB_CLIENT="mongo"
37 | fi
38 | echo " - Using MongoDB client: [$MONGODB_CLIENT]"
39 | echo ""
40 | echo "::endgroup::"
41 |
42 |
43 | # Helper function to wait for MongoDB to be started before moving on
44 | wait_for_mongodb () {
45 | echo "::group::Waiting for MongoDB to accept connections"
46 | sleep 1
47 | TIMER=0
48 |
49 | MONGODB_ARGS=""
50 |
51 | if [ -z "$MONGODB_REPLICA_SET" ]
52 | then
53 | if [ -z "$MONGODB_USERNAME" ]
54 | then
55 | MONGODB_ARGS=""
56 | else
57 | # no replica set, but username given: use them as args
58 | MONGODB_ARGS="--username $MONGODB_USERNAME --password $MONGODB_PASSWORD"
59 | fi
60 | fi
61 |
62 | # until ${WAIT_FOR_MONGODB_COMMAND}
63 | until docker exec --tty $MONGODB_CONTAINER_NAME $MONGODB_CLIENT --port $MONGODB_PORT $MONGODB_ARGS --eval "db.serverStatus()"
64 | do
65 | echo "."
66 | sleep 1
67 | TIMER=$((TIMER + 1))
68 |
69 | if [[ $TIMER -eq 20 ]]; then
70 | echo "MongoDB did not initialize within 20 seconds. Exiting."
71 | exit 2
72 | fi
73 | done
74 | echo "::endgroup::"
75 | }
76 |
77 |
78 | # check if the container already exists and remove it
79 | ## TODO: put this behind an option flag
80 | # if [ "$(docker ps -q -f name=$MONGODB_CONTAINER_NAME)" ]; then
81 | # echo "Removing existing container [$MONGODB_CONTAINER_NAME]"
82 | # docker rm -f $MONGODB_CONTAINER_NAME
83 | # fi
84 |
85 |
86 | if [ -z "$MONGODB_REPLICA_SET" ]; then
87 | echo "::group::Starting single-node instance, no replica set"
88 | echo " - port [$MONGODB_PORT]"
89 | echo " - version [$MONGODB_VERSION]"
90 | echo " - database [$MONGODB_DB]"
91 | echo " - credentials [$MONGODB_USERNAME:$MONGODB_PASSWORD]"
92 | echo " - container-name [$MONGODB_CONTAINER_NAME]"
93 | echo ""
94 |
95 | docker run --name $MONGODB_CONTAINER_NAME --publish $MONGODB_PORT:$MONGODB_PORT -e MONGO_INITDB_DATABASE=$MONGODB_DB -e MONGO_INITDB_ROOT_USERNAME=$MONGODB_USERNAME -e MONGO_INITDB_ROOT_PASSWORD=$MONGODB_PASSWORD --detach $MONGODB_IMAGE:$MONGODB_VERSION --port $MONGODB_PORT
96 |
97 | if [ $? -ne 0 ]; then
98 | echo "Error starting MongoDB Docker container"
99 | exit 2
100 | fi
101 | echo "::endgroup::"
102 |
103 | wait_for_mongodb
104 |
105 | exit 0
106 | fi
107 |
108 |
109 | echo "::group::Starting MongoDB as single-node replica set"
110 | echo " - port [$MONGODB_PORT]"
111 | echo " - version [$MONGODB_VERSION]"
112 | echo " - replica set [$MONGODB_REPLICA_SET]"
113 | echo ""
114 |
115 |
116 | docker run --name $MONGODB_CONTAINER_NAME --publish $MONGODB_PORT:$MONGODB_PORT --detach $MONGODB_IMAGE:$MONGODB_VERSION --port $MONGODB_PORT --replSet $MONGODB_REPLICA_SET
117 |
118 | if [ $? -ne 0 ]; then
119 | echo "Error starting MongoDB Docker container"
120 | exit 2
121 | fi
122 | echo "::endgroup::"
123 |
124 | wait_for_mongodb
125 |
126 | echo "::group::Initiating replica set [$MONGODB_REPLICA_SET]"
127 |
128 | docker exec --tty $MONGODB_CONTAINER_NAME $MONGODB_CLIENT --port $MONGODB_PORT --eval "
129 | rs.initiate({
130 | \"_id\": \"$MONGODB_REPLICA_SET\",
131 | \"members\": [ {
132 | \"_id\": 0,
133 | \"host\": \"localhost:$MONGODB_PORT\"
134 | } ]
135 | })
136 | "
137 |
138 | echo "Success! Initiated replica set [$MONGODB_REPLICA_SET]"
139 | echo "::endgroup::"
140 |
141 |
142 | echo "::group::Checking replica set status [$MONGODB_REPLICA_SET]"
143 | docker exec --tty $MONGODB_CONTAINER_NAME $MONGODB_CLIENT --port $MONGODB_PORT --eval "rs.status()"
144 | echo "::endgroup::"
145 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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 } = 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 ? 'admin' : 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 |
--------------------------------------------------------------------------------