Files
client160_IHMKOS/.gitea/workflows/promote-prod.yml
T

80 lines
2.7 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
name: promote-prod
on:
workflow_dispatch:
inputs:
sha:
description: "Commit SHA à promouvoir (vide = SHA du push)"
required: false
jobs:
promote:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with: { fetch-depth: 1 }
- name: Promote sur PROD (checkout + restart + healthcheck + rollback auto)
uses: appleboy/ssh-action@v1.0.3
env:
DEPLOY_PULL_TOKEN: ${{ secrets.DEPLOY_PULL_TOKEN }}
PROMOTE_SHA: ${{ inputs.sha != '' && inputs.sha || github.sha }}
SYSTEMD_SERVICE: ${{ vars.SYSTEMD_SERVICE }}
HEALTHCHECK_URL: ${{ vars.HEALTHCHECK_URL }}
with:
host: ${{ secrets.SSH_HOST }}
port: ${{ secrets.SSH_PORT }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_KEY }}
envs: DEPLOY_PULL_TOKEN,PROMOTE_SHA,SYSTEMD_SERVICE,HEALTHCHECK_URL
script: |
set -euo pipefail
repo=/srv/ihm/app/IHMKOS
svc="${SYSTEMD_SERVICE:-ihm-ephapoc}"
url="${HEALTHCHECK_URL:-http://127.0.0.1:15080/}"
# Préparer git
git config --global --add safe.directory "${repo}" || true
umask 077
cat > ~/.netrc <<EOF
machine ephagit.ephaitek.com
login moham
password ${DEPLOY_PULL_TOKEN}
EOF
# Sauvegarde commit courant pour rollback
cd "${repo}"
git rev-parse HEAD > .rollback_commit
# Se placer sur le commit demandé
git fetch origin main --depth=50
git checkout -f "${PROMOTE_SHA}"
git submodule update --init --recursive || true
# Redémarrer le service (qui lance docker-run.sh)
sudo -n systemctl daemon-reload || true
sudo -n systemctl restart "${svc}.service"
# Health-check (60s, 1s d’intervalle)
ok=0
for i in $(seq 1 60); do
code="$(curl -sk -o /dev/null -w '%{http_code}' "$url" || true)"
if [ "$code" = "200" ] || [ "$code" = "302" ] || [ "$code" = "301" ]; then
ok=1; break
fi
sleep 1
done
rm -f ~/.netrc
if [ "$ok" -ne 1 ]; then
echo "Health-check KO sur $url -> rollback automatique"
if [ -s .rollback_commit ]; then
prev="$(cat .rollback_commit)"
git checkout -f "$prev" || { echo "Rollback checkout échoué"; exit 1; }
sudo -n systemctl restart "${svc}.service" || true
fi
exit 1
fi
echo "Health-check OK sur $url"