diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index bac0593..d146498 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -6,67 +6,86 @@ on: - main jobs: - remote-build-and-deploy: + # =================================================================== + # UN ÚNICO JOB PARA CONSTRUIR, GUARDAR, COPIAR Y DESPLEGAR + # =================================================================== + build-and-deploy: runs-on: ubuntu-latest steps: - - name: Run Entire CI/CD Process on Host via SSH + # ----------------------------------------------------------------- + # PASO 1: Checkout + # ----------------------------------------------------------------- + - name: Checkout code + uses: actions/checkout@v3 + + # ----------------------------------------------------------------- + # PASO 2: Construir imágenes con Kaniko y guardarlas como .tar + # ----------------------------------------------------------------- + - name: Build Backend & Frontend Images run: | set -e + echo "Building backend image..." + 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.repository_owner}/${gitea.repository_name}-backend:latest \ + --tarPath=/workspace/backend.tar + + echo "Building frontend image..." + 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.repository_owner}/${gitea.repository_name}-frontend:latest \ + --tarPath=/workspace/frontend.tar - # 1. Preparar el cliente SSH (sin cambios) - apt-get update > /dev/null && apt-get install -y openssh-client git > /dev/null + # ----------------------------------------------------------------- + # PASO 3: Preparar archivos de configuración y despliegue + # ----------------------------------------------------------------- + - name: Prepare Deployment Files + run: | + set -e + echo "Creating .env file for production..." + # Creamos el fichero .env en el workspace del job + echo "DB_SA_PASSWORD=${{ secrets.DB_SA_PASSWORD_SECRET }}" > ${{ gitea.workspace }}/.env + echo "JWT_KEY=${{ secrets.JWT_KEY_SECRET }}" >> ${{ gitea.workspace }}/.env + + echo "Preparing SSH client..." + apt-get update > /dev/null && apt-get install -y openssh-client > /dev/null 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 - - # 2. Conectarse al HOST y ejecutar todo el proceso allí - ssh ${{ secrets.PROD_SERVER_USER }}@${{ secrets.PROD_SERVER_HOST }} << EOF + + # ----------------------------------------------------------------- + # PASO 4: Copiar archivos al servidor y ejecutar despliegue + # ----------------------------------------------------------------- + - name: Copy Files and Deploy + run: | + set -e + echo "Copying image and .env files to production server..." + # Copiamos los .tar Y el .env al host + scp \ + ${{ gitea.workspace }}/backend.tar \ + ${{ gitea.workspace }}/frontend.tar \ + ${{ gitea.workspace }}/.env \ + ${{ secrets.PROD_SERVER_USER }}@${{ secrets.PROD_SERVER_HOST }}:/opt/gestion-integral/ + + echo "Connecting to host to load images and deploy..." + # El script remoto es ahora muy simple y robusto + ssh ${{ secrets.PROD_SERVER_USER }}@${{ secrets.PROD_SERVER_HOST }} << 'EOF' set -e - - # --- PARTE 1 Y 2: PREPARACIÓN Y BUILD (SIN CAMBIOS) --- - echo "--- (HOST) Preparing temporary workspace & Building... ---" - TEMP_DIR="/tmp/gitea-build/${{ gitea.run_id }}" - REPO_NAME_RAW="${{ gitea.repository }}" - REPO_NAME=\$(echo "\$REPO_NAME_RAW" | tr '[:upper:]' '[:lower:]') - GITEA_REPO_PATH="/var/lib/docker/volumes/gitea-stack_gitea-data/_data/git/repositories/\${REPO_NAME}.git" - - git config --global --add safe.directory "\$GITEA_REPO_PATH" - rm -rf \$TEMP_DIR - git clone \$GITEA_REPO_PATH \$TEMP_DIR - cd \$TEMP_DIR - git checkout "${{ gitea.sha }}" - - 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=\${REPO_NAME}-backend:latest --tarPath=/workspace/backend.tar - docker run --rm -v "\$(pwd)":/workspace gcr.io/kaniko-project/executor:v1.9.0 \ - --context=/workspace --dockerfile=/workspace/Frontend/Dockerfile --no-push \ - --destination=\${REPO_NAME}-frontend:latest --tarPath=/workspace/frontend.tar - - # --- PARTE 3: DESPLEGAR (LÓGICA CORREGIDA) --- - echo "--- (HOST) Loading images and deploying... ---" + echo "--- (HOST) CONECTADO ---" + cd /opt/gestion-integral + echo "Loading images into Docker..." docker load < backend.tar docker load < frontend.tar - cd /opt/gestion-integral - - # --- ¡¡LA CORRECCIÓN ESTÁ AQUÍ!! --- - # Creamos el archivo .env con comandos 'echo'. Es más robusto. - echo "Creating temporary .env file..." - echo "DB_SA_PASSWORD=${{ secrets.DB_SA_PASSWORD_SECRET }}" > .env - echo "JWT_KEY=${{ secrets.JWT_KEY_SECRET }}" >> .env - - # docker-compose leerá automáticamente el archivo .env - echo "Starting application stack..." + echo "Starting application stack (will use .env file)..." + # docker-compose usará el .env automáticamente docker compose up -d - # --- PARTE 4: LIMPIEZA --- - echo "--- (HOST) Cleaning up... ---" - rm -rf \$TEMP_DIR - rm .env + echo "Cleaning up temporary files..." + rm backend.tar frontend.tar .env docker image prune -af echo "--- ¡¡DESPLIEGUE COMPLETADO Y VERIFICADO!! ---"