# ═══════════════════════════════════════════════════════════════════ # 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