name: Release # A tag vX.Y.Z[-pre] publishes the image gitea.encke-hake.ts.net/balbosoft/khaemhat:X.Y.Z (ADR 0009, 0010, 0015) and a # Gitea release whose notes are the CHANGELOG.md section of that version (no binaries). When the version is already # in the registry (published by scripts/publish-image.sh) the build is skipped and only the release is created. # The runner's own image has the docker CLI and builds with the host's daemon. on: push: tags: ["v*"] workflow_dispatch: inputs: tag: description: "Tag esistente da (ri)pubblicare, es. v0.0.1" required: true arm64: description: "Anche linux/arm64 (richiede QEMU e buildx sull'host)" type: boolean default: false permissions: contents: write packages: write env: TAG: ${{ github.event.inputs.tag || github.ref_name }} # Where people and programs reach Gitea (the runner itself uses github.server_url). Variable PUBLIC_URL overrides it. PUBLIC_URL: ${{ vars.PUBLIC_URL || 'https://gitea.encke-hake.ts.net' }} jobs: release: name: Image and release runs-on: ubuntu-latest timeout-minutes: 60 steps: - name: Remove what cancelled jobs left run: | echo "NAME=khaemhat-rel-$(hostname)" >> "$GITHUB_ENV" for c in $(docker ps -aq --filter label=khaemhat-ci.job); do job="$(docker inspect -f '{{index .Config.Labels "khaemhat-ci.job"}}' "$c")" docker inspect "$job" > /dev/null 2>&1 || docker rm -f -v "$c" done for n in $(docker network ls -q --filter label=khaemhat-ci.job); do job="$(docker network inspect -f '{{index .Labels "khaemhat-ci.job"}}' "$n")" docker inspect "$job" > /dev/null 2>&1 || docker network rm "$n" done - name: Version run: | case "$TAG" in v[0-9]*.[0-9]*.[0-9]*) ;; *) echo "::error title=Tag::$TAG is not vX.Y.Z[-pre]"; exit 1 ;; esac version="${TAG#v}" prerelease=false case "$version" in *-*) prerelease=true ;; esac major_minor="${version%%-*}" major_minor="${major_minor%.*}" owner="$(printf '%s' "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')" registry="${{ vars.REGISTRY }}" registry="${registry:-$(printf '%s' "$PUBLIC_URL" | sed -E 's#^[a-z]+://##; s#/.*$##')}" { echo "VERSION=$version" echo "PRERELEASE=$prerelease" echo "MAJOR_MINOR=$major_minor" echo "REGISTRY_HOST=$registry" echo "IMAGE=$registry/$owner/khaemhat" } >> "$GITHUB_ENV" echo "Tag $TAG -> image $registry/$owner/khaemhat:$version (prerelease: $prerelease)" - name: Checkout env: TOKEN: ${{ github.token }} run: | git config --global --add safe.directory "$PWD" git init -q . git remote add origin "${{ github.server_url }}/${{ github.repository }}.git" git -c http.extraheader="Authorization: basic $(printf 'x-access-token:%s' "$TOKEN" | base64 -w0)" fetch -q --depth=1 origin "refs/tags/$TAG:refs/tags/$TAG" git checkout -q "refs/tags/$TAG" echo "GIT_SHA=$(git rev-parse --short HEAD)" >> "$GITHUB_ENV" - name: Sign in to the registry env: USER: ${{ vars.REGISTRY_USER || github.actor }} TOKEN: ${{ secrets.REGISTRY_TOKEN }} run: | if [ -z "$TOKEN" ] || ! printf '%s' "$TOKEN" | docker login "$REGISTRY_HOST" -u "$USER" --password-stdin; then echo "::error title=Registry::Sign-in to $REGISTRY_HOST refused. The organisation secret REGISTRY_TOKEN must be an access token with package read and write." exit 1 fi status="$(curl -s -o /dev/null -w '%{http_code}' -H "Authorization: token $TOKEN" \ "${{ github.server_url }}/api/v1/packages/${{ github.repository_owner }}/container/khaemhat/$VERSION")" if [ "$status" = "200" ]; then echo "Version $VERSION is already in the registry: build and push skipped (ADR 0015)." echo "PUBLISHED=true" >> "$GITHUB_ENV" else echo "PUBLISHED=false" >> "$GITHUB_ENV" fi - name: Build image if: env.PUBLISHED != 'true' run: | icon="$PUBLIC_URL/${{ github.repository }}/raw/branch/main/assets/icon-256.png" if [ -f "assets/icon-v$MAJOR_MINOR-256.png" ]; then icon="$PUBLIC_URL/${{ github.repository }}/raw/branch/main/assets/icon-v$MAJOR_MINOR-256.png" fi echo "ICON_URL=$icon" >> "$GITHUB_ENV" docker build -f docker/Dockerfile \ --build-arg VERSION="$VERSION" \ --build-arg GIT_SHA="$GIT_SHA" \ --build-arg CREATED="$(date -u +%Y-%m-%dT%H:%M:%SZ)" \ --build-arg SOURCE_URL="$PUBLIC_URL/${{ github.repository }}" \ --build-arg ICON_URL="$icon" \ --label khaemhat-ci.job="$(hostname)" \ -t "$IMAGE:$VERSION" . - name: Smoke test (/health/ready) if: env.PUBLISHED != 'true' run: | docker network create --label khaemhat-ci.job="$(hostname)" "$NAME" docker network connect "$NAME" "$(hostname)" docker run -d --name "$NAME-app" --network "$NAME" --label khaemhat-ci.job="$(hostname)" \ -e KHAEMHAT_SYSTEM_DB=sqlite:///data/system.db -e KHAEMHAT_LICENSE_MODE=test \ -e KHAEMHAT_SECRET_KEY="$(head -c 32 /dev/urandom | base64)" \ -e KHAEMHAT_SUPERUSER=root -e KHAEMHAT_SUPERUSER_PASSWORD=Ci-Smoke-Test-2026 -e KHAEMHAT_COMPANY_CODE=CI \ "$IMAGE:$VERSION" for i in $(seq 1 60); do if curl -fsS --max-time 5 "http://$NAME-app:8080/health/ready"; then echo; break; fi sleep 2 done report="$(curl -fsS --max-time 5 "http://$NAME-app:8080/health")" echo "$report" echo "$report" | grep -q "\"version\":\"$VERSION\"" - name: Push if: env.PUBLISHED != 'true' run: | docker push "$IMAGE:$VERSION" if [ "$PRERELEASE" != "true" ]; then docker tag "$IMAGE:$VERSION" "$IMAGE:latest" docker push "$IMAGE:latest" fi echo "Image: $IMAGE:$VERSION$([ "$PRERELEASE" != "true" ] && echo " and :latest")" - name: Multi-architecture push (arm64) if: env.PUBLISHED != 'true' && github.event.inputs.arm64 == 'true' run: | docker buildx build --platform linux/amd64,linux/arm64 -f docker/Dockerfile \ --build-arg VERSION="$VERSION" --build-arg GIT_SHA="$GIT_SHA" \ --build-arg SOURCE_URL="$PUBLIC_URL/${{ github.repository }}" --build-arg ICON_URL="$ICON_URL" \ -t "$IMAGE:$VERSION" --push . # Notes: the "## [X.Y.Z]" section of CHANGELOG.md. The release is created or, when it exists, left as it is. - name: Gitea release env: TOKEN: ${{ github.token }} run: | api="${{ github.server_url }}/api/v1/repos/${{ github.repository }}" if curl -fs -o /dev/null -H "Authorization: token $TOKEN" "$api/releases/tags/$TAG"; then echo "Release $TAG already exists." exit 0 fi awk -v v="$VERSION" ' index($0, "## [" v "]") == 1 { on = 1; next } on && /^## \[/ { exit } on { print } ' CHANGELOG.md > "$RUNNER_TEMP/notes.md" if [ ! -s "$RUNNER_TEMP/notes.md" ]; then echo "::warning title=Changelog::No section [$VERSION] in CHANGELOG.md" echo "Vedi CHANGELOG.md." > "$RUNNER_TEMP/notes.md" fi printf '\nImmagine: `%s:%s`\n' "$IMAGE" "$VERSION" >> "$RUNNER_TEMP/notes.md" body="$(awk 'BEGIN { ORS = "\\n" } { gsub(/\r/, ""); gsub(/\\/, "\\\\"); gsub(/"/, "\\\""); gsub(/\t/, "\\t"); print }' "$RUNNER_TEMP/notes.md")" printf '{"tag_name":"%s","name":"Khaemhat %s","body":"%s","draft":false,"prerelease":%s}' \ "$TAG" "$VERSION" "$body" "$PRERELEASE" > "$RUNNER_TEMP/release.json" curl -fsS -X POST -H "Authorization: token $TOKEN" -H "Content-Type: application/json" \ --data-binary "@$RUNNER_TEMP/release.json" "$api/releases" > /dev/null echo "Release $TAG created." - name: Clean up if: always() run: | docker logs "$NAME-app" 2> /dev/null || true docker rm -f -v "$NAME-app" 2> /dev/null || true docker network disconnect "$NAME" "$(hostname)" 2> /dev/null || true docker network rm "$NAME" 2> /dev/null || true docker image rm "$IMAGE:$VERSION" "$IMAGE:latest" 2> /dev/null || true docker logout "$REGISTRY_HOST" || true