From 3cd214ae78d2035c80464919c474c08fb32a1960 Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Tue, 1 Dec 2026 19:21:40 +0100 Subject: [PATCH] Publish the image on version tags and test what is delivered A v* tag now builds, tries and publishes the image: the version must be the one of Directory.Build.props, latest only moves forward and the registry token is in one step, given to docker on its input. The same script works by hand. The image says its version and commit, and its health check also covers the engine loop and the live prices. The Unraid template is final: a test compares it with appendix E and with the variables the loader really reads, and another reads the workflow. refs #70 refs #71 Co-Authored-By: Claude Opus 5.5 --- .gitea/workflows/ci.yml | 48 ++- Directory.Build.props | 2 +- README.md | 4 +- deploy/docker/Dockerfile | 19 +- deploy/docker/publish.sh | 47 +++ deploy/unraid/anubis.xml | 2 +- docs/SVILUPPO.md | 8 +- src/Anubis.Server/Endpoints.cs | 33 +- src/Anubis.Server/SystemView.cs | 15 +- src/Anubis/Data/Collector.cs | 9 + src/Anubis/Data/MarketDataService.cs | 3 + src/Anubis/Platform/PlatformLoader.cs | 4 + src/Anubis/Trading/TradingService.cs | 4 + tests/Anubis.Tests/DeliveryTests.cs | 477 ++++++++++++++++++++++++++ tests/Anubis.Tests/MiniYaml.cs | 177 ++++++++++ tools/Anubis.Tasks/Program.cs | 34 +- 16 files changed, 859 insertions(+), 27 deletions(-) create mode 100644 deploy/docker/publish.sh create mode 100644 tests/Anubis.Tests/DeliveryTests.cs create mode 100644 tests/Anubis.Tests/MiniYaml.cs diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 1e7aa75..7671a18 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -1,10 +1,11 @@ # CI (SPEC 15.5, ADR 0003/0004): only run steps with git, dotnet and docker, no external actions. -# Publishing to the registry on v* tags arrives in phase 9 (issue #70). +# The image is published only on v* tags, after the tests and the drill (SPEC 15.5, ADR 0002 and 0020); never on fase-N. name: ci on: push: branches: ["**"] + tags: ["v*"] pull_request: jobs: @@ -44,10 +45,15 @@ jobs: TESTINGPLATFORM_TELEMETRY_OPTOUT: "1" steps: - name: Checkout + # The token goes to git through its environment, so it is on no command line. + env: + GIT_CONFIG_COUNT: "1" + GIT_CONFIG_KEY_0: http.extraHeader + GIT_CONFIG_VALUE_0: "Authorization: token ${{ github.token }}" run: | git init -q . git remote add origin "${{ github.server_url }}/${{ github.repository }}.git" - git -c http.extraHeader="Authorization: token ${{ github.token }}" fetch -q --depth 1 origin "${{ github.sha }}" + git fetch -q --depth 1 origin "${{ github.sha }}" git checkout -q FETCH_HEAD - name: Check run: sh ./tasks.sh check @@ -59,20 +65,42 @@ jobs: image: needs: test runs-on: ubuntu-latest - # Registry settings for phase 9 (issue #70): declared here, not used before then. env: - REGISTRY: gitea.encke-hake.ts.net - REGISTRY_USER: ${{ github.repository_owner }} - REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }} + # The Docker daemon of the runner is shared between runs: a name of this run only, so what is published is what was tried. + IMAGE: anubis:ci-${{ github.run_id }}-${{ github.run_attempt }} steps: - name: Checkout + # The token goes to git through its environment, so it is on no command line. + env: + GIT_CONFIG_COUNT: "1" + GIT_CONFIG_KEY_0: http.extraHeader + GIT_CONFIG_VALUE_0: "Authorization: token ${{ github.token }}" run: | git init -q . git remote add origin "${{ github.server_url }}/${{ github.repository }}.git" - git -c http.extraHeader="Authorization: token ${{ github.token }}" fetch -q --depth 1 origin "${{ github.sha }}" + git fetch -q --depth 1 origin "${{ github.sha }}" git checkout -q FETCH_HEAD - - name: Build image (no publish) - run: docker build -f deploy/docker/Dockerfile -t anubis:ci . + - name: Build image + env: + REF: ${{ github.ref }} + REF_NAME: ${{ github.ref_name }} + run: | + case "$REF" in refs/tags/v*) version=${REF_NAME#v} ;; *) version=dev ;; esac + docker build -f deploy/docker/Dockerfile --build-arg "ANUBIS_VERSION=$version" --build-arg "ANUBIS_COMMIT=$(git rev-parse HEAD)" -t "$IMAGE" . # The guard drill inside the image (issue #48): offline, its own SQLite database in a temporary folder. - name: Guard drill in the image - run: docker run --rm anubis:ci drill + run: docker run --rm "$IMAGE" drill + - name: Publish image + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') + env: + REGISTRY: ${{ vars.REGISTRY || 'gitea.encke-hake.ts.net' }} + REGISTRY_USER: ${{ vars.REGISTRY_USER || github.actor }} + REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }} + TAG: ${{ github.ref_name }} + GIT_CONFIG_COUNT: "1" + GIT_CONFIG_KEY_0: http.extraHeader + GIT_CONFIG_VALUE_0: "Authorization: token ${{ github.token }}" + run: sh ./deploy/docker/publish.sh "$IMAGE" "$TAG" + - name: Remove the image of this run + if: always() + run: docker image rm -f "$IMAGE" || true diff --git a/Directory.Build.props b/Directory.Build.props index 8bd1fc2..0d197c7 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -13,7 +13,7 @@ it;en BalboSoft Anubis - 0.7.0 + 0.9.0 diff --git a/README.md b/README.md index 8e78e1c..12fbe5c 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Database a scelta: PostgreSQL oppure SQLite. Scritto in C# su .NET 10, con inter Nell'antico Egitto Anubis, il dio con la testa di sciacallo, pesava il cuore dei defunti contro la piuma di Maat, la verità e l'equilibrio, e custodiva le tombe. Il bot fa lo stesso con i soldi: **pesa** ogni operazione contro il rischio prima di farla e **protegge** il capitale. Il logo è quella bilancia, con il cuore e la piuma in equilibrio (ADR 0005). -> Stato: **progettazione completata, sviluppo non ancora iniziato.** Il piano è nelle [milestone](https://gitea.encke-hake.ts.net/BalboSoft/Anubis/milestones) del repository. +> Stato: **in simulazione, pronto da installare in Docker.** Il piano è nelle [milestone](https://gitea.encke-hake.ts.net/BalboSoft/Anubis/milestones) del repository. ![Panoramica](docs/design/01-panoramica.jpg) @@ -29,7 +29,7 @@ Nell'antico Egitto Anubis, il dio con la testa di sciacallo, pesava il cuore dei | [docs/SVILUPPO.md](docs/SVILUPPO.md) | Come si lavora: flusso su Gitea, regole, prove | | [docs/adr/](docs/adr/) | Decisioni prese: nome (0001), Gitea (0002), linguaggio C# (0003), componenti esterni (0004), logo (0005) | | [docs/design/](docs/design/) | Schermate di riferimento e logo | -| [deploy/unraid/anubis.xml](deploy/unraid/anubis.xml) | Template Docker per Unraid (bozza) | +| [deploy/unraid/anubis.xml](deploy/unraid/anubis.xml) | Template Docker per Unraid | ## Schermate di riferimento diff --git a/deploy/docker/Dockerfile b/deploy/docker/Dockerfile index 6c2f15d..511c295 100644 --- a/deploy/docker/Dockerfile +++ b/deploy/docker/Dockerfile @@ -17,9 +17,11 @@ COPY docs/design/logo/ docs/design/logo/ # The backtest outcomes are read at the first start (SPEC 12.3); .dockerignore lets only their JSON files through. COPY docs/backtests/ docs/backtests/ COPY docs/learning/ docs/learning/ -RUN dotnet publish src/Anubis.Server/Anubis.Server.csproj -c Release --no-restore -o /app +# The commit goes into the version shown by the diagnostics report; empty when not given. +ARG ANUBIS_COMMIT= +RUN dotnet publish src/Anubis.Server/Anubis.Server.csproj -c Release --no-restore -o /app ${ANUBIS_COMMIT:+-p:SourceRevisionId=$ANUBIS_COMMIT} # The guard (SPEC 9.3, ADR 0012) is the main process and starts the engine next to it, from the same folder. -RUN dotnet publish src/Anubis.Guard/Anubis.Guard.csproj -c Release --no-restore -o /app +RUN dotnet publish src/Anubis.Guard/Anubis.Guard.csproj -c Release --no-restore -o /app ${ANUBIS_COMMIT:+-p:SourceRevisionId=$ANUBIS_COMMIT} FROM mcr.microsoft.com/dotnet/aspnet:10.0 WORKDIR /app @@ -35,7 +37,16 @@ EXPOSE 8787 VOLUME /data # Starts as root only to give /data to PUID:PGID, then runs the guard as that user (entrypoint.sh); the guard starts # the engine. "docker run --rm drill" runs the guard drill instead (issue #48). -# Health through bash's /dev/tcp: no curl or wget in the image. 503 (database down) counts as unhealthy. -HEALTHCHECK --interval=30s --timeout=5s --start-period=60s --retries=3 \ +# Health through bash's /dev/tcp: no curl or wget in the image. 503 (database down, loops stopped, no prices) counts as unhealthy. +HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \ CMD ["bash", "-c", "exec 3<>/dev/tcp/127.0.0.1/${PORT:-8787} && printf 'GET /health HTTP/1.0\r\nHost: localhost\r\n\r\n' >&3 && head -n 1 <&3 | grep -q ' 200 '"] ENTRYPOINT ["anubis-entrypoint"] +ARG ANUBIS_VERSION=dev +ARG ANUBIS_COMMIT= +LABEL org.opencontainers.image.title="Anubis" \ + org.opencontainers.image.description="Bot di trading personale: azioni USA e crypto spot, senza leva" \ + org.opencontainers.image.vendor="BalboSoft" \ + org.opencontainers.image.url="https://gitea.encke-hake.ts.net/BalboSoft/Anubis" \ + org.opencontainers.image.source="https://gitea.encke-hake.ts.net/BalboSoft/Anubis" \ + org.opencontainers.image.version="$ANUBIS_VERSION" \ + org.opencontainers.image.revision="$ANUBIS_COMMIT" diff --git a/deploy/docker/publish.sh b/deploy/docker/publish.sh new file mode 100644 index 0000000..1b67eb7 --- /dev/null +++ b/deploy/docker/publish.sh @@ -0,0 +1,47 @@ +#!/bin/sh +# Publishes an image already built and tried (SPEC 15.5, ADR 0002 and 0020). +# Usage: publish.sh +# From the environment: REGISTRY, REGISTRY_USER, REGISTRY_TOKEN (write:package). The token reaches docker only on its +# input and stays in a temporary folder removed at the end. +set -eu + +image="$1" +tag="$2" +root=$(cd "$(dirname "$0")/../.." && pwd) +fail() { + echo "Pubblicazione non riuscita: $1" >&2 + exit 1 +} + +printf '%s\n' "$tag" | grep -Eq '^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.]+)?$' || fail "il tag '$tag' non è una versione (vX.Y.Z)." +version=${tag#v} +expected=$(sed -n 's:.*\(.*\).*:\1:p' "$root/Directory.Build.props") +[ "${version%%-*}" = "$expected" ] || fail "il tag $tag non corrisponde alla versione $expected di Directory.Build.props." +[ -n "${REGISTRY:-}" ] || fail "manca REGISTRY." +[ -n "${REGISTRY_USER:-}" ] || fail "manca REGISTRY_USER." +[ -n "${REGISTRY_TOKEN:-}" ] || fail "manca il segreto REGISTRY_TOKEN (token Gitea con il permesso write:package)." +name="$REGISTRY/balbosoft/anubis" + +# latest moves only for the highest plain version among the tags of the repository. +latest="" +case "$version" in + *-*) ;; + *) + tags=$(git -C "$root" ls-remote --tags --refs origin 'v*') || fail "non riesco a leggere i tag del repository." + newest=$(printf '%s\n' "$tags" "x refs/tags/$tag" | sed -n 's:.*refs/tags/v\([0-9]*\.[0-9]*\.[0-9]*\)$:\1:p' | sort -t. -k1,1n -k2,2n -k3,3n | tail -n 1) + if [ "$newest" = "$version" ]; then latest=1; fi + ;; +esac + +DOCKER_CONFIG=$(mktemp -d) +export DOCKER_CONFIG +trap 'rm -rf "$DOCKER_CONFIG"' EXIT +printf '%s' "$REGISTRY_TOKEN" | docker login "$REGISTRY" --username "$REGISTRY_USER" --password-stdin +docker tag "$image" "$name:$version" +docker push "$name:$version" +if [ -n "$latest" ]; then + docker tag "$image" "$name:latest" + docker push "$name:latest" +fi + +echo "Pubblicata $name:$version${latest:+ (e latest)}." diff --git a/deploy/unraid/anubis.xml b/deploy/unraid/anubis.xml index c078524..9515c12 100644 --- a/deploy/unraid/anubis.xml +++ b/deploy/unraid/anubis.xml @@ -1,6 +1,6 @@