name: Build and Push Frontend on: push: branches: - main paths: - 'workloads/ecommerce/index.html' - 'workloads/ecommerce/Dockerfile' jobs: build: runs-on: ubuntu-latest timeout-minutes: 20 steps: - name: Checkout del código uses: actions/checkout@v3 with: # Descarga el historial necesario para sincronizar origin/main. fetch-depth: 0 - name: Definir Versión Semántica id: vars 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 with: registry: gitea.cruzcloud.net username: ${{ secrets.REGISTRY_USER }} password: ${{ secrets.REGISTRY_PASSWORD }} - name: Construir y Subir Imagen uses: docker/build-push-action@v4 with: context: workloads/ecommerce/ push: true tags: | gitea.cruzcloud.net/devops/ecommerce-frontend:${{ steps.vars.outputs.VERSION }} 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: | 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