diff --git a/.forgejo/workflows/build.yaml b/.forgejo/workflows/build.yaml index 098d3aa..b1cc45b 100644 --- a/.forgejo/workflows/build.yaml +++ b/.forgejo/workflows/build.yaml @@ -14,35 +14,32 @@ # les jobs doivent joindre Forgejo (code et registre) par une adresse interne # plutôt que par son URL publique (ROOT_URL). Le serveur de production tire # toujours les images par l'URL publique : c'est le même registre. - + name: build - + on: push: branches: [main] tags: ["v*"] pull_request: workflow_dispatch: - + jobs: test: runs-on: docker container: # Image complète (et non slim) : git y est déjà présent. image: python:3.13-trixie - services: - postgres: - image: postgres:17-alpine - env: - POSTGRES_PASSWORD: ci-superuser - options: >- - --health-cmd "pg_isready -U postgres" - --health-interval 5s - --health-timeout 5s - --health-retries 10 + # PostgreSQL tourne DANS le conteneur du job, sur un socket Unix privé, et + # non comme « service » : en docker-in-docker avec `container.network: host`, + # les services de tous les jobs partagent 127.0.0.1:5432, et un service resté + # en vie d'un run précédent répondrait à la place de celui du run (rôles déjà + # créés : « role "shortstats_owner" already exists »). Ici la base est neuve + # à chaque run, sans port réseau, quel que soit le réglage du runner. env: PGUSER: postgres PGPASSWORD: ci-superuser + PGHOST: /tmp/pgsock steps: - name: Récupérer le code env: @@ -55,39 +52,41 @@ jobs: auth=$(printf 'x-access-token:%s' "$TOKEN" | base64 -w0) git -c "http.extraHeader=Authorization: Basic ${auth}" fetch -q --depth 1 origin "${GITHUB_SHA}" git checkout -q FETCH_HEAD - - - name: Installer psql et les dépendances + + - name: Installer PostgreSQL et les dépendances 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]" - - - name: Trouver le service PostgreSQL + + - name: Démarrer un PostgreSQL privé au job run: | - # Réseau dédié au job : le service se nomme « postgres ». - # Runner en `container.network: host` (fréquent en docker-in-docker) : - # le nom n'est pas résolu, le service écoute sur 127.0.0.1. - # On essaie les deux pendant 60 s et on garde celui qui répond. - for _ in $(seq 1 30); do - for h in postgres 127.0.0.1; do - if pg_isready -q -h "$h" -p 5432 2>/dev/null; then - echo "PGHOST=${h}" >> "$GITHUB_ENV"; echo "PostgreSQL trouvé sur : ${h}"; exit 0 - fi - done - sleep 2 - done - echo "PostgreSQL injoignable par « postgres » et par 127.0.0.1" >&2; exit 1 - + bin=$(printf '%s\n' /usr/lib/postgresql/*/bin | sort -V | tail -n 1) + install -d -o postgres -g postgres -m 700 /tmp/pgdata + install -d -o postgres -g postgres -m 755 "$PGHOST" + printf '%s\n' "$PGPASSWORD" > /tmp/pgpass.txt + chown postgres /tmp/pgpass.txt + # Mots de passe réellement vérifiés (SCRAM), comme en production. + 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 + rm -f /tmp/pgpass.txt + # listen_addresses vide : aucun port TCP, seulement le socket Unix. + su postgres -c "$bin/pg_ctl -D /tmp/pgdata -l /tmp/pgdata/server.log -w -o \"-c listen_addresses='' -k $PGHOST\" start" + psql -qAtc 'SELECT version()' + - name: Créer la base avec le script SQL minimal run: | psql -v ON_ERROR_STOP=1 -f sql/bootstrap.sql - + - name: Tests run: | - export TEST_OWNER_URL="postgresql://shortstats_owner:remplacer-mdp-owner@${PGHOST}:5432/shortstats" - export TEST_COLLECTOR_URL="postgresql://shortstats_collector:remplacer-mdp-collector@${PGHOST}:5432/shortstats" - export TEST_WEB_URL="postgresql://shortstats_web:remplacer-mdp-web@${PGHOST}:5432/shortstats" + q="host=${PGHOST}" + export TEST_OWNER_URL="postgresql://shortstats_owner:remplacer-mdp-owner@/shortstats?${q}" + 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 - + images: needs: test if: github.event_name != 'pull_request' @@ -100,7 +99,7 @@ jobs: steps: - name: Outils run: apk add --no-cache -q git - + - name: Récupérer le code env: TOKEN: ${{ github.token }} @@ -112,7 +111,7 @@ jobs: auth=$(printf 'x-access-token:%s' "$TOKEN" | base64 -w 0) git -c "http.extraHeader=Authorization: Basic ${auth}" fetch -q --depth 1 origin "${GITHUB_SHA}" git checkout -q FETCH_HEAD - + - name: Calculer le nom et les étiquettes de l'image id: meta env: @@ -144,14 +143,14 @@ jobs: echo "version=${version}" } >> "$GITHUB_OUTPUT" echo "Image : ${image} ; étiquettes : ${tags}" - + - name: Connexion au registre Forgejo env: REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }} REGISTRY_USER: ${{ vars.REGISTRY_USER || github.actor }} run: | printf '%s' "$REGISTRY_TOKEN" | docker login "${{ steps.meta.outputs.registry }}" -u "$REGISTRY_USER" --password-stdin - + - name: Construire et publier env: DOCKER_BUILDKIT: "1" @@ -171,7 +170,7 @@ jobs: --label "org.opencontainers.image.source=${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}" \ ${tag_args} . for t in ${{ steps.meta.outputs.tags }}; do docker push "$t"; done - + - name: Valeurs pour le compose du serveur run: | first_tag=$(echo "${{ steps.meta.outputs.tags }}" | cut -d' ' -f1) @@ -180,7 +179,7 @@ jobs: echo "Dans le .env du dossier de déploiement :" echo " IMAGE_PREFIX=${{ steps.meta.outputs.public_registry }}/${{ steps.meta.outputs.owner }}" echo " IMAGE_TAG=${{ steps.meta.outputs.version }}" - + - name: Déconnexion if: always() - run: docker logout "${{ steps.meta.outputs.registry }}" || true + run: docker logout "${{ steps.meta.outputs.registry }}" || true \ No newline at end of file diff --git a/.forgejo/workflows/runner-check.yaml b/.forgejo/workflows/runner-check.yaml index df42469..52d80bf 100644 --- a/.forgejo/workflows/runner-check.yaml +++ b/.forgejo/workflows/runner-check.yaml @@ -39,32 +39,38 @@ jobs: docker logout "$registry" 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 container: image: python:3.13-trixie - services: - postgres: - image: postgres:17-alpine - env: - POSTGRES_PASSWORD: verif steps: - - name: Joindre le service PostgreSQL + - name: Installer et démarrer PostgreSQL dans le job env: PGUSER: postgres PGPASSWORD: verif + PGHOST: /tmp/pgsock run: | - apt-get update -qq && apt-get install -y -qq --no-install-recommends postgresql-client >/dev/null - host="" - for _ in $(seq 1 30); do - for h in postgres 127.0.0.1; do - if pg_isready -q -h "$h" -p 5432 2>/dev/null; then host="$h"; break 2; fi - done - sleep 2 - done - [ -n "$host" ] || { echo "PostgreSQL injoignable par « postgres » et par 127.0.0.1" >&2; exit 1; } - echo "PostgreSQL trouvé sur : ${host}" - psql -h "$host" -tAc 'select version()' + 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 + bin=$(printf '%s\n' /usr/lib/postgresql/*/bin | sort -V | tail -n 1) + install -d -o postgres -g postgres -m 700 /tmp/pgdata + install -d -o postgres -g postgres -m 755 "$PGHOST" + printf '%s\n' "$PGPASSWORD" > /tmp/pgpass.txt && chown postgres /tmp/pgpass.txt + 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 + su postgres -c "$bin/pg_ctl -D /tmp/pgdata -l /tmp/pgdata/server.log -w -o \"-c listen_addresses='' -k $PGHOST\" start" + psql -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) env: @@ -74,4 +80,4 @@ jobs: base="${INTERNAL_URL:-$GITHUB_SERVER_URL}" echo "Dépôt : ${base%/}/${GITHUB_REPOSITORY}.git" auth=$(printf 'x-access-token:%s' "$TOKEN" | base64 -w0) - git -c "http.extraHeader=Authorization: Basic ${auth}" ls-remote "${base%/}/${GITHUB_REPOSITORY}.git" HEAD + git -c "http.extraHeader=Authorization: Basic ${auth}" ls-remote "${base%/}/${GITHUB_REPOSITORY}.git" HEAD \ No newline at end of file