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 @@