diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..0403e3a --- /dev/null +++ b/.dockerignore @@ -0,0 +1,16 @@ +.git +.forgejo +.env +.env.* +deploy/ +docs/ +tests/ +node_modules/ +src/shortstats/web/static/vendor/ +**/__pycache__ +**/*.egg-info +.venv +venv +.pytest_cache +build/ +dist/ diff --git a/.forgejo/workflows/build.yaml b/.forgejo/workflows/build.yaml new file mode 100644 index 0000000..d2f7e62 --- /dev/null +++ b/.forgejo/workflows/build.yaml @@ -0,0 +1,156 @@ +# Tests puis construction et publication des deux images dans le registre +# de conteneurs du Forgejo qui exécute le workflow. Les images publiées sont +# le seul livrable : le serveur les utilise avec son propre compose +# (deploy/compose.yaml), sans le code source. +# +# Prérequis (voir docs/02-installation.md, section 2) : +# - un runner Forgejo avec le label `docker` et un accès à un démon Docker +# (docker-in-docker ou socket de l'hôte) ; +# - le secret REGISTRY_TOKEN : jeton d'accès avec la portée write:package ; +# - facultatif : la variable REGISTRY_USER (sinon l'auteur du push) ; +# - facultatif : la variable IMAGE_PLATFORM (ex. linux/amd64) si l'architecture +# du serveur diffère de celle du runner. + +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 + env: + PGHOST: postgres + PGUSER: postgres + PGPASSWORD: ci-superuser + TEST_OWNER_URL: postgresql://shortstats_owner:remplacer-mdp-owner@postgres:5432/shortstats + TEST_COLLECTOR_URL: postgresql://shortstats_collector:remplacer-mdp-collector@postgres:5432/shortstats + TEST_WEB_URL: postgresql://shortstats_web:remplacer-mdp-web@postgres:5432/shortstats + steps: + - name: Récupérer le code + env: + TOKEN: ${{ github.token }} + run: | + git init -q . + git remote add origin "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git" + 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 + run: | + apt-get update -qq && apt-get install -y -qq --no-install-recommends postgresql-client >/dev/null + pip install -q --constraint constraints.txt ".[collector,web,dev]" + + - name: Créer la base avec le script SQL minimal + run: | + for _ in $(seq 1 30); do pg_isready -q && break; sleep 2; done + psql -v ON_ERROR_STOP=1 -f sql/bootstrap.sql + + - name: Tests + run: pytest -q + + images: + needs: test + if: github.event_name != 'pull_request' + runs-on: docker + container: + image: docker:29-cli + strategy: + matrix: + target: [collector, web] + steps: + - name: Outils + run: apk add --no-cache -q git + + - name: Récupérer le code + env: + TOKEN: ${{ github.token }} + run: | + git init -q . + git remote add origin "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git" + 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 + run: | + registry=$(echo "${GITHUB_SERVER_URL}" | sed -E 's#^https?://##; s#/.*$##') + owner=$(echo "${GITHUB_REPOSITORY%%/*}" | tr '[:upper:]' '[:lower:]') + image="${registry}/${owner}/shortstats-${{ matrix.target }}" + short_sha=$(echo "${GITHUB_SHA}" | cut -c1-12) + tags="${image}:sha-${short_sha}" + if [ "${GITHUB_REF_TYPE:-}" = "tag" ] || echo "${GITHUB_REF}" | grep -q '^refs/tags/'; then + tags="${tags} ${image}:${GITHUB_REF_NAME#v}" + fi + if [ "${GITHUB_REF}" = "refs/heads/main" ]; then + tags="${tags} ${image}:latest" + fi + version="sha-${short_sha}" + case "${GITHUB_REF}" in refs/tags/v*) version="${GITHUB_REF_NAME#v}" ;; esac + { + echo "registry=${registry}" + echo "owner=${owner}" + echo "image=${image}" + echo "tags=${tags}" + 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" + IMAGE_PLATFORM: ${{ vars.IMAGE_PLATFORM }} + run: | + set -eu + tag_args="" + for t in ${{ steps.meta.outputs.tags }}; do tag_args="${tag_args} --tag ${t}"; done + platform_args="" + if [ -n "${IMAGE_PLATFORM:-}" ]; then platform_args="--platform ${IMAGE_PLATFORM}"; fi + # shellcheck disable=SC2086 + docker build \ + --target "${{ matrix.target }}" \ + ${platform_args} \ + --label "org.opencontainers.image.revision=${GITHUB_SHA}" \ + --label "org.opencontainers.image.version=${{ steps.meta.outputs.version }}" \ + --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) + echo "Architecture : $(docker image inspect --format '{{.Os}}/{{.Architecture}}' "${first_tag}")" + echo "Empreinte : $(docker image inspect --format '{{index .RepoDigests 0}}' "${first_tag}")" + echo "Dans le .env du dossier de déploiement :" + echo " IMAGE_PREFIX=${{ steps.meta.outputs.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 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c0b8c3d --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +# Généré au build (npm run build) +src/shortstats/web/static/vendor/ +node_modules/ + +# Secrets et configuration locale +.env +deploy/users.txt +users.txt + +# Python +__pycache__/ +*.egg-info/ +.venv/ +venv/ +.pytest_cache/ +build/ +dist/