From 20d04b947b5cf6a9acfcdf756ea4bef2602e98d1 Mon Sep 17 00:00:00 2001 From: dmolinari Date: Tue, 17 Jun 2025 12:56:18 -0300 Subject: [PATCH] Retry 1256 --- .gitea/workflows/deploy.yml | 95 +++++++++++++++++++++++-------------- 1 file changed, 60 insertions(+), 35 deletions(-) diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 8e34fe5..00bc2a4 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -6,64 +6,89 @@ on: - main jobs: + # =================================================================== + # UN ÚNICO JOB PARA CONSTRUIR, GUARDAR Y DESPLEGAR + # =================================================================== build-and-deploy: runs-on: ubuntu-latest steps: + # ----------------------------------------------------------------- + # PASO 1: PREPARACIÓN DEL ENTORNO DEL JOB + # ----------------------------------------------------------------- - name: Checkout code uses: actions/checkout@v3 - # Ya no necesitamos crear config.json. La acción de login lo maneja. - - name: Login to Gitea Registry - uses: docker/login-action@v2 - with: - registry: 127.0.0.1:5000 - username: ${{ secrets.REGISTRY_USER }} - password: ${{ secrets.ACTIONS_PAT }} - - - name: Build and Push Backend - uses: docker/build-push-action@v4 - with: - context: . - file: Backend/GestionIntegral.Api/Dockerfile - push: true - # La imagen se etiqueta para el registro local - tags: 127.0.0.1:5000/dmolinari/gestionintegralweb-backend:latest,127.0.0.1:5000/dmolinari/gestionintegralweb-backend:${{ gitea.sha_short }} - - - name: Build and Push Frontend - uses: docker/build-push-action@v4 - with: - context: . - file: Frontend/Dockerfile - push: true - tags: 127.0.0.1:5000/dmolinari/gestionintegralweb-frontend:latest,127.0.0.1:5000/dmolinari/gestionintegralweb-frontend:${{ gitea.sha_short }} - - # El despliegue ahora es un paso final en el mismo job - - name: Deploy to Production via SSH + - name: Install necessary tools run: | set -e - echo "Preparing SSH client..." + # Instala el cliente SSH para el paso de despliegue apt-get update && apt-get install -y openssh-client + + # ----------------------------------------------------------------- + # PASO 2: CONSTRUIR IMÁGENES CON KANIKO Y GUARDARLAS + # ----------------------------------------------------------------- + - name: Build Backend Image and Save as Tar + run: | + set -e + # Ejecutamos Kaniko y le decimos que guarde el .tar en el workspace + docker run --rm -v ${{ gitea.workspace }}:/workspace \ + gcr.io/kaniko-project/executor:v1.9.0 \ + --context=/workspace \ + --dockerfile=/workspace/Backend/GestionIntegral.Api/Dockerfile \ + --no-push \ + --destination=${{ gitea.actor }}/${{ toLower(gitea.repository_name) }}-backend:latest \ + --tarPath=/workspace/backend.tar + + - name: Build Frontend Image and Save as Tar + run: | + set -e + docker run --rm -v ${{ gitea.workspace }}:/workspace \ + gcr.io/kaniko-project/executor:v1.9.0 \ + --context=/workspace \ + --dockerfile=/workspace/Frontend/Dockerfile \ + --no-push \ + --destination=${{ gitea.actor }}/${{ toLower(gitea.repository_name) }}-frontend:latest \ + --tarPath=/workspace/frontend.tar + + - name: Verify that image files exist + run: | + echo "--- Verifying contents of workspace ---" + ls -lh ${{ gitea.workspace }} + # Este paso nos dará la prueba definitiva de que los .tar existen + + # ----------------------------------------------------------------- + # PASO 3: DESPLEGAR (COPIAR FICHEROS, CARGAR IMÁGENES Y LEVANTAR) + # ----------------------------------------------------------------- + - name: Deploy to Production via SCP and SSH + run: | + set -e + echo "Preparing SSH credentials..." mkdir -p ~/.ssh echo "${{ secrets.PROD_SERVER_SSH_KEY }}" > ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa ssh-keyscan -H ${{ secrets.PROD_SERVER_HOST }} >> ~/.ssh/known_hosts - echo "Connecting to host to deploy..." + echo "Copying image files to production server..." + scp ${{ gitea.workspace }}/backend.tar ${{ gitea.workspace }}/frontend.tar ${{ secrets.PROD_SERVER_USER }}@${{ secrets.PROD_SERVER_HOST }}:/opt/gestion-integral/ + + echo "Connecting to host to load images and deploy..." ssh ${{ secrets.PROD_SERVER_USER }}@${{ secrets.PROD_SERVER_HOST }} << 'EOF' set -e echo "--- CONECTADO AL SERVIDOR DE PRODUCCIÓN ---" cd /opt/gestion-integral + echo "Loading images into Docker..." + docker load < backend.tar + docker load < frontend.tar + + echo "Starting application stack..." export DB_SA_PASSWORD="${{ secrets.DB_SA_PASSWORD_SECRET }}" export JWT_KEY="${{ secrets.JWT_KEY_SECRET }}" - - # El login ya no es necesario aquí, el demonio ya tiene la sesión - # del paso anterior del pipeline. Pero lo dejamos por robustez. - echo "${{ secrets.ACTIONS_PAT }}" | docker login 127.0.0.1:5000 -u ${{ secrets.REGISTRY_USER }} --password-stdin - - docker compose pull docker compose up -d + + echo "Cleaning up tar files and old images..." + rm backend.tar frontend.tar docker image prune -af echo "--- ¡¡DESPLIEGUE COMPLETADO Y VERIFICADO!! ---"