diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 2cd0708..0c111d4 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -14,74 +14,65 @@ jobs: run: | set -e - # 1. Preparar el cliente SSH + # 1. Preparar cliente SSH. apt-get update > /dev/null && apt-get install -y openssh-client git > /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í - # Pasamos las variables como argumentos a 'bash -s'. - # Esto es seguro y evita problemas de expansión. - ssh ${{ secrets.PROD_SERVER_USER }}@${{ secrets.PROD_SERVER_HOST }} 'bash -s' \ - "${{ gitea.sha }}" \ - "${{ gitea.run_id }}" \ - "${{ gitea.repository }}" \ - '${{ secrets.DB_SA_PASSWORD_SECRET }}' \ - '${{ secrets.JWT_KEY_SECRET }}' \ - << 'EOF' - set -e + # 2. Conectarse al HOST y ejecutar todo. + # Usamos << 'EOF' para evitar la expansión de variables en el cliente. + ssh ${{ secrets.PROD_SERVER_USER }}@${{ secrets.PROD_SERVER_HOST }} << 'EOF' + set -e - # --- PARTE 1: PREPARACIÓN (EN EL HOST) --- - echo "--- (HOST) Preparing temporary workspace ---" - - # Leemos los argumentos que pasamos desde el comando ssh - COMMIT_SHA="$1" - RUN_ID="$2" - REPO_NAME_RAW="$3" - DB_PASSWORD="$4" - JWT_KEY="$5" + # --- PARTE 1: PREPARACIÓN (EN EL HOST) --- + echo "--- (HOST) Preparing temporary workspace ---" + + 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" + + echo "Cloning repository from local path: $GITEA_REPO_PATH" + rm -rf $TEMP_DIR + git clone $GITEA_REPO_PATH $TEMP_DIR + cd $TEMP_DIR + git checkout "${GITEA_SHA}" + + # --- PARTE 2: CONSTRUIR IMÁGENES CON KANIKO (EN EL HOST) --- + echo "--- (HOST) Building images... ---" + + 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 - REPO_NAME=$(echo "$REPO_NAME_RAW" | tr '[:upper:]' '[:lower:]') - TEMP_DIR="/tmp/gitea-build/$RUN_ID" - 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" - - echo "Cloning repository from local path: $GITEA_REPO_PATH" - rm -rf $TEMP_DIR - git clone $GITEA_REPO_PATH $TEMP_DIR - cd $TEMP_DIR - git checkout "$COMMIT_SHA" - - # --- PARTE 2: CONSTRUIR IMÁGENES CON KANIKO (EN EL HOST) --- - echo "--- (HOST) Building images... ---" - - 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 - 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: PREPARAR Y DESPLEGAR (EN EL HOST) --- + echo "--- (HOST) Preparing deployment ---" + + # ¡¡LA SOLUCIÓN!! Creamos el .env línea por línea. + # El > sobrescribe el archivo, el >> añade al final. Es explícito e infalible. + echo "DB_SA_PASSWORD=${DB_SA_PASSWORD_SECRET}" > /opt/gestion-integral/.env + echo "JWT_KEY=${JWT_KEY_SECRET}" >> /opt/gestion-integral/.env + + echo "Loading images and deploying..." + docker load < backend.tar + docker load < frontend.tar + + cd /opt/gestion-integral + docker compose up -d - # --- PARTE 3: DESPLEGAR (EN EL HOST) --- - echo "--- (HOST) Loading images and deploying... ---" - - docker load < backend.tar - docker load < frontend.tar - - cd /opt/gestion-integral - # Usamos las variables leídas de los argumentos - export DB_SA_PASSWORD="$DB_PASSWORD" - export JWT_KEY="$JWT_KEY" - docker compose up -d - - # --- PARTE 4: LIMPIEZA (EN EL HOST) --- - echo "--- (HOST) Cleaning up... ---" - rm -rf $TEMP_DIR - docker image prune -af - - echo "--- ¡¡DESPLIEGUE COMPLETADO Y VERIFICADO!! ---" + # --- PARTE 4: LIMPIEZA (EN EL HOST) --- + echo "--- (HOST) Cleaning up... ---" + rm -rf $TEMP_DIR + rm /opt/gestion-integral/.env # Limpiamos el .env por seguridad. + docker image prune -af + + echo "--- ¡¡DESPLIEGUE COMPLETADO Y VERIFICADO!! ---" EOF \ No newline at end of file