From b6d104b22ab21d524688a08eaa6b427eaefc1dc2 Mon Sep 17 00:00:00 2001 From: EmmaLRussell Date: Mon, 12 Aug 2024 16:28:34 +0100 Subject: [PATCH] db migrate image --- .github/workflows/docker-e2e.yml | 9 +++++++-- docker/common | 13 ++++++++++--- docker/db-migrate/Dockerfile | 7 +++++++ docker/db-migrate/build | 9 +++++++++ docker/db-migrate/push | 6 ++++++ docker/push-branch-tag | 7 +++++-- docker/run | 19 +++++++++++++------ 7 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 docker/db-migrate/Dockerfile create mode 100644 docker/db-migrate/build create mode 100644 docker/db-migrate/push diff --git a/.github/workflows/docker-e2e.yml b/.github/workflows/docker-e2e.yml index 57eea9d7..ed7cc982 100644 --- a/.github/workflows/docker-e2e.yml +++ b/.github/workflows/docker-e2e.yml @@ -23,10 +23,12 @@ jobs: password: ${{ secrets.GITHUB_TOKEN }} - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - - name: Build docker image + - name: Build docker images run: ./docker/build + run: ./docker/db-migrate/build - name: Push SHA tag run: ./docker/push + run: ./docker/db-migrate/push - name: Set environment variables for app run: scripts/ci/copy-env-vars-to-dot-env-file env: @@ -35,7 +37,10 @@ jobs: - name: Run service dependencies run: scripts/run-dependencies - name: Run app in docker - run: ./docker/run + run: ./docker/run + - name: log check + # TODO: remove this! + run: docker logs daedalus-web-app - name: Set up Node uses: actions/setup-node@v4 with: diff --git a/docker/common b/docker/common index 3f143471..b08f1625 100644 --- a/docker/common +++ b/docker/common @@ -1,5 +1,5 @@ #!/usr/bin/env bash -set -ex +set -euxo pipefail GIT_SHA=$(git rev-parse --short=7 HEAD) if [[ -v "BRANCH_NAME" ]]; then @@ -10,12 +10,19 @@ fi REGISTRY=ghcr.io ORG=jameel-institute +PREFIX="${REGISTRY}/${ORG}" + DB_IMAGE_NAME=daedalus-web-app-db -DB_TAG="${REGISTRY}/${ORG}/${DB_IMAGE_NAME}" +DB_TAG="${PREFIX}/${DB_IMAGE_NAME}" DB_TAG_SHA="${DB_TAG}:${GIT_SHA}" DB_TAG_BRANCH="${DB_TAG}:${GIT_BRANCH}" +DB_MIGRATE_IMAGE_NAME=daedalus-web-app-db-migrate +DB_MIGRATE_TAG="${PREFIX}/${DB_MIGRATE_IMAGE_NAME}" +DB_MIGRATE_TAG_SHA="${DB_MIGRATE_TAG}:${GIT_SHA}" +DB_MIGRATE_TAG_BRANCH="${DB_MIGRATE_TAG}:${GIT_BRANCH}" + APP_IMAGE_NAME=daedalus-web-app -APP_TAG="${REGISTRY}/${ORG}/${APP_IMAGE_NAME}" +APP_TAG="$PREFIX}/${APP_IMAGE_NAME}" APP_TAG_SHA="${APP_TAG}:${GIT_SHA}" APP_TAG_BRANCH="${APP_TAG}:${GIT_BRANCH}" diff --git a/docker/db-migrate/Dockerfile b/docker/db-migrate/Dockerfile new file mode 100644 index 00000000..6e866d54 --- /dev/null +++ b/docker/db-migrate/Dockerfile @@ -0,0 +1,7 @@ +FROM node:20-slim as base + +WORKDIR /src + +RUN npm ci + +CMD [ "prisma", "migrate", "deploy" ] \ No newline at end of file diff --git a/docker/db-migrate/build b/docker/db-migrate/build new file mode 100644 index 00000000..b522c900 --- /dev/null +++ b/docker/db-migrate/build @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +set -euxo pipefail +HERE=$(dirname $0) +. $HERE/../common + +docker build \ + --tag $DB_MIGRATE_TAG_SHA \ + -f docker/db-migrate/Dockerfile \ + . \ No newline at end of file diff --git a/docker/db-migrate/push b/docker/db-migrate/push new file mode 100644 index 00000000..2b67e7ea --- /dev/null +++ b/docker/db-migrate/push @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +set -euxo pipefail +HERE=$(dirname $0) +. $HERE/common + +docker push "$DB_MIGRATE_TAG_SHA" \ No newline at end of file diff --git a/docker/push-branch-tag b/docker/push-branch-tag index d0955db1..a698ffe2 100755 --- a/docker/push-branch-tag +++ b/docker/push-branch-tag @@ -1,7 +1,10 @@ #!/usr/bin/env bash -set -e +set -euxo pipefail HERE=$(dirname $0) . $HERE/common docker tag "$APP_TAG_SHA" "$APP_TAG_BRANCH" -docker push "$APP_TAG_BRANCH" \ No newline at end of file +docker push "$APP_TAG_BRANCH" + +docker tag "$DB_MIGRATE_TAG_SHA" "$DB_MIGRATE_TAG_BRANCH" +docker push "$DB_MIGRATE_TAG_BRANCH" \ No newline at end of file diff --git a/docker/run b/docker/run index 317e3e7e..4dd19010 100755 --- a/docker/run +++ b/docker/run @@ -2,16 +2,23 @@ set -euxo pipefail HERE=$(dirname $0) . $HERE/common -# run web app image, assuming dependencies are already running + +# Migrate the db - db container and other dependencies should already be running +docker run --rm -d \ + --name $DB_MIGRATE_IMAGE_NAME + --env DATABASE_URL=postgresql://daedalus-web-app-user:changeme@localhost:5432/daedalus-web-app + "$DB_MIGRATE_TAG_SHA" + docker run --rm -d \ - --network bridge \ --name "$APP_IMAGE_NAME" \ -p 3000:3000 \ + --env DATABASE_URL=postgresql://daedalus-web-app-user:changeme@localhost:5432/daedalus-web-app + --env NUXT_R_API_BASE=http://daedalus-api:8001/ "$APP_TAG_SHA" -# env vars - +# TODO: Need to have build both SHA images.. maybe roll build scripts together +# TODO: docker env file # TODO: WAIT? -# Migrate the db -docker exec "$APP_IMAGE_NAME" prisma migrate deploy + +# TODO: build and push db container in same pattern as app and migrate containers