Improve resiliance CICD
This commit is contained in:
parent
d5312c3667
commit
60bb767603
2 changed files with 68 additions and 63 deletions
|
|
@ -30,19 +30,16 @@ jobs:
|
||||||
container:
|
container:
|
||||||
# Image complète (et non slim) : git y est déjà présent.
|
# Image complète (et non slim) : git y est déjà présent.
|
||||||
image: python:3.13-trixie
|
image: python:3.13-trixie
|
||||||
services:
|
# PostgreSQL tourne DANS le conteneur du job, sur un socket Unix privé, et
|
||||||
postgres:
|
# non comme « service » : en docker-in-docker avec `container.network: host`,
|
||||||
image: postgres:17-alpine
|
# les services de tous les jobs partagent 127.0.0.1:5432, et un service resté
|
||||||
env:
|
# en vie d'un run précédent répondrait à la place de celui du run (rôles déjà
|
||||||
POSTGRES_PASSWORD: ci-superuser
|
# créés : « role "shortstats_owner" already exists »). Ici la base est neuve
|
||||||
options: >-
|
# à chaque run, sans port réseau, quel que soit le réglage du runner.
|
||||||
--health-cmd "pg_isready -U postgres"
|
|
||||||
--health-interval 5s
|
|
||||||
--health-timeout 5s
|
|
||||||
--health-retries 10
|
|
||||||
env:
|
env:
|
||||||
PGUSER: postgres
|
PGUSER: postgres
|
||||||
PGPASSWORD: ci-superuser
|
PGPASSWORD: ci-superuser
|
||||||
|
PGHOST: /tmp/pgsock
|
||||||
steps:
|
steps:
|
||||||
- name: Récupérer le code
|
- name: Récupérer le code
|
||||||
env:
|
env:
|
||||||
|
|
@ -56,26 +53,27 @@ jobs:
|
||||||
git -c "http.extraHeader=Authorization: Basic ${auth}" fetch -q --depth 1 origin "${GITHUB_SHA}"
|
git -c "http.extraHeader=Authorization: Basic ${auth}" fetch -q --depth 1 origin "${GITHUB_SHA}"
|
||||||
git checkout -q FETCH_HEAD
|
git checkout -q FETCH_HEAD
|
||||||
|
|
||||||
- name: Installer psql et les dépendances
|
- name: Installer PostgreSQL et les dépendances
|
||||||
run: |
|
run: |
|
||||||
apt-get update -qq && apt-get install -y -qq --no-install-recommends postgresql-client >/dev/null
|
# Pas de cluster Debian démarré automatiquement : on crée le nôtre.
|
||||||
|
mkdir -p /etc/postgresql-common/createcluster.d
|
||||||
|
echo 'create_main_cluster = false' > /etc/postgresql-common/createcluster.d/ci.conf
|
||||||
|
apt-get update -qq && apt-get install -y -qq --no-install-recommends postgresql >/dev/null
|
||||||
pip install -q --constraint constraints.txt ".[collector,web,dev]"
|
pip install -q --constraint constraints.txt ".[collector,web,dev]"
|
||||||
|
|
||||||
- name: Trouver le service PostgreSQL
|
- name: Démarrer un PostgreSQL privé au job
|
||||||
run: |
|
run: |
|
||||||
# Réseau dédié au job : le service se nomme « postgres ».
|
bin=$(printf '%s\n' /usr/lib/postgresql/*/bin | sort -V | tail -n 1)
|
||||||
# Runner en `container.network: host` (fréquent en docker-in-docker) :
|
install -d -o postgres -g postgres -m 700 /tmp/pgdata
|
||||||
# le nom n'est pas résolu, le service écoute sur 127.0.0.1.
|
install -d -o postgres -g postgres -m 755 "$PGHOST"
|
||||||
# On essaie les deux pendant 60 s et on garde celui qui répond.
|
printf '%s\n' "$PGPASSWORD" > /tmp/pgpass.txt
|
||||||
for _ in $(seq 1 30); do
|
chown postgres /tmp/pgpass.txt
|
||||||
for h in postgres 127.0.0.1; do
|
# Mots de passe réellement vérifiés (SCRAM), comme en production.
|
||||||
if pg_isready -q -h "$h" -p 5432 2>/dev/null; then
|
su postgres -c "$bin/initdb -D /tmp/pgdata -U postgres --auth=scram-sha-256 --pwfile=/tmp/pgpass.txt -E UTF8 --locale=C.UTF-8" >/dev/null
|
||||||
echo "PGHOST=${h}" >> "$GITHUB_ENV"; echo "PostgreSQL trouvé sur : ${h}"; exit 0
|
rm -f /tmp/pgpass.txt
|
||||||
fi
|
# listen_addresses vide : aucun port TCP, seulement le socket Unix.
|
||||||
done
|
su postgres -c "$bin/pg_ctl -D /tmp/pgdata -l /tmp/pgdata/server.log -w -o \"-c listen_addresses='' -k $PGHOST\" start"
|
||||||
sleep 2
|
psql -qAtc 'SELECT version()'
|
||||||
done
|
|
||||||
echo "PostgreSQL injoignable par « postgres » et par 127.0.0.1" >&2; exit 1
|
|
||||||
|
|
||||||
- name: Créer la base avec le script SQL minimal
|
- name: Créer la base avec le script SQL minimal
|
||||||
run: |
|
run: |
|
||||||
|
|
@ -83,9 +81,10 @@ jobs:
|
||||||
|
|
||||||
- name: Tests
|
- name: Tests
|
||||||
run: |
|
run: |
|
||||||
export TEST_OWNER_URL="postgresql://shortstats_owner:remplacer-mdp-owner@${PGHOST}:5432/shortstats"
|
q="host=${PGHOST}"
|
||||||
export TEST_COLLECTOR_URL="postgresql://shortstats_collector:remplacer-mdp-collector@${PGHOST}:5432/shortstats"
|
export TEST_OWNER_URL="postgresql://shortstats_owner:remplacer-mdp-owner@/shortstats?${q}"
|
||||||
export TEST_WEB_URL="postgresql://shortstats_web:remplacer-mdp-web@${PGHOST}:5432/shortstats"
|
export TEST_COLLECTOR_URL="postgresql://shortstats_collector:remplacer-mdp-collector@/shortstats?${q}"
|
||||||
|
export TEST_WEB_URL="postgresql://shortstats_web:remplacer-mdp-web@/shortstats?${q}"
|
||||||
pytest -q
|
pytest -q
|
||||||
|
|
||||||
images:
|
images:
|
||||||
|
|
|
||||||
|
|
@ -39,32 +39,38 @@ jobs:
|
||||||
docker logout "$registry"
|
docker logout "$registry"
|
||||||
|
|
||||||
service:
|
service:
|
||||||
# Le job `test` de build.yaml utilise un service PostgreSQL et récupère le code par git.
|
# Le job `test` de build.yaml installe PostgreSQL dans son conteneur (paquets
|
||||||
|
# Debian) et récupère le code par git. On vérifie les deux, et on signale les
|
||||||
|
# conteneurs PostgreSQL restés en vie d'anciens runs (port 5432 partagé en
|
||||||
|
# `container.network: host`), sans rien changer.
|
||||||
runs-on: docker
|
runs-on: docker
|
||||||
container:
|
container:
|
||||||
image: python:3.13-trixie
|
image: python:3.13-trixie
|
||||||
services:
|
|
||||||
postgres:
|
|
||||||
image: postgres:17-alpine
|
|
||||||
env:
|
|
||||||
POSTGRES_PASSWORD: verif
|
|
||||||
steps:
|
steps:
|
||||||
- name: Joindre le service PostgreSQL
|
- name: Installer et démarrer PostgreSQL dans le job
|
||||||
env:
|
env:
|
||||||
PGUSER: postgres
|
PGUSER: postgres
|
||||||
PGPASSWORD: verif
|
PGPASSWORD: verif
|
||||||
|
PGHOST: /tmp/pgsock
|
||||||
run: |
|
run: |
|
||||||
apt-get update -qq && apt-get install -y -qq --no-install-recommends postgresql-client >/dev/null
|
mkdir -p /etc/postgresql-common/createcluster.d
|
||||||
host=""
|
echo 'create_main_cluster = false' > /etc/postgresql-common/createcluster.d/ci.conf
|
||||||
for _ in $(seq 1 30); do
|
apt-get update -qq && apt-get install -y -qq --no-install-recommends postgresql >/dev/null
|
||||||
for h in postgres 127.0.0.1; do
|
bin=$(printf '%s\n' /usr/lib/postgresql/*/bin | sort -V | tail -n 1)
|
||||||
if pg_isready -q -h "$h" -p 5432 2>/dev/null; then host="$h"; break 2; fi
|
install -d -o postgres -g postgres -m 700 /tmp/pgdata
|
||||||
done
|
install -d -o postgres -g postgres -m 755 "$PGHOST"
|
||||||
sleep 2
|
printf '%s\n' "$PGPASSWORD" > /tmp/pgpass.txt && chown postgres /tmp/pgpass.txt
|
||||||
done
|
su postgres -c "$bin/initdb -D /tmp/pgdata -U postgres --auth=scram-sha-256 --pwfile=/tmp/pgpass.txt -E UTF8 --locale=C.UTF-8" >/dev/null
|
||||||
[ -n "$host" ] || { echo "PostgreSQL injoignable par « postgres » et par 127.0.0.1" >&2; exit 1; }
|
su postgres -c "$bin/pg_ctl -D /tmp/pgdata -l /tmp/pgdata/server.log -w -o \"-c listen_addresses='' -k $PGHOST\" start"
|
||||||
echo "PostgreSQL trouvé sur : ${host}"
|
psql -tAc 'select version()'
|
||||||
psql -h "$host" -tAc 'select version()'
|
|
||||||
|
- name: Ancien PostgreSQL sur 127.0.0.1:5432 ? (information)
|
||||||
|
run: |
|
||||||
|
if (exec 3<>/dev/tcp/127.0.0.1/5432) 2>/dev/null; then
|
||||||
|
echo "::warning::Quelque chose écoute sur 127.0.0.1:5432 dans le réseau des jobs : probablement un service PostgreSQL resté en vie d'un ancien run. Sans conséquence pour build.yaml ; voir la documentation d'installation (dépannage) pour le supprimer."
|
||||||
|
else
|
||||||
|
echo "Rien sur 127.0.0.1:5432."
|
||||||
|
fi
|
||||||
|
|
||||||
- name: Récupérer le code (même méthode que build.yaml)
|
- name: Récupérer le code (même méthode que build.yaml)
|
||||||
env:
|
env:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue