This commit is contained in:
@@ -4,7 +4,7 @@ set -euo pipefail
|
||||
[[ "${DEBUG_DEPLOY:-0}" = "1" ]] && set -x
|
||||
trap 'echo "[deploy-db] error at line $LINENO" >&2' ERR
|
||||
|
||||
# Required environment variables (must be exported by the CI step)
|
||||
# Required env (exported by CI step from .env.runtime)
|
||||
: "${REMOTE_BASE:?missing REMOTE_BASE}"
|
||||
: "${DB_DIR:?missing DB_DIR}"
|
||||
: "${SSH_USER:?missing SSH_USER}"
|
||||
@@ -29,20 +29,35 @@ fi
|
||||
RSYNC_FLAGS=(-az --delete)
|
||||
[[ "${DEBUG_DEPLOY:-0}" = "1" ]] && RSYNC_FLAGS=(-avz --delete)
|
||||
|
||||
# Create remote directories and sync files
|
||||
# AppRole creds for Vault Agent, fetched in the previous pipeline step
|
||||
VAULT_ROLE_ID="$(cat secrets/ROLE_ID)"
|
||||
VAULT_SECRET_ID="$(cat secrets/SECRET_ID)"
|
||||
|
||||
# Create remote directory structure
|
||||
ssh "${SSH_OPTS[@]}" "$REMOTE_TARGET" "mkdir -p ${REMOTE_DIR}/{compose,env}"
|
||||
|
||||
# Sync compose bundle and runtime env to the remote host
|
||||
rsync "${RSYNC_FLAGS[@]}" -e "ssh ${SSH_OPTS[*]}" ci/prod/compose/ "$REMOTE_TARGET:${REMOTE_DIR}/compose/"
|
||||
rsync "${RSYNC_FLAGS[@]}" -e "ssh ${SSH_OPTS[*]}" ci/prod/.env.runtime "$REMOTE_TARGET:${REMOTE_DIR}/env/.env.runtime"
|
||||
|
||||
# Deploy on remote host (use 'bash -s' so heredoc is executed remotely)
|
||||
ssh "${SSH_OPTS[@]}" "$REMOTE_TARGET" REMOTE_DIR="$REMOTE_DIR" bash -s <<'EOSSH'
|
||||
# Deploy on remote: export AppRole creds ONLY to the compose commands.
|
||||
# The vault-agent container will read them, write to tmpfs, unset, and exec.
|
||||
ssh "${SSH_OPTS[@]}" "$REMOTE_TARGET" \
|
||||
REMOTE_DIR="$REMOTE_DIR" \
|
||||
VAULT_ROLE_ID="$VAULT_ROLE_ID" \
|
||||
VAULT_SECRET_ID="$VAULT_SECRET_ID" \
|
||||
bash -s <<'EOSSH'
|
||||
set -euo pipefail
|
||||
cd "${REMOTE_DIR}/compose"
|
||||
set -a; . ../env/.env.runtime; set +a
|
||||
# Pull quietly if supported; otherwise fall back to normal
|
||||
docker compose -f db.yml pull --quiet 2>/dev/null || docker compose -f db.yml pull
|
||||
docker compose -f db.yml up -d --remove-orphans
|
||||
docker compose ps
|
||||
|
||||
# Pull and start with ephemeral AppRole env present only for these invocations
|
||||
VAULT_ROLE_ID="${VAULT_ROLE_ID}" VAULT_SECRET_ID="${VAULT_SECRET_ID}" docker compose -f db.yml pull --quiet 2>/dev/null || \
|
||||
VAULT_ROLE_ID="${VAULT_ROLE_ID}" VAULT_SECRET_ID="${VAULT_SECRET_ID}" docker compose -f db.yml pull
|
||||
|
||||
VAULT_ROLE_ID="${VAULT_ROLE_ID}" VAULT_SECRET_ID="${VAULT_SECRET_ID}" docker compose -f db.yml up -d --remove-orphans
|
||||
|
||||
docker compose -f db.yml ps
|
||||
date -Is > .last_deploy
|
||||
logger -t deploy-db "db deployed at $(date -Is) in ${REMOTE_DIR}"
|
||||
EOSSH
|
||||
|
||||
Reference in New Issue
Block a user