ia_dev/deploy/run-project-hooks.sh
Nicolas Cantu 418bfb044a evol(deploy): shared ssh/log libs, SECRETS_BASE export, lecoffreio secrets path
**Motivations:**
- Single source for ssh helpers; export secrets base from conf; centralize lecoffre secrets under ia_dev.

**Evolutions:**
- deploy/lib/ssh.sh, deploy-log.sh; run-project-hooks + deploy-by-script-to export SECRETS_BASE from secrets_path; lecoffreio conf secrets_path ia_dev; deploy/lib README.

**Pages affectées:**
- deploy/lib/*, deploy/run-project-hooks.sh, deploy/deploy-by-script-to.sh, projects/lecoffreio/conf.json
2026-03-23 12:50:31 +01:00

72 lines
2.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# Run deploy.hooks.phases from projects/<id>/conf.json (paths relative to repository_root).
# If phases is empty or missing, exec deploy.deploy_script_path with the same arguments.
# Usage: run-project-hooks.sh <env> [options passed to each phase / fallback script]
# Requires: IA_PROJECT_ID, IA_DEV_ROOT (or re-exec from project root like change-to-all-branches).
set -euo pipefail
SCRIPT_REAL="$(readlink -f "${BASH_SOURCE[0]:-$0}" 2>/dev/null || realpath "${BASH_SOURCE[0]:-$0}" 2>/dev/null || echo "${BASH_SOURCE[0]:-$0}")"
DEPLOY_DIR="$(cd "$(dirname "$SCRIPT_REAL")" && pwd)"
IA_DEV_ROOT="$(cd "$DEPLOY_DIR/.." && pwd)"
if [[ -z "${IA_PROJECT_ID:-}" ]]; then
echo "[run-project-hooks][ERROR] IA_PROJECT_ID is not set" >&2
exit 1
fi
# shellcheck source=../lib/project_config.sh
source "${IA_DEV_ROOT}/lib/project_config.sh"
# shellcheck source=../lib/project_git_root_from_conf.sh
source "${IA_DEV_ROOT}/lib/project_git_root_from_conf.sh"
ia_dev_resolve_project_git_root
REPO_ROOT="${IA_PROJECT_GIT_ROOT:-}"
if [[ -z "$REPO_ROOT" || ! -d "$REPO_ROOT" ]]; then
echo "[run-project-hooks][ERROR] Could not resolve repository root for project ${IA_PROJECT_ID}" >&2
exit 1
fi
CONF="${PROJECT_CONFIG_PATH:-}"
if [[ -z "$CONF" || ! -f "$CONF" ]]; then
echo "[run-project-hooks][ERROR] Missing conf: ${CONF:-}" >&2
exit 1
fi
if ! command -v jq >/dev/null 2>&1; then
echo "[run-project-hooks][ERROR] jq is required to read deploy.hooks.phases" >&2
exit 1
fi
SECRETS_PATH_CFG="$(jq -r '.deploy.secrets_path // empty' "$CONF")"
if [[ -n "$SECRETS_PATH_CFG" && "$SECRETS_PATH_CFG" != "null" && -d "$SECRETS_PATH_CFG" ]]; then
export SECRETS_BASE="$SECRETS_PATH_CFG"
export LECOFFRE_SECRETS_BASE="$SECRETS_PATH_CFG"
fi
DEPLOY_SCRIPT_PATH="$(jq -r '.deploy.deploy_script_path // empty' "$CONF")"
if [[ -z "$DEPLOY_SCRIPT_PATH" || ! -f "$DEPLOY_SCRIPT_PATH" ]]; then
echo "[run-project-hooks][ERROR] deploy.deploy_script_path missing or not a file: ${DEPLOY_SCRIPT_PATH:-}" >&2
exit 1
fi
PHASE_COUNT="$(jq '.deploy.hooks.phases // [] | length' "$CONF")"
if [[ "$PHASE_COUNT" == "0" ]]; then
exec bash "$DEPLOY_SCRIPT_PATH" "$@"
fi
mapfile -t PHASE_SCRIPTS < <(jq -r '.deploy.hooks.phases[]? | if type == "string" then . elif type == "object" and (.run | type == "string") then .run else empty end' "$CONF")
if [[ ${#PHASE_SCRIPTS[@]} -eq 0 ]]; then
exec bash "$DEPLOY_SCRIPT_PATH" "$@"
fi
for rel in "${PHASE_SCRIPTS[@]}"; do
[[ -z "$rel" ]] && continue
phase_path="${REPO_ROOT}/${rel}"
if [[ ! -f "$phase_path" ]]; then
echo "[run-project-hooks][ERROR] Phase script not found: ${phase_path}" >&2
exit 1
fi
echo "[run-project-hooks] Running: ${rel} $*"
bash "$phase_path" "$@"
done