Skip to content

Commit

Permalink
🚧 progress: First attempt at defining CI MongoDB instances once.
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed Apr 23, 2024
1 parent f41548a commit 50199f3
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 71 deletions.
12 changes: 12 additions & 0 deletions .ci/storage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
services:
database:
extends:
file: ../storage.yaml
service: database
networks:
- network

networks:
network:
name: "${MONGO_NETWORK}"
external: true
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ ROOT_URL=http://localhost
PORT=3000

MONGO_VERSION=5.0
MONGO_HOSTNAME=patient-db
MONGO_DATABASE_NAME=meteor
MONGO_URL=mongodb://patient-db:27017/meteor
MONGO_REPLICA_SET_NAME=meteor
MONGO_DB_PATH=/data/db

HTTP_FORWARDED_COUNT=1
6 changes: 5 additions & 1 deletion .github/actions/compose-container/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ description: >
inputs:

compose-file:
required: true

service:
required: true

Expand All @@ -25,11 +28,12 @@ runs:
id: fetch

env:
COMPOSE_FILE: ${{ inputs.compose-file }}
SERVICE: ${{ inputs.service }}

shell: bash

run: |
CONTAINER_ID="$(docker compose ps -q "${SERVICE}")"
CONTAINER_ID="$(docker compose -f "${COMPOSE_FILE}" ps -q "${SERVICE}")"
echo "id=${CONTAINER_ID}" >> "$GITHUB_OUTPUT"
test -n "${CONTAINER_ID}"
19 changes: 19 additions & 0 deletions .github/actions/database-down/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: database-down
description: >
Stop the database container.
inputs:

runs:

using: composite

steps:

- name: Stop database service
run: |
docker compose -f storage.yaml stop database
- name: Remove database service
run: |
docker compose -f storage.yaml rm database
52 changes: 52 additions & 0 deletions .github/actions/database-up/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: database-up
description: >
Run the database container.
inputs:

name:
default: mongodb
required: true

hostname:
required: true

version:
required: true

replica-set:
required: true

network:
required: true

runs:

using: composite

steps:

- name: Run a detached MongoDB container

id: run

env:
MONGO_VERSION: ${{ inputs.version }}
MONGO_REPLICA_SET_NAME: ${{ inputs.replica-set }}
MONGO_DB_PATH: /data/db
MONGO_HOSTNAME: ${{ inputs.hostname }}
MONGO_NAME: ${{ inputs.name }}
MONGO_NETWORK: ${{ inputs.network }}
COMPOSE_FILE: ${{ inputs.network == 'host' && 'storage.yaml' || '.ci/storage.yaml' }}
PUBLISH_FLAGS: ${{ inputs.network == 'host' && '--publish 27017:27017' || '' }}

shell: bash

run: |
mkdir -p "${GITHUB_WORKSPACE}/${MONGO_DB_PATH}"
docker compose -f "${COMPOSE_FILE}" run \
--detach \
${PUBLISH_FLAGS} \
--volume "${GITHUB_WORKSPACE}/${MONGO_DB_PATH}:${MONGO_DB_PATH}" \
--name "${MONGO_NAME}" \
database
40 changes: 18 additions & 22 deletions .github/workflows/ci:build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ jobs:
scripts/assert-replica-set.js
.github/actions/is-healthy/action.yml
.github/actions/is-reachable/action.yml
storage.yaml
.github/actions/database-up/action.yml
.github/actions/database-down/action.yml
sparse-checkout-cone-mode: false

- name: Load build 💽
Expand Down Expand Up @@ -150,30 +153,24 @@ jobs:
npm install
- name: Start MongoDB container
env:
MONGO_VERSION: '5.0'
run: |
mkdir -p "${GITHUB_WORKSPACE}/data/db"
docker container run \
--detach \
--restart unless-stopped \
--publish 27017:27017 \
--volume "${GITHUB_WORKSPACE}/data/db":/data/db \
--volume "${GITHUB_WORKSPACE}/scripts/assert-replica-set.js":/scripts/assert-replica-set.js \
--log-opt max-size=100m \
--log-opt max-file=7 \
--health-cmd 'mongo --eval "replSet = \"meteor\"; hostname = \"localhost\"" /scripts/assert-replica-set.js' \
--health-interval 5s \
--health-retries 3 \
--health-timeout 10s \
--health-start-period 40s \
--name mongodb \
"mongo:${MONGO_VERSION}" mongod --dbpath /data/db --replSet meteor
uses: ./.github/actions/database-up
with:
name: mongodb
network: host
hostname: localhost
version: '5.0'
replica-set: meteor

- id: patient-db-container
uses: ./.github/actions/compose-container
with:
compose-file: storage.yaml
service: mongodb

- name: Wait for MongoDB container to be healthy
uses: ./.github/actions/is-healthy
with:
container: mongodb
container: ${{ steps.patient-db-container.outputs.id }}
timeout: 60

- name: Run server
Expand Down Expand Up @@ -208,5 +205,4 @@ jobs:
kill -SIGTERM ${{ steps.server.outputs.pid }}
- name: Stop MongoDB container
run: |
docker container stop mongodb
uses: ./.github/actions/database-down
2 changes: 2 additions & 0 deletions .github/workflows/ci:build:compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ jobs:
- id: patient-db-container
uses: ./.github/actions/compose-container
with:
compose-file: compose.yaml
service: patient-db

- name: Wait for database container to be healthy
Expand All @@ -111,6 +112,7 @@ jobs:
- id: patient-web-container
uses: ./.github/actions/compose-container
with:
compose-file: compose.yaml
service: patient-web

- name: Wait for web container to be healthy
Expand Down
44 changes: 21 additions & 23 deletions .github/workflows/ci:build:image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,15 @@ jobs:
with:
sparse-checkout: |
scripts/assert-replica-set.js
storage.yaml
.ci/storage.yaml
.github/actions/ip-address/action.yml
.github/actions/is-healthy/action.yml
.github/actions/is-reachable/action.yml
.github/actions/is-running/action.yml
.github/actions/compose-container/action.yml
.github/actions/database-up/action.yml
.github/actions/database-down/action.yml
sparse-checkout-cone-mode: false

- name: Get server image URL
Expand All @@ -112,30 +117,24 @@ jobs:
docker network create patient-network
- name: Start MongoDB container
env:
MONGO_VERSION: '5.0'
run: |
mkdir -p "${GITHUB_WORKSPACE}/data/db"
docker container run \
--detach \
--restart unless-stopped \
--network patient-network \
--volume "${GITHUB_WORKSPACE}/data/db":/data/db \
--volume "${GITHUB_WORKSPACE}/scripts/assert-replica-set.js":/scripts/assert-replica-set.js \
--log-opt max-size=100m \
--log-opt max-file=7 \
--health-cmd 'mongo --eval "replSet = \"meteor\"; hostname = \"mongodb\"" /scripts/assert-replica-set.js' \
--health-interval 5s \
--health-retries 3 \
--health-timeout 10s \
--health-start-period 40s \
--name mongodb \
"mongo:${MONGO_VERSION}" mongod --dbpath /data/db --replSet meteor
uses: ./.github/actions/database-up
with:
name: mongodb
network: patient-network
hostname: mongodb
version: '5.0'
replica-set: meteor

- id: patient-db-container
uses: ./.github/actions/compose-container
with:
compose-file: .ci/storage.yaml
service: mongodb

- name: Wait for MongoDB container to be healthy
uses: ./.github/actions/is-healthy
with:
container: mongodb
container: ${{ steps.patient-db-container.outputs.id }}
timeout: 60

- name: Run server
Expand Down Expand Up @@ -242,9 +241,8 @@ jobs:
run: |
docker container rm server
- name: Remove MongoDB container
run: |
docker container rm mongodb
- name: Stop MongoDB container
uses: ./.github/actions/database-down

- name: Show docker containers
if: always()
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/ci:test:deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:
- id: patient-db-container
uses: ./.github/actions/compose-container
with:
compose-file: compose.yaml
service: patient-db

- name: Wait for database container to be healthy
Expand All @@ -71,6 +72,7 @@ jobs:
- id: patient-web-container
uses: ./.github/actions/compose-container
with:
compose-file: compose.yaml
service: patient-web

- name: Wait for web container to be healthy
Expand Down
29 changes: 4 additions & 25 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,13 @@ services:

patient-db:
restart: always
image: "mongo:${MONGO_VERSION}"
extends:
file: storage.yaml
service: database
volumes:
- patient-data:/data/db
- ./scripts/assert-replica-set.js:/scripts/assert-replica-set.js
- "patient-data:${MONGO_DB_PATH}"
networks:
- patient-network
logging:
driver: "json-file"
options:
max-size: "100m"
max-file: "7"
command: [
"--bind_ip", "localhost,patient-db",
"--dbpath", "/data/db",
"--replSet", "meteor"
]
healthcheck:
test: [
"CMD",
"mongo",
"--eval", "replSet = 'meteor'; hostname = 'patient-db'",
"/scripts/assert-replica-set.js"
]
start_period: 40s
start_interval: 2s
interval: 1m30s
timeout: 10s
retries: 3

networks:
patient-network:
Expand Down
28 changes: 28 additions & 0 deletions storage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
services:
database:
restart: no
image: "mongo:${MONGO_VERSION}"
volumes:
- ./scripts/assert-replica-set.js:/scripts/assert-replica-set.js
logging:
driver: "json-file"
options:
max-size: "100m"
max-file: "7"
command: [
"--bind_ip", "localhost,${MONGO_HOSTNAME}",
"--dbpath", "${MONGO_DB_PATH}",
"--replSet", "${MONGO_REPLICA_SET_NAME}"
]
healthcheck:
test: [
"CMD",
"mongo",
"--eval", "replSet = '${MONGO_REPLICA_SET_NAME}'; hostname = '${MONGO_HOSTNAME}'",
"/scripts/assert-replica-set.js"
]
start_period: 40s
start_interval: 2s
interval: 1m30s
timeout: 10s
retries: 3

0 comments on commit 50199f3

Please sign in to comment.