Actualiser .gitea/workflows/promote-prod.yml
This commit is contained in:
1 parent
1327913199
commit
9d0edf398e
1 file changed
+39
-85
@@ -3,113 +3,67 @@ name: promote-prod
|
|||||||
on:
|
on:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
inputs:
|
inputs:
|
||||||
sha:
|
tag:
|
||||||
description: "Commit SHA à promouvoir (vide = SHA du push)"
|
description: "Tag d'image à promouvoir (ex: canary-<sha> ou latest-canary)"
|
||||||
required: false
|
required: true
|
||||||
|
default: "latest-canary"
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
promote:
|
promote:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
# 1) Forcer la résolution vers l’IP LAN
|
|
||||||
- name: Force LAN host mapping (ephagit & srv)
|
- name: Force LAN host mapping (ephagit & srv)
|
||||||
run: |
|
run: |
|
||||||
echo "10.90.0.10 ephagit.ephaitek.com" | sudo tee -a /etc/hosts
|
echo "10.90.0.10 ephagit.ephaitek.com" | sudo tee -a /etc/hosts
|
||||||
echo "10.10.0.10 srv.ephaitek.com" | sudo tee -a /etc/hosts
|
echo "10.10.0.10 srv.ephaitek.com" | sudo tee -a /etc/hosts
|
||||||
getent hosts ephagit.ephaitek.com
|
|
||||||
getent hosts srv.ephaitek.com
|
|
||||||
|
|
||||||
# 2) Debug réseau non bloquant
|
- name: Promote on PROD (switch image + restart + healthcheck + rollback)
|
||||||
- name: Debug réseau/Git (non bloquant)
|
|
||||||
shell: bash
|
|
||||||
continue-on-error: true
|
|
||||||
run: |
|
|
||||||
set -xeuo pipefail
|
|
||||||
git --version
|
|
||||||
getent hosts ephagit.ephaitek.com
|
|
||||||
getent hosts srv.ephaitek.com
|
|
||||||
curl -sI https://ephagit.ephaitek.com/ | head -n1 || true
|
|
||||||
|
|
||||||
# 3) Rendre Git “impatient”
|
|
||||||
- name: Tweaks Git (fail fast)
|
|
||||||
run: |
|
|
||||||
git config --global http.lowSpeedLimit 1000
|
|
||||||
git config --global http.lowSpeedTime 5
|
|
||||||
|
|
||||||
# 4) Checkout rapide
|
|
||||||
- name: actions/checkout (shallow)
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
timeout-minutes: 2
|
|
||||||
with:
|
|
||||||
fetch-depth: 1
|
|
||||||
persist-credentials: false
|
|
||||||
# Si besoin, passe ton PAT pour lever tout doute d’auth :
|
|
||||||
token: ${{ secrets.CHECKOUT_TOKEN }}
|
|
||||||
|
|
||||||
# 5) Promote prod: checkout commit + restart + healthcheck + rollback
|
|
||||||
- name: Promote sur PROD (checkout + restart + healthcheck + rollback auto)
|
|
||||||
uses: appleboy/ssh-action@v1.0.3
|
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:
|
with:
|
||||||
host: ${{ secrets.SSH_HOST }}
|
host: ${{ secrets.SSH_HOST }}
|
||||||
port: ${{ secrets.SSH_PORT }}
|
port: ${{ secrets.SSH_PORT }}
|
||||||
username: ${{ secrets.SSH_USER }}
|
username: ${{ secrets.SSH_USER }}
|
||||||
key: ${{ secrets.SSH_KEY }}
|
key: ${{ secrets.SSH_KEY }}
|
||||||
envs: DEPLOY_PULL_TOKEN,PROMOTE_SHA,SYSTEMD_SERVICE,HEALTHCHECK_URL
|
envs: REGISTRY,IMAGE_REPO
|
||||||
script: |
|
script: |
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
repo=/srv/ihm/app/IHMKOS
|
TAG="${{ github.event.inputs.tag }}"
|
||||||
svc="${SYSTEMD_SERVICE:-ihm-ephapoc}"
|
NEW_IMAGE="${REGISTRY}/${IMAGE_REPO}:${TAG}"
|
||||||
url="${HEALTHCHECK_URL:-http://127.0.0.1:15080/}"
|
ENV_PROD="/srv/ihm/.env"
|
||||||
|
|
||||||
# Préparer git (dépôt local sur srv)
|
# sauvegarder ancien IMAGE
|
||||||
git config --global --add safe.directory "${repo}" || true
|
OLD_IMAGE="$(grep -E '^IMAGE=' "${ENV_PROD}" | sed -E 's/^IMAGE=//')"
|
||||||
|
echo "Ancienne image: ${OLD_IMAGE}"
|
||||||
|
echo "${OLD_IMAGE}" > /srv/ihm/app/.rollback_image
|
||||||
|
|
||||||
# Auth HTTPS non interactive pour le pull si besoin
|
# switch image
|
||||||
umask 077
|
if grep -q '^IMAGE=' "${ENV_PROD}"; then
|
||||||
cat > ~/.netrc <<EOF
|
sed -i -E "s|^IMAGE=.*|IMAGE=${NEW_IMAGE}|" "${ENV_PROD}"
|
||||||
machine ephagit.ephaitek.com
|
else
|
||||||
login moham
|
echo "IMAGE=${NEW_IMAGE}" >> "${ENV_PROD}"
|
||||||
password ${DEPLOY_PULL_TOKEN}
|
fi
|
||||||
EOF
|
echo "Nouvelle image: ${NEW_IMAGE}"
|
||||||
|
|
||||||
cd "${repo}"
|
# restart service prod
|
||||||
# Sauvegarde commit courant pour rollback
|
sudo systemctl restart ihm-ephapoc.service
|
||||||
git rev-parse HEAD > .rollback_commit
|
|
||||||
|
|
||||||
# Se placer sur le commit demandé
|
# petit délai et healthcheck
|
||||||
git fetch origin main --depth=50
|
sleep 5
|
||||||
git checkout -f "${PROMOTE_SHA}"
|
if ! curl -sf http://127.0.0.1:${HTTP_PORT:-15080}/ >/dev/null; then
|
||||||
git submodule update --init --recursive || true
|
echo "❌ Healthcheck PROD KO, rollback…"
|
||||||
|
if [ -s /srv/ihm/app/.rollback_image ]; then
|
||||||
# Redémarrer le service (appelle docker-run.sh)
|
RB_IMG="$(cat /srv/ihm/app/.rollback_image)"
|
||||||
sudo -n systemctl daemon-reload || true
|
sed -i -E "s|^IMAGE=.*|IMAGE=${RB_IMG}|" "${ENV_PROD}"
|
||||||
sudo -n systemctl restart "${svc}.service"
|
sudo systemctl restart ihm-ephapoc.service
|
||||||
|
sleep 5
|
||||||
# Health-check (60s)
|
curl -sf http://127.0.0.1:${HTTP_PORT:-15080}/ >/dev/null
|
||||||
ok=0
|
echo "✅ Rollback OK (${RB_IMG})"
|
||||||
for i in $(seq 1 60); do
|
exit 1
|
||||||
code="$(curl -sk -o /dev/null -w '%{http_code}' "$url" || true)"
|
else
|
||||||
if [ "$code" = "200" ] || [ "$code" = "302" ] || [ "$code" = "301" ]; then
|
echo "⚠️ Pas d'image de rollback, à corriger manuellement."
|
||||||
ok=1; break
|
exit 1
|
||||||
fi
|
fi
|
||||||
sleep 1
|
|
||||||
done
|
|
||||||
|
|
||||||
rm -f ~/.netrc
|
|
||||||
|
|
||||||
if [ "$ok" -ne 1 ]; then
|
|
||||||
echo "Health-check KO -> rollback"
|
|
||||||
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
|
fi
|
||||||
|
|
||||||
echo "Health-check OK"
|
echo "✅ PROD ok avec ${NEW_IMAGE}"
|
||||||
|
4) Ce que ça change côté usage
|
||||||
Reference in new issue
Block a user