49 lines
3.1 KiB
Bash
49 lines
3.1 KiB
Bash
#!/usr/bin/env bash
|
|
# CI job side only. Never execute host deploy logic in the runner filesystem.
|
|
set -euo pipefail
|
|
set +x
|
|
fail() { printf 'ERROR: %s\n' "$1" >&2; exit 2; }
|
|
[[ ${GITHUB_ACTIONS:-${GITEA_ACTIONS:-}} == true && ( ${BRANCH:-} == develop || ${BRANCH:-} == main ) && $# == 0 ]] || fail 'CI-only immutable deployment'
|
|
[[ ${DEPLOY_SHA:-} =~ ^[0-9a-f]{40}$ ]] || fail 'immutable DEPLOY_SHA required'
|
|
# Repository secret DEPLOY_KEY is the historical approved devops SSH credential.
|
|
# Never fall back to baked-in runner keys, agent forwarding or docker.sock.
|
|
[[ -n ${DEPLOY_KNOWN_HOSTS:-} ]] || fail 'provision DEPLOY_KNOWN_HOSTS: independently verified [87.248.133.138]:2234 host key; never use ssh-keyscan trust-on-first-use'
|
|
[[ -n ${DEPLOY_KEY:-} ]] || fail 'provision existing approved DEPLOY_KEY for devops@87.248.133.138:2234'
|
|
for tool in ssh ssh-keygen mktemp; do command -v "$tool" >/dev/null || fail "job context missing $tool"; done
|
|
umask 077
|
|
scratch=$(mktemp -d)
|
|
trap 'rm -rf "$scratch"' EXIT
|
|
printf '%s\n' "$DEPLOY_KNOWN_HOSTS" > "$scratch/known_hosts"
|
|
printf '%s\n' "$DEPLOY_KEY" > "$scratch/key"
|
|
unset DEPLOY_KEY DEPLOY_KNOWN_HOSTS
|
|
ssh-keygen -F '[87.248.133.138]:2234' -f "$scratch/known_hosts" >/dev/null || fail 'DEPLOY_KNOWN_HOSTS has no pinned deployment endpoint'
|
|
# Only the validated SHA enters the remote command; no user-controlled shell text.
|
|
ssh -F /dev/null -T -p 2234 -i "$scratch/key" \
|
|
-o BatchMode=yes -o IdentitiesOnly=yes -o IdentityAgent=none \
|
|
-o StrictHostKeyChecking=yes -o UserKnownHostsFile="$scratch/known_hosts" \
|
|
-o GlobalKnownHostsFile=/dev/null -o ConnectTimeout=15 \
|
|
devops@87.248.133.138 "sudo -n bash -s -- $DEPLOY_SHA $BRANCH" <<'HOST'
|
|
set -euo pipefail
|
|
set +x
|
|
umask 077
|
|
fail() { printf 'ERROR: host context: %s\n' "$1" >&2; exit 2; }
|
|
[[ $# == 2 && $1 =~ ^[0-9a-f]{40}$ && ( $2 == develop || $2 == main ) ]] || fail 'immutable SHA and approved branch required'
|
|
[[ ! -e /.dockerenv && ! -e /run/.containerenv ]] || fail 'refusing container filesystem'
|
|
[[ $(id -u) == 0 ]] || fail 'noninteractive approved sudo required'
|
|
for tool in bash python3 docker git flock realpath install wget; do command -v "$tool" >/dev/null || fail "missing $tool"; done
|
|
[[ -d /opt/services/canina/stage/.git && -d /opt/services/proxy/certs/canina ]] || fail 'Stage operational checkout/certificates unavailable'
|
|
[[ -s /opt/services/canina/stage/.support-chat.env && ! -L /opt/services/canina/stage/.support-chat.env ]] || fail 'Stage runtime env unavailable'
|
|
docker compose version >/dev/null
|
|
docker volume inspect stage_canina_stage_db stage_canina_stage_uploads >/dev/null
|
|
docker network inspect traefik_public >/dev/null
|
|
if [[ $2 == main ]]; then
|
|
[[ -s /opt/services/canina/production-acceptance/$1.json ]] || fail 'audited Stage acceptance receipt missing'
|
|
fi
|
|
printf 'Host context validated; branch=%s commit=%s\n' "$2" "$1"
|
|
script=$(mktemp)
|
|
trap 'rm -f "$script"' EXIT
|
|
wget --no-proxy -qO "$script" "https://git.parsaaghayi.ir/parsa/canina/raw/commit/${1}/scripts/deploy.sh"
|
|
# CI marker exists only across this authenticated, fixed host boundary.
|
|
GITHUB_ACTIONS=true STAGE_PROVISION_ENABLED=true DEPLOY_SHA="$1" bash "$script" "$2"
|
|
HOST
|