From 976d3fc9d46e8afd4f1c8b6b15e51e655180e488 Mon Sep 17 00:00:00 2001 From: devops Date: Sun, 12 Jul 2026 07:38:03 +0000 Subject: [PATCH] Update .gitea/workflows/build.yaml --- .gitea/workflows/build.yaml | 95 ++++++++++++++++++++++++++++++------- 1 file changed, 79 insertions(+), 16 deletions(-) diff --git a/.gitea/workflows/build.yaml b/.gitea/workflows/build.yaml index a09693a..d89c212 100644 --- a/.gitea/workflows/build.yaml +++ b/.gitea/workflows/build.yaml @@ -11,15 +11,24 @@ on: jobs: build: runs-on: ubuntu-latest + timeout-minutes: 20 + steps: - name: Checkout del código uses: actions/checkout@v3 with: - fetch-depth: 0 # Clonar todo para evitar errores de historial + # Descarga el historial necesario para sincronizar origin/main. + fetch-depth: 0 - name: Definir Versión Semántica id: vars - run: echo "VERSION=v1.0.${{ github.run_number }}" >> $GITHUB_OUTPUT + shell: bash + run: | + set -euo pipefail + + VERSION="v1.0.${{ github.run_number }}" + echo "VERSION=${VERSION}" >> "$GITHUB_OUTPUT" + echo "Versión generada: ${VERSION}" - name: Login en Gitea Registry uses: docker/login-action@v2 @@ -38,18 +47,72 @@ jobs: gitea.cruzcloud.net/devops/ecommerce-frontend:latest - name: Actualizar Manifiesto GitOps (CD) + shell: bash + env: + VERSION: ${{ steps.vars.outputs.VERSION }} + MANIFEST: workloads/ecommerce/frontend.yaml + IMAGE_REPOSITORY: gitea.cruzcloud.net/devops/ecommerce-frontend run: | - # 1. Configuración de identidad - git config --global user.name "Gitea Actions" - git config --global user.email "actions@gitea.cruzcloud.net" - - # 2. Sincronización agresiva: asegurar que estamos en la rama correcta - git checkout main - - # 3. Aplicar el cambio - sed -i -E "s|(image: gitea.cruzcloud.net/devops/ecommerce-frontend:).*|\1${{ steps.vars.outputs.VERSION }}|g" workloads/ecommerce/frontend.yaml - - # 4. Commit y Push forzado - git add workloads/ecommerce/frontend.yaml - git commit -m "chore: release ${{ steps.vars.outputs.VERSION }} [skip ci]" || exit 0 - git push origin main --force \ No newline at end of file + set -euo pipefail + + git config user.name "Cristian Felipe" + git config user.email "cristiancruz0529@gmail.com" + + # El repositorio remoto puede cambiar mientras se construye la imagen. + # Se reintenta desde el último origin/main sin usar push --force. + for intento in 1 2 3; do + echo "--------------------------------------------------" + echo "Intento ${intento}/3" + echo "Versión: ${VERSION}" + echo "Manifiesto: ${MANIFEST}" + echo "--------------------------------------------------" + + git fetch origin main + git checkout -B main origin/main + + if [ ! -f "${MANIFEST}" ]; then + echo "ERROR: no existe el archivo ${MANIFEST}." + exit 1 + fi + + if ! grep -qE \ + "image:[[:space:]]*${IMAGE_REPOSITORY}:" \ + "${MANIFEST}"; then + echo "ERROR: no se encontró la imagen ${IMAGE_REPOSITORY} en ${MANIFEST}." + echo "Líneas image encontradas:" + grep -nE "^[[:space:]]*image:" "${MANIFEST}" || true + exit 1 + fi + + sed -i -E \ + "s|(image:[[:space:]]*${IMAGE_REPOSITORY}:).*|\\1${VERSION}|g" \ + "${MANIFEST}" + + echo "Imagen resultante en el manifiesto:" + grep -nE "^[[:space:]]*image:" "${MANIFEST}" || true + + git add "${MANIFEST}" + + if git diff --cached --quiet; then + echo "El manifiesto ya contiene la versión ${VERSION}." + exit 0 + fi + + git commit -m "chore: release ${VERSION} [skip ci]" + + if git push origin HEAD:main; then + echo "Manifiesto actualizado correctamente con ${VERSION}." + exit 0 + fi + + echo "origin/main cambió mientras se realizaba el push." + + if [ "${intento}" -lt 3 ]; then + espera=$((intento * 3)) + echo "Reintentando en ${espera} segundos..." + sleep "${espera}" + fi + done + + echo "ERROR: no fue posible actualizar main después de 3 intentos." + exit 1 \ No newline at end of file