Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Example for MongoDB #21

Merged
merged 7 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions .github/workflows/mongodb-service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Mongodb Service Example

on:
push:
branches:
- master
pull_request:
branches:
- master

defaults:
run:
working-directory: ./mongodb

jobs:
container-job:
runs-on: ubuntu-latest

# runs all of the steps inside the specified container rather than on the VM host.
# Because of this the network configuration changes from host based network to a container network.
container:
image: node:10.16-jessie

services:
mongodb:
image: mongo
ports:
- 27017:27017

steps:
- uses: actions/checkout@v1
- run: npm ci
- run: node client.js
env:
# use mongodb for the host here because we have specified a container for the job.
# If we were running the job on the VM this would be localhost
MONGODB_HOST: mongodb
MONGODB_PORT: ${{ job.services.mongodb.ports[27017] }}

# Runs all steps on the VM
# The service containers will use host port binding instead of container networking so you access them via localhost rather than the service name
vm-job:
runs-on: ubuntu-latest

services:
mongodb:
image: mongo
ports:
# will assign a random free host port
- 27017/tcp

steps:
- uses: actions/checkout@v1
- run: npm ci
- run: node client.js
env:
# use localhost for the host here because we are running the job on the VM.
# If we were running the job on in a container this would be mongodb
MONGODB_HOST: localhost
MONGODB_PORT: ${{ job.services.mongodb.ports[27017] }} # get randomly assigned published port
29 changes: 29 additions & 0 deletions mongodb/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { MongoClient } = require("mongodb");
const PORT = process.env.MONGODB_PORT || 27017;
const HOST = process.env.MONGODB_HOST || "localhost";

async function main() {
const client = new MongoClient(`mongodb://${HOST}:${PORT}/dbName`, {
useUnifiedTopology: true
});
try {
await client.connect();
await client
.db()
.collection("exampleCollection")
.insertOne({
someKey: "someVal"
});
const result = await client
.db()
.collection("exampleCollection")
.findOne({});
console.log(JSON.stringify(result));
} catch (e) {
console.error(e);
} finally {
await client.close();
}
}

main().catch(console.error);
147 changes: 147 additions & 0 deletions mongodb/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions mongodb/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"mongodb": "^3.5.2"
}
}