Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First draft for compose.yaml #880

Merged
merged 9 commits into from
Apr 17, 2024
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
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ROOT_URL=http://localhost
PORT=3000

MONGO_VERSION=5.0
MONGO_URL=mongodb://patient-db:27017/meteor
12 changes: 6 additions & 6 deletions .github/workflows/ci:build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ jobs:
with:
sparse-checkout: |
scripts/healthcheck.cjs
scripts/ensure-replica-set.js
sparse-checkout-cone-mode: false

- name: Load build 💽
Expand Down Expand Up @@ -150,22 +151,21 @@ jobs:
env:
MONGO_VERSION: '5.0'
run: |
echo 'dbpath=/data/db' > "${GITHUB_WORKSPACE}/mongodb.conf"
echo 'replSet=meteor' >> "${GITHUB_WORKSPACE}/mongodb.conf"
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}/mongodb.conf":/mongodb.conf \
--volume "${GITHUB_WORKSPACE}/scripts/ensure-replica-set.js":/scripts/ensure-replica-set.js \
--log-opt max-size=100m \
--log-opt max-file=7 \
--name mongodb \
"mongo:${MONGO_VERSION}" mongod -f /mongodb.conf
"mongo:${MONGO_VERSION}" mongod --dbpath /data/db --replSet meteor
sleep 10
docker exec mongodb mongo --eval \
'try {var c = rs.config();} catch (e){} if (c && c.members[0].host === "127.0.0.1:27017") { c.members[0].host = "127.0.0.1:27017"; rs.reconfig(c); } else { rs.initiate({_id: "meteor", members: [{_id: 0, host: "127.0.0.1:27017"}]}) }'
docker exec mongodb mongo \
--eval 'replSet = "meteor"; address = "127.0.0.1"' \
/scripts/ensure-replica-set.js

- name: Run server
id: server
Expand Down
81 changes: 81 additions & 0 deletions .github/workflows/ci:build:compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: ci:build:compose

on:
push:
branches:
- main
pull_request:
merge_group:

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:

build:
name: Continuous integration (build compose)
strategy:
matrix:
platform:
- ubuntu-latest

runs-on: ${{ matrix.platform }}

timeout-minutes: 18

steps:
- name: Checkout 🛎️
uses: actions/checkout@v4

- name: Wait for image build workflow to succeed
uses: ArcticLampyrid/[email protected]
with:
workflow: ci:build:image.yml
sha: ${{ github.sha }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Bake 🧑‍🍳
uses: docker/bake-action@v4
with:
load: true
files: |
compose.yaml
set: |
patient-web.cache-from=type=gha
patient-web.cache-to=type=gha,mode=max

- name: Start
run: |
docker compose up --detach

- name: Wait for database container to be healthy
run: |
timeout 60 bash -c \
'until docker inspect --format "{{json .State.Health }}" "$(docker compose ps -q patient-db)" | jq -e ".Status == \"healthy\"" ; do sleep 1; done'

- name: Wait for web port to be available
run: |
timeout 60 bash -c \
'until nc -z -v -w5 127.0.0.1 3000 ; do sleep 1; done'

- name: Wait for web container to be healthy
run: |
timeout 60 bash -c \
'until docker inspect --format "{{json .State.Health }}" "$(docker compose ps -q patient-web)" | jq -e ".Status == \"healthy\"" ; do sleep 1; done'

- name: Call healthcheck endpoint
run: |
timeout 60 bash -c \
'curl -f http://localhost:3000/api/healthcheck'

- name: Run healthcheck script
run: |
timeout 60 bash -c \
'node scripts/healthcheck.cjs http://localhost:3000/api/healthcheck'

- name: Stop
run: |
docker compose down
17 changes: 11 additions & 6 deletions .github/workflows/ci:build:image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ jobs:
timeout-minutes: 3

steps:
- uses: actions/checkout@v4
with:
sparse-checkout: |
scripts/ensure-replica-set.js
sparse-checkout-cone-mode: false

- name: Log in to GitHub Packages registry
uses: docker/login-action@v3
with:
Expand All @@ -96,22 +102,21 @@ jobs:
env:
MONGO_VERSION: '5.0'
run: |
echo 'dbpath=/data/db' > "${GITHUB_WORKSPACE}/mongodb.conf"
echo 'replSet=meteor' >> "${GITHUB_WORKSPACE}/mongodb.conf"
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}/mongodb.conf":/mongodb.conf \
--volume "${GITHUB_WORKSPACE}/scripts/ensure-replica-set.js":/scripts/ensure-replica-set.js \
--log-opt max-size=100m \
--log-opt max-file=7 \
--name mongodb \
"mongo:${MONGO_VERSION}" mongod -f /mongodb.conf
"mongo:${MONGO_VERSION}" mongod --dbpath /data/db --replSet meteor
sleep 5
docker exec mongodb mongo --eval \
'try {var c = rs.config();} catch (e){} if (c && c.members[0].host === "mongodb:27017") { c.members[0].host = "mongodb:27017"; rs.reconfig(c); } else { rs.initiate({_id: "meteor", members: [{_id: 0, host: "mongodb:27017"}]}) }'
docker exec mongodb mongo \
--eval 'replSet = "meteor"; hostname = "mongodb"' \
/scripts/ensure-replica-set.js

- name: Run server
env:
Expand Down
53 changes: 53 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
services:
patient-web:
restart: always
build:
context: .
environment:
- ROOT_URL=${ROOT_URL}
- PORT=${PORT}
- MONGO_URL=${MONGO_URL}
depends_on:
patient-db:
condition: service_healthy
networks:
- patient-network
ports:
- "${PORT}:${PORT}"

patient-db:
restart: always
image: "mongo:${MONGO_VERSION}"
volumes:
- patient-data:/data/db
- ./scripts/ensure-replica-set.js:/scripts/ensure-replica-set.js
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/ensure-replica-set.js"
]
start_period: 40s
start_interval: 2s
interval: 1m30s
timeout: 10s
retries: 3

networks:
patient-network:

volumes:
patient-data:
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,15 @@
"unicorn/prefer-module": "off"
}
},
{
"files": [
"scripts/ensure-replica-set.js"
],
"rules": {
"no-var": "off",
"no-undef": "off"
}
},
{
"files": [
".deploy/*/mup.js",
Expand Down
26 changes: 26 additions & 0 deletions scripts/ensure-replica-set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// @ts-nocheck

var c;

try {
c = rs.config();
} catch {}

var replSet = replSet || 'rs01';
var port = port || '27017';
var hostname = hostname || 'localhost';
var address = address || hostname || '127.0.0.1';
var host = host || `${address}:${port}`;

if (c === undefined) {
rs.initiate({_id: replSet, members: [{_id: 0, host}]});
} else if (c.members[0].host !== host) {
c.members[0].host = host;
rs.reconfig(c);
}

var status = rs.status();

if (status.members[0].health !== 1) quit(2);

quit(0);
Loading