From c45e09e7133dcea15db055cef314477355f16df1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Rossi?= Date: Thu, 1 Oct 2026 11:05:10 +0200 Subject: [PATCH] added workflow to define a product version --- .gitea/workflows/deploy-env-dns.yml | 10 +- .gitea/workflows/product-version.yml | 159 +++++++++++++++++++++++++++ 2 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 .gitea/workflows/product-version.yml diff --git a/.gitea/workflows/deploy-env-dns.yml b/.gitea/workflows/deploy-env-dns.yml index 1227264..79ef198 100644 --- a/.gitea/workflows/deploy-env-dns.yml +++ b/.gitea/workflows/deploy-env-dns.yml @@ -1,7 +1,9 @@ # ═══════════════════════════════════════════════════════════════════ # Secret richiesti (a livello repo/org, ereditati con `secrets: inherit`): # DEPLOY_SSH_KEY, MAVEN_USERNAME, MAVEN_PASSWORD -# INFISICAL_CLIENT_ID, INFISICAL_CLIENT_SECRET, INFISICAL_PROJECT_ID +# INFISICAL_CLIENT_ID, INFISICAL_CLIENT_SECRET +# Variables richieste: DEPLOY_USER, DEPLOY_BASE_PATH, DOCKER_REGISTRY, +# INFISICAL_HOST, INFISICAL_PROJECT_ID # # INFISICAL: i secret applicativi vengono iniettati a runtime dalla CLI # infisical installata SUL SERVER di deploy (nessun file di secret viene @@ -46,6 +48,11 @@ on: required: false type: string default: "180" + ref: + description: "Ref/SHA da deployare (default: il commit che ha avviato il workflow). Usato es. dopo product-version.yml" + required: false + type: string + default: "" jobs: deploy: @@ -59,6 +66,7 @@ jobs: steps: - uses: actions/checkout@v6 with: + ref: ${{ inputs.ref }} fetch-depth: 0 submodules: recursive diff --git a/.gitea/workflows/product-version.yml b/.gitea/workflows/product-version.yml new file mode 100644 index 0000000..48238bf --- /dev/null +++ b/.gitea/workflows/product-version.yml @@ -0,0 +1,159 @@ +# ═══════════════════════════════════════════════════════════════════ +# Calcola la versione di prodotto di un deploy e la scrive in +# PRODUCT_VERSION del file .env_ del repo chiamante. +# +# Formato: " - YY.MM.DD." (prod) +# "TEST - - YY.MM.DD." (test) +# = ultimo commit del branch che non sia un commit di versione +# = contatore giornaliero CONDIVISO tra ambienti, ricavato dai +# tag git YY.MM.DD.N (prossimo numero libero del giorno) +# +# Effetti sul repo chiamante: +# - commit "chore(version): ... [skip ci]" su (il [skip ci] +# evita che il push riattivi le pipeline di deploy on-push) +# - tag annotato YY.MM.DD. sul commit di versione +# - release Gitea sul tag (prerelease per gli ambienti != prod) +# +# Il chiamante deve deployare l'output `commit-sha` (es. input `ref` di +# deploy-env-dns.yml), altrimenti il .env deployato non contiene la versione. +# +# Secret richiesti (ereditati con `secrets: inherit`): REPO_ACCESS_TOKEN +# ═══════════════════════════════════════════════════════════════════ +name: Product Version (reusable) + +on: + workflow_call: + inputs: + environment: + description: "Nome ambiente (test, prod, ...): aggiorna .env_" + required: true + type: string + branch: + description: "Branch su cui committare la versione" + required: false + type: string + default: master + outputs: + version: + description: "Valore di PRODUCT_VERSION (es. 'TEST - abc1234 - 26.10.01.3')" + value: ${{ jobs.version.outputs.version }} + tag: + description: "Tag creato (es. '26.10.01.3')" + value: ${{ jobs.version.outputs.tag }} + commit-sha: + description: "SHA del commit di versione, da deployare" + value: ${{ jobs.version.outputs.commit-sha }} + +jobs: + version: + runs-on: ubuntu-latest + env: + ENV_NAME: ${{ inputs.environment }} + BRANCH: ${{ inputs.branch }} + GITEA_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }} + outputs: + version: ${{ steps.version.outputs.VERSION }} + tag: ${{ steps.version.outputs.TAG }} + commit-sha: ${{ steps.version.outputs.COMMIT_SHA }} + steps: + - name: Calcola, committa e tagga la versione + id: version + run: | + set -euo pipefail + if [ -z "${GITEA_TOKEN:-}" ]; then + echo "::error::REPO_ACCESS_TOKEN non configurato: impossibile committare la versione" + exit 1 + fi + + ENV_FILE=".env_${ENV_NAME}" + PREFIX="" + [ "$ENV_NAME" = "test" ] && PREFIX="TEST - " + + git clone --branch "$BRANCH" \ + "https://biters.gitea:${GITEA_TOKEN}@git.biters.it/${{ github.repository }}.git" repo + cd repo + git config user.name "CI Release Bot" + git config user.email "ci@biters.it" + + if ! grep -q '^PRODUCT_VERSION=' "$ENV_FILE" 2>/dev/null; then + echo "::error::${ENV_FILE} assente o senza riga PRODUCT_VERSION=" + exit 1 + fi + + PUSHED="" + for attempt in 1 2 3; do + git fetch origin "$BRANCH" + git fetch --tags --force origin + git reset --hard "origin/${BRANCH}" + + # Revision = ultimo commit "reale": un redeploy senza modifiche non + # deve usare come revision il commit di versione precedente. + REV=$(git log -1 --format=%h --abbrev=7 --invert-grep --grep='^chore(version):') + TODAY=$(TZ=Europe/Rome date +%y.%m.%d) + LAST=$(git tag -l "${TODAY}.*" | sed "s/^${TODAY}\.//" | grep -E '^[0-9]+$' | sort -n | tail -1 || true) + BUILD=$(( ${LAST:-0} + 1 )) + TAG="${TODAY}.${BUILD}" + VERSION="${PREFIX}${REV} - ${TAG}" + + sed -i "s|^PRODUCT_VERSION=.*|PRODUCT_VERSION=\"${VERSION}\"|" "$ENV_FILE" + git commit -am "chore(version): ${ENV_NAME} PRODUCT_VERSION ${VERSION} [skip ci]" + git tag -a "$TAG" -m "${ENV_NAME} ${VERSION}" + + # Commit e tag insieme: se il branch e' avanzato o il tag e' stato + # preso da un deploy concorrente fallisce tutto e si ricalcola. + if git push --atomic origin "HEAD:refs/heads/${BRANCH}" "refs/tags/${TAG}"; then + PUSHED=1 + break + fi + echo "::warning::push fallito (tentativo ${attempt}/3), ricalcolo la versione" + git tag -d "$TAG" + sleep $(( attempt * 3 )) + done + + if [ -z "$PUSHED" ]; then + echo "::error::impossibile pushare commit e tag di versione dopo 3 tentativi" + exit 1 + fi + + COMMIT_SHA=$(git rev-parse HEAD) + echo "PRODUCT_VERSION=${VERSION} (tag ${TAG}, commit ${COMMIT_SHA})" + { + echo "VERSION=${VERSION}" + echo "TAG=${TAG}" + echo "REV=${REV}" + echo "PREFIX=${PREFIX}" + echo "COMMIT_SHA=${COMMIT_SHA}" + } >> "$GITHUB_OUTPUT" + + # ── Gitea Release ────────────────────────────────────────── + # Non bloccante: commit e tag esistono gia', il deploy puo' procedere. + - name: Crea release su Gitea + env: + VERSION: ${{ steps.version.outputs.VERSION }} + TAG: ${{ steps.version.outputs.TAG }} + REV: ${{ steps.version.outputs.REV }} + PREFIX: ${{ steps.version.outputs.PREFIX }} + run: | + PRERELEASE=true + [ "$ENV_NAME" = "prod" ] && PRERELEASE=false + RUN_URL="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" + + BODY="- **Ambiente**: ${ENV_NAME} + - **PRODUCT_VERSION**: \`${VERSION}\` + - **Revision**: \`${REV}\` + - **Pipeline**: ${RUN_URL}" + + if ! curl -sSf -X POST \ + -H "Authorization: token ${GITEA_TOKEN}" \ + -H "Content-Type: application/json" \ + "https://git.biters.it/api/v1/repos/${{ github.repository }}/releases" \ + -d "$(jq -n \ + --arg tag "$TAG" \ + --arg target "$BRANCH" \ + --arg name "${PREFIX}${TAG}" \ + --arg body "$BODY" \ + --argjson pre "$PRERELEASE" \ + '{tag_name: $tag, target_commitish: $target, name: $name, body: $body, draft: false, prerelease: $pre}')" \ + > /dev/null; then + echo "::warning::creazione release Gitea fallita per il tag ${TAG}" + fi