fix: record the version in .env before starting the containers
CI / check (pull_request) Successful in 19s
CI / check (pull_request) Successful in 19s
The services read ~/netork/.env through env_file, so whatever it says when `up -d` creates a container ends up in that container's environment. The settings were recorded only after the swap, so every container carried the previous deploy's NETORK_VERSION: the image was right, `docker exec <api> env` was not. Record them once the pull has brought the images onto the host and before the first `up -d`. A failed pull still leaves .env alone. If the swap fails afterwards, .env names the tag whose compose files and images are already on the host, which is what a hand-typed `docker compose up -d` there should start. Also say which branches a feature-<branch> tag comes from: feature/<name> and feat/<name> become feature-<name>, and fix/<name> now becomes feature-fix-<name> (NetOrk/netork#208), because the registry keeps only feature-* branch tags. The accepted forms do not change; the comment, the refusal message and the README now say so, and a test holds that feature-fix-<name> passes and a bare fix-<name> does not. Closes #6
This commit is contained in:
1 parent
a7e3617291
commit
3a15c311a0
5 files changed
+269
-57
No files matched your search
@@ -208,15 +208,18 @@ fi
|
||||
# silently, with the only symptom surfacing later at `alembic upgrade head`,
|
||||
# by which point containers are already switched over. Reject anything that
|
||||
# doesn't match a tag format netOrk's CI actually publishes: latest,
|
||||
# latest-dev, main-<sha>, feature-<branch> (feature/* only), or a release
|
||||
# version (X.Y.Z). See NetOrk/netork#55.
|
||||
# latest-dev, main-<sha>, feature-<branch>, or a release version (X.Y.Z).
|
||||
# Every branch build is a feature-<branch> tag: feature/<name> and feat/<name>
|
||||
# become feature-<name>, fix/<name> becomes feature-fix-<name>. The registry
|
||||
# keeps feature-* tags for 14 days; a tag of any other form would be deleted
|
||||
# within a day. See NetOrk/netork#55 and NetOrk/netork#208.
|
||||
VALID_VERSION_RE='^(latest|latest-dev|main-[0-9a-f]{7,40}|feature-[A-Za-z0-9._-]+|[0-9]+\.[0-9]+\.[0-9]+)$'
|
||||
|
||||
_validate_version() {
|
||||
local version="$1"
|
||||
if [[ ! "$version" =~ $VALID_VERSION_RE ]]; then
|
||||
echo "Refusing to deploy version '${version}': not a tag CI publishes (expected latest," \
|
||||
"latest-dev, main-<sha>, feature-<branch>, or X.Y.Z)." >&2
|
||||
"latest-dev, main-<sha>, feature-<branch> (also for fix/* branches), or X.Y.Z)." >&2
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
@@ -313,6 +316,42 @@ deploy_steps() {
|
||||
REGISTRY_HOST='${REGISTRY_HOST}' NETORK_VERSION='${SERVER_VERSION}' \
|
||||
docker compose -f docker-compose.yml -f docker-compose.registry.yml pull ${SERVICES}"
|
||||
|
||||
# docker compose substitutes REGISTRY_HOST / NETORK_VERSION from the project
|
||||
# .env when they are not in the environment. This script passes both inline,
|
||||
# but a hand-typed `docker compose up -d flower` on the host needs them there
|
||||
# or dies on "invalid reference format". Values are rewritten in place, so
|
||||
# the file does not grow a new line per deploy.
|
||||
#
|
||||
# Not earlier: this used to run before the pull, so a deploy that failed on a
|
||||
# tag the registry does not have still left the host recording that tag, and
|
||||
# the next `docker compose up` a human ran there reached for an image that
|
||||
# does not exist. Once the pull went through, the images are on the host.
|
||||
#
|
||||
# Not later: the services also read this file (`env_file: .env`), so it is in
|
||||
# every container's environment. Written after the swap, as it was for a
|
||||
# while, it made each container carry the previous deploy's NETORK_VERSION
|
||||
# (NetOrk/deploy#6). Should the swap below fail, the file names the tag whose
|
||||
# compose files and images are already on the host, which is what a
|
||||
# hand-typed `docker compose up -d` there should start.
|
||||
echo "[${SERVER}] Recording registry settings in ~/netork/.env..."
|
||||
# shellcheck disable=SC2087 # registry and version expand here; \$ escapes run remotely
|
||||
ssh "$SERVER" "bash -s" <<REMOTE_ENV
|
||||
set -eu
|
||||
cd ~/netork
|
||||
touch .env
|
||||
# Append to a file that does not end in a newline, and the last line grows a
|
||||
# suffix instead of gaining a neighbour.
|
||||
[ ! -s .env ] || [ -z "\$(tail -c1 .env)" ] || printf '\n' >> .env
|
||||
for kv in "REGISTRY_HOST=${REGISTRY_HOST}" "NETORK_VERSION=${SERVER_VERSION}"; do
|
||||
key=\${kv%%=*}
|
||||
if grep -q "^\${key}=" .env; then
|
||||
sed -i "s|^\${key}=.*|\${kv}|" .env
|
||||
else
|
||||
printf '%s\n' "\${kv}" >> .env
|
||||
fi
|
||||
done
|
||||
REMOTE_ENV
|
||||
|
||||
# --force-recreate is deliberate: `docker compose up -d` decides what to
|
||||
# replace from the service definition's config hash, and the image reference
|
||||
# (`.../ui:latest-dev`) does not change when the tag is moved to a new image.
|
||||
@@ -417,36 +456,6 @@ deploy_steps() {
|
||||
fi
|
||||
echo "[${SERVER}] Verified: containers run ${SERVER_VERSION}."
|
||||
|
||||
# docker compose substitutes REGISTRY_HOST / NETORK_VERSION from the project
|
||||
# .env when they are not in the environment. This script passes both inline,
|
||||
# but a hand-typed `docker compose up -d flower` on the host needs them there
|
||||
# or dies on "invalid reference format". Values are rewritten in place, so
|
||||
# the file does not grow a new line per deploy.
|
||||
#
|
||||
# Only now. This used to run before the pull, so a deploy that failed on a tag
|
||||
# the registry does not have still left the host recording that tag — and the
|
||||
# next `docker compose up` a human ran there reached for an image that does
|
||||
# not exist. The file is a record of what this server runs, so it is written
|
||||
# once that is true: after the swap and after the image-id check agreed.
|
||||
echo "[${SERVER}] Recording registry settings in ~/netork/.env..."
|
||||
# shellcheck disable=SC2087 # registry and version expand here; \$ escapes run remotely
|
||||
ssh "$SERVER" "bash -s" <<REMOTE_ENV
|
||||
set -eu
|
||||
cd ~/netork
|
||||
touch .env
|
||||
# Append to a file that does not end in a newline, and the last line grows a
|
||||
# suffix instead of gaining a neighbour.
|
||||
[ ! -s .env ] || [ -z "\$(tail -c1 .env)" ] || printf '\n' >> .env
|
||||
for kv in "REGISTRY_HOST=${REGISTRY_HOST}" "NETORK_VERSION=${SERVER_VERSION}"; do
|
||||
key=\${kv%%=*}
|
||||
if grep -q "^\${key}=" .env; then
|
||||
sed -i "s|^\${key}=.*|\${kv}|" .env
|
||||
else
|
||||
printf '%s\n' "\${kv}" >> .env
|
||||
fi
|
||||
done
|
||||
REMOTE_ENV
|
||||
|
||||
# netOrk migrates *after* the swap — its API tolerates the gap. What it does
|
||||
# not tolerate is the migration failing unnoticed: the new code is already
|
||||
# serving, so a missing column is a 500 on every request that touches it
|
||||
|
||||
Reference in new issue
Block a user