From 50199f346d424d603bc0bb6a1e1bf83c8324a153 Mon Sep 17 00:00:00 2001 From: make-github-pseudonymous-again <5165674+make-github-pseudonymous-again@users.noreply.github.com> Date: Mon, 22 Apr 2024 23:05:57 +0200 Subject: [PATCH] :construction: progress: First attempt at defining CI MongoDB instances once. --- .ci/storage.yaml | 12 +++++ .env | 4 ++ .github/actions/compose-container/action.yml | 6 ++- .github/actions/database-down/action.yml | 19 +++++++ .github/actions/database-up/action.yml | 52 ++++++++++++++++++++ .github/workflows/ci:build.yml | 40 +++++++-------- .github/workflows/ci:build:compose.yml | 2 + .github/workflows/ci:build:image.yml | 44 ++++++++--------- .github/workflows/ci:test:deploy.yml | 2 + compose.yaml | 29 ++--------- storage.yaml | 28 +++++++++++ 11 files changed, 167 insertions(+), 71 deletions(-) create mode 100644 .ci/storage.yaml create mode 100644 .github/actions/database-down/action.yml create mode 100644 .github/actions/database-up/action.yml create mode 100644 storage.yaml diff --git a/.ci/storage.yaml b/.ci/storage.yaml new file mode 100644 index 000000000..395fcd438 --- /dev/null +++ b/.ci/storage.yaml @@ -0,0 +1,12 @@ +services: + database: + extends: + file: ../storage.yaml + service: database + networks: + - network + +networks: + network: + name: "${MONGO_NETWORK}" + external: true diff --git a/.env b/.env index 85c792594..f9f3ac186 100644 --- a/.env +++ b/.env @@ -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 diff --git a/.github/actions/compose-container/action.yml b/.github/actions/compose-container/action.yml index 238ccfa90..4a8a57e1c 100644 --- a/.github/actions/compose-container/action.yml +++ b/.github/actions/compose-container/action.yml @@ -4,6 +4,9 @@ description: > inputs: + compose-file: + required: true + service: required: true @@ -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}" diff --git a/.github/actions/database-down/action.yml b/.github/actions/database-down/action.yml new file mode 100644 index 000000000..b26290d6d --- /dev/null +++ b/.github/actions/database-down/action.yml @@ -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 diff --git a/.github/actions/database-up/action.yml b/.github/actions/database-up/action.yml new file mode 100644 index 000000000..d92fda9be --- /dev/null +++ b/.github/actions/database-up/action.yml @@ -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 diff --git a/.github/workflows/ci:build.yml b/.github/workflows/ci:build.yml index cf83da8b8..923049789 100644 --- a/.github/workflows/ci:build.yml +++ b/.github/workflows/ci:build.yml @@ -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 💽 @@ -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 @@ -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 diff --git a/.github/workflows/ci:build:compose.yml b/.github/workflows/ci:build:compose.yml index c9e1ef27f..f82ee34e2 100644 --- a/.github/workflows/ci:build:compose.yml +++ b/.github/workflows/ci:build:compose.yml @@ -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 @@ -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 diff --git a/.github/workflows/ci:build:image.yml b/.github/workflows/ci:build:image.yml index 280f7aee6..2299eec02 100644 --- a/.github/workflows/ci:build:image.yml +++ b/.github/workflows/ci:build:image.yml @@ -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 @@ -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 @@ -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() diff --git a/.github/workflows/ci:test:deploy.yml b/.github/workflows/ci:test:deploy.yml index e531e33f0..781a3aea3 100644 --- a/.github/workflows/ci:test:deploy.yml +++ b/.github/workflows/ci:test:deploy.yml @@ -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 @@ -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 diff --git a/compose.yaml b/compose.yaml index ceef31102..6fc6ecc86 100644 --- a/compose.yaml +++ b/compose.yaml @@ -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: diff --git a/storage.yaml b/storage.yaml new file mode 100644 index 000000000..53cf1481e --- /dev/null +++ b/storage.yaml @@ -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