From 3b1ab1bb1a0b75242ff0a661b293d5622f06ff2b Mon Sep 17 00:00:00 2001 From: minottic Date: Thu, 21 Nov 2024 12:08:31 +0100 Subject: [PATCH] Run entrypoint in DEV only on first creation --- README.md | 2 ++ entrypoints/add_chrome.sh | 2 ++ entrypoints/infinite_loop.sh | 2 ++ entrypoints/merge_json.sh | 2 ++ entrypoints/npm_ci.sh | 2 ++ entrypoints/setup_git.sh | 31 ++++++++----------- .../services/v3/entrypoints/merge_json.sh | 2 ++ .../services/v4/entrypoints/db_migration.sh | 2 ++ 8 files changed, 27 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 5c9dffa9..aae5f0ef 100644 --- a/README.md +++ b/README.md @@ -175,6 +175,8 @@ docker compose -f compose.yaml -f .github/compose.dev.test.yaml ... It is very convenient if using [VSCode](https://code.visualstudio.com/docs/devcontainers/attach-container), as, after the docker services are running, one can attach to it and start developing using all VSCode features, including version control and debugging. +Please note that [entrypoints](#entrypoints) when `DEV=true` are only run when the component's container is created for the first time. This is done to avoid clashes with local changes. + To ease writing DEV configuration, a dev template is provided [here](./services/compose.dev.yaml) and each component inhearits from it, as you can see [here](./services/frontend/compose.dev.yaml) setting the componenent specific variables from the relative [.env file](./services/frontend/.env). :warning: Docker compose applies a [precedence mechanism](https://docs.docker.com/compose/how-tos/environment-variables/variable-interpolation/#local-env-file-versus-project-directory-env-file) whenever the same variable is defined in `.env` files in nested folders, with precedence to the folder where the default `COMPOSE_FILE` lives. This means that the current template cannot be used in case of nested components, at least for the parts where local variables are used. There is no conflict with variables defined multiple times in `.env` files at the same level. :warning: To prevent git unpushed changes from being lost when a container is restarted, the work folder of each service, when in DEV mode, is mounted to a docker volume, with naming convention `${COMPOSE_PROJECT_NAME}__dev`. Make sure, to commit and push frequently, especially before removing docker volumes to push the relevant changes. diff --git a/entrypoints/add_chrome.sh b/entrypoints/add_chrome.sh index 94a08dff..1d6d32a6 100644 --- a/entrypoints/add_chrome.sh +++ b/entrypoints/add_chrome.sh @@ -1,3 +1,5 @@ #!/bin/sh +[ -e ".finished" ] && return 0 + apk update && apk add chromium diff --git a/entrypoints/infinite_loop.sh b/entrypoints/infinite_loop.sh index b137461f..9f65a3b6 100755 --- a/entrypoints/infinite_loop.sh +++ b/entrypoints/infinite_loop.sh @@ -1,3 +1,5 @@ #!/bin/sh +touch .finished + while true; do sleep 600; done diff --git a/entrypoints/merge_json.sh b/entrypoints/merge_json.sh index cf284a62..61e8313f 100755 --- a/entrypoints/merge_json.sh +++ b/entrypoints/merge_json.sh @@ -1,5 +1,7 @@ #!/bin/sh +[ -e ".finished" ] && return 0 + apk update && apk add jq gettext jq -s 'reduce .[] as $item ({}; . * $item)' /config/*.json | envsubst \ diff --git a/entrypoints/npm_ci.sh b/entrypoints/npm_ci.sh index ca6f97b6..251b8c31 100644 --- a/entrypoints/npm_ci.sh +++ b/entrypoints/npm_ci.sh @@ -1,3 +1,5 @@ #!/bin/sh +[ -e ".finished" ] && return 0 + npm ci diff --git a/entrypoints/setup_git.sh b/entrypoints/setup_git.sh index f5d4985c..6bd0494e 100755 --- a/entrypoints/setup_git.sh +++ b/entrypoints/setup_git.sh @@ -1,24 +1,19 @@ #!/bin/sh -apk update && apk add git - -git remote || INIT=true +[ -e ".finished" ] && return 0 +apk update && apk add git echo "${GITHUB_REPO}" | grep -q "#" && TAG="${GITHUB_REPO#*#}" || TAG= - -if [ "${INIT}" ] +git init +chown -R "$(find . -maxdepth 1 -exec ls -ld {} + | awk '{print $3":"$4}' | tail -n1)" .git +git config --global --add safe.directory "${PWD}" +REPO="${GITHUB_REPO%%#*}" +git remote add origin "${REPO}" +git fetch +DEFAULT_BRANCH=$(git remote show origin | sed -n '/HEAD branch/s/.*: //p') +git reset --hard origin/"${DEFAULT_BRANCH}" +git clean -fd +if [ -n "${TAG}" ] then - git init - chown -R "$(find . -maxdepth 1 -exec ls -ld {} + | awk '{print $3":"$4}' | tail -n1)" .git - git config --global --add safe.directory "${PWD}" - REPO="${GITHUB_REPO%%#*}" - git remote add origin "${REPO}" - git fetch - DEFAULT_BRANCH=$(git remote show origin | sed -n '/HEAD branch/s/.*: //p') - git reset --hard origin/"${DEFAULT_BRANCH}" - git clean -fd - if [ -n "${TAG}" ] - then - git checkout "${TAG}" - fi + git checkout "${TAG}" fi diff --git a/services/backend/services/v3/entrypoints/merge_json.sh b/services/backend/services/v3/entrypoints/merge_json.sh index e146bf84..d648cbdf 100755 --- a/services/backend/services/v3/entrypoints/merge_json.sh +++ b/services/backend/services/v3/entrypoints/merge_json.sh @@ -1,5 +1,7 @@ #!/bin/sh +[ -e ".finished" ] && return 0 + for config in $(find /config -maxdepth 1 -type f -exec basename {} \; | cut -d '.' -f 1 | sort -u) do # shellcheck disable=SC2016 diff --git a/services/backend/services/v4/entrypoints/db_migration.sh b/services/backend/services/v4/entrypoints/db_migration.sh index 5182ec80..07912233 100755 --- a/services/backend/services/v4/entrypoints/db_migration.sh +++ b/services/backend/services/v4/entrypoints/db_migration.sh @@ -1,5 +1,7 @@ #!/bin/sh +[ -e ".finished" ] && return 0 + echo "MONGODB_URI=$MONGODB_URI" >.env npm run migrate:db:up rm .env