ansible/roles/app_setup/templates/deploy_app.sh.j2
ilia c2e797a027
All checks were successful
CI / skip-ci-check (pull_request) Successful in 1m22s
CI / lint-and-test (pull_request) Successful in 1m27s
CI / ansible-validation (pull_request) Successful in 2m53s
CI / secret-scanning (pull_request) Successful in 1m24s
CI / dependency-scan (pull_request) Successful in 1m28s
CI / sast-scan (pull_request) Successful in 2m32s
CI / license-check (pull_request) Successful in 1m28s
CI / vault-check (pull_request) Successful in 2m30s
CI / playbook-test (pull_request) Successful in 2m32s
CI / container-scan (pull_request) Successful in 1m53s
CI / sonar-analysis (pull_request) Successful in 2m40s
CI / workflow-summary (pull_request) Successful in 1m22s
feat(app_setup): improve deployment reliability and add mirrormatch support
- Fix deploy script to handle non-git directories by cloning to temp
  location and moving contents, preserving .env files during clone
- Remove comment lines from env.j2 template to prevent xargs errors
- Add initial deploy task to app_setup role to ensure app is deployed
  before service starts
- Fix migrate command precedence to check env-specific overrides first
- Add sudo to systemctl restart commands in deploy script
- Update documentation with project-specific configuration notes

These changes improve deployment reliability for all app projects while
adding support for mirrormatch-specific requirements (db:push, seeding).
All changes are backward-compatible with existing projects (pote, punimTag).
2026-01-04 16:50:54 -05:00

101 lines
2.6 KiB
Django/Jinja

#!/usr/bin/env bash
# Ansible-managed deploy script
set -euo pipefail
REPO_URL="{{ app_repo_url }}"
BRANCH="{{ app_repo_branch }}"
APP_ROOT="{{ app_repo_dest }}"
BACKEND_DIR="{{ app_backend_dir }}"
FRONTEND_DIR="{{ app_frontend_dir }}"
ENV_FILE="{{ app_root }}/.env.{{ app_env }}"
echo "[deploy] repo=${REPO_URL} branch=${BRANCH} root=${APP_ROOT}"
# Load env for build/migrate steps (needed for Prisma/Next build)
if [[ -f "${ENV_FILE}" ]]; then
set -a
# shellcheck disable=SC1090
source "${ENV_FILE}"
set +a
fi
if [[ ! -d "${APP_ROOT}/.git" ]]; then
echo "[deploy] cloning repo"
# Preserve existing env files
env_tmp="$(mktemp -d)"
shopt -s nullglob dotglob
for f in "${APP_ROOT}"/.env.*; do
[[ -f "$f" ]] && cp "$f" "${env_tmp}/" || true
done
shopt -u nullglob dotglob
# Clone to temp location
clone_tmp="$(mktemp -d)"
git clone --branch "${BRANCH}" --single-branch "${REPO_URL}" "${clone_tmp}/repo"
# Clean app root (keep directory and .env files)
find "${APP_ROOT}" -mindepth 1 -maxdepth 1 ! -name '.env.*' -exec rm -rf {} + 2>/dev/null || true
# Move cloned repo contents to app root (including hidden files)
shopt -s dotglob
mv "${clone_tmp}/repo"/* "${APP_ROOT}"/ 2>/dev/null || true
shopt -u dotglob
rm -rf "${clone_tmp}"
# Restore env files
shopt -s nullglob
for f in "${env_tmp}"/.env.*; do
[[ -f "$f" ]] && cp "$f" "${APP_ROOT}/" || true
done
shopt -u nullglob
rm -rf "${env_tmp}"
fi
echo "[deploy] syncing branch"
git -C "${APP_ROOT}" fetch origin --prune
if ! git -C "${APP_ROOT}" rev-parse --verify --quiet "refs/remotes/origin/${BRANCH}" >/dev/null; then
echo "[deploy] ERROR: branch '${BRANCH}' not found on origin"
exit 2
fi
git -C "${APP_ROOT}" checkout -B "${BRANCH}" "origin/${BRANCH}"
git -C "${APP_ROOT}" pull --ff-only origin "${BRANCH}"
if [[ "{{ app_enable_backend | bool }}" == "True" ]]; then
echo "[deploy] backend install"
cd "${BACKEND_DIR}"
{{ app_backend_install_cmd }}
echo "[deploy] backend build"
{{ app_backend_build_cmd }}
echo "[deploy] backend migrations"
{{ app_backend_migrate_cmd }}
{% if app_backend_seed_cmd | default('') | length > 0 %}
echo "[deploy] backend seed"
{{ app_backend_seed_cmd }}
{% endif %}
fi
if [[ "{{ app_enable_frontend | bool }}" == "True" ]]; then
echo "[deploy] frontend install"
cd "${FRONTEND_DIR}"
{{ app_frontend_install_cmd }}
echo "[deploy] frontend build"
{{ app_frontend_build_cmd }}
fi
echo "[deploy] restarting services"
{% if app_enable_backend | bool %}
sudo systemctl restart app-backend.service
{% endif %}
{% if app_enable_frontend | bool %}
sudo systemctl restart app-frontend.service
{% endif %}
echo "[deploy] done"