86 lines
2.1 KiB
Bash
86 lines
2.1 KiB
Bash
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
# Usage: ./scripts/deploy-release.sh v0.0.1
|
|
# Requires: curl, tar, (optional) rsync
|
|
#
|
|
# Auth token: set RELEASE_TOKEN env var or create /etc/ferme-release-token
|
|
|
|
TAG="${1:-}"
|
|
if [ -z "$TAG" ]; then
|
|
echo "Usage: $0 v0.0.1" >&2
|
|
exit 1
|
|
fi
|
|
|
|
REPO_OWNER="MALIO-DEV"
|
|
REPO_NAME="Ferme"
|
|
GITEA_API="https://gitea.malio.fr/api/v1"
|
|
DEPLOY_DIR="/var/www/ferme"
|
|
|
|
if [ -f /etc/ferme-release-token ] && [ -z "${RELEASE_TOKEN:-}" ]; then
|
|
RELEASE_TOKEN="$(cat /etc/ferme-release-token)"
|
|
fi
|
|
|
|
tmp_dir="$(mktemp -d)"
|
|
cleanup() {
|
|
rm -rf "$tmp_dir"
|
|
}
|
|
trap cleanup EXIT
|
|
|
|
release_json="$tmp_dir/release.json"
|
|
curl_opts=(-sS)
|
|
if [ -n "${RELEASE_TOKEN:-}" ]; then
|
|
curl_opts+=(-H "Authorization: token ${RELEASE_TOKEN}")
|
|
fi
|
|
curl "${curl_opts[@]}" \
|
|
"${GITEA_API}/repos/${REPO_OWNER}/${REPO_NAME}/releases/tags/${TAG}" \
|
|
-o "$release_json"
|
|
|
|
asset_url="$(python3 - "$release_json" <<'PY'
|
|
import json, sys
|
|
data = json.load(open(sys.argv[1], 'r'))
|
|
assets = data.get("assets", [])
|
|
for a in assets:
|
|
name = a.get("name", "")
|
|
if name.startswith("ferme-") and name.endswith(".tar.gz"):
|
|
print(a.get("browser_download_url", ""))
|
|
break
|
|
PY
|
|
)"
|
|
|
|
if [ -z "$asset_url" ]; then
|
|
echo "Release asset not found for tag ${TAG}" >&2
|
|
exit 1
|
|
fi
|
|
|
|
archive="$tmp_dir/artefact.tar.gz"
|
|
curl "${curl_opts[@]}" -L "$asset_url" -o "$archive"
|
|
|
|
tar -xzf "$archive" -C "$tmp_dir"
|
|
|
|
if command -v rsync >/dev/null 2>&1; then
|
|
rsync -a --delete \
|
|
--exclude ".env" \
|
|
--exclude ".env.local" \
|
|
--exclude "config/jwt" \
|
|
--exclude "var" \
|
|
"$tmp_dir"/ "$DEPLOY_DIR"/
|
|
else
|
|
cp -a "$tmp_dir"/. "$DEPLOY_DIR"/
|
|
fi
|
|
|
|
echo "Release ${TAG} deployed to ${DEPLOY_DIR}"
|
|
|
|
if [ -n "${DEPLOY_OWNER:-}" ]; then
|
|
DEPLOY_GROUP="${DEPLOY_GROUP:-www-data}"
|
|
chown -R "${DEPLOY_OWNER}:${DEPLOY_GROUP}" "$DEPLOY_DIR"
|
|
chmod -R g+rx,o+rx "$DEPLOY_DIR"
|
|
fi
|
|
|
|
if [ -f "${DEPLOY_DIR}/.env.local" ]; then
|
|
echo "Running migrations (if any)..."
|
|
php "${DEPLOY_DIR}/bin/console" doctrine:migrations:migrate --no-interaction --env=prod
|
|
else
|
|
echo "Skip migrations: ${DEPLOY_DIR}/.env.local not found" >&2
|
|
fi
|