diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 00bc2a4..894b60c 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -6,89 +6,73 @@ on: - main jobs: - # =================================================================== - # UN ÚNICO JOB PARA CONSTRUIR, GUARDAR Y DESPLEGAR - # =================================================================== - build-and-deploy: + # Solo tenemos un job que lo hace todo, remotamente. + remote-build-and-deploy: runs-on: ubuntu-latest steps: - # ----------------------------------------------------------------- - # PASO 1: PREPARACIÓN DEL ENTORNO DEL JOB - # ----------------------------------------------------------------- - - name: Checkout code - uses: actions/checkout@v3 + - name: Run Entire CI/CD Process on Host via SSH + run: | + set -e # Fallar inmediatamente si algo sale mal - - name: Install necessary tools - run: | - set -e - # 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..." + # 1. Preparar el cliente SSH dentro del contenedor del job (esto es lo único que hacemos aquí) + echo "Preparing SSH client..." + apt-get update && apt-get install -y openssh-client git 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 "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..." + # 2. Conectarse al HOST y ejecutar todo el proceso allí + echo "Connecting to host to execute entire CI/CD process..." ssh ${{ secrets.PROD_SERVER_USER }}@${{ secrets.PROD_SERVER_HOST }} << 'EOF' - set -e - echo "--- CONECTADO AL SERVIDOR DE PRODUCCIÓN ---" - cd /opt/gestion-integral + set -e # También aquí, para fallar en la sesión remota + + # --- PARTE 1: PREPARACIÓN (EN EL HOST) --- + echo "--- (HOST) Preparing temporary workspace ---" + REPO_URL="${{ gitea.remotes.origin.url }}" + COMMIT_SHA="${{ gitea.sha }}" + TEMP_DIR="/tmp/gitea-build/${{ gitea.run_id }}" + + rm -rf $TEMP_DIR + mkdir -p $TEMP_DIR + git clone --branch main --single-branch $REPO_URL $TEMP_DIR + cd $TEMP_DIR + git checkout $COMMIT_SHA + + # --- PARTE 2: CONSTRUIR IMÁGENES CON KANIKO (EN EL HOST) --- + echo "--- (HOST) Building images... ---" + + # Backend + docker run --rm -v "$(pwd)":/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 + + # Frontend + docker run --rm -v "$(pwd)":/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 + + # --- PARTE 3: DESPLEGAR (EN EL HOST) --- + echo "--- (HOST) Loading images and deploying... ---" - echo "Loading images into Docker..." docker load < backend.tar docker load < frontend.tar - echo "Starting application stack..." + cd /opt/gestion-integral export DB_SA_PASSWORD="${{ secrets.DB_SA_PASSWORD_SECRET }}" export JWT_KEY="${{ secrets.JWT_KEY_SECRET }}" docker compose up -d - echo "Cleaning up tar files and old images..." - rm backend.tar frontend.tar + # --- PARTE 4: LIMPIEZA (EN EL HOST) --- + echo "--- (HOST) Cleaning up... ---" + rm -rf $TEMP_DIR docker image prune -af echo "--- ¡¡DESPLIEGUE COMPLETADO Y VERIFICADO!! ---"