diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index aaeab73..0f4d53b 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -6,10 +6,7 @@ on: - main jobs: - # =================================================================== - # UN ÚNICO JOB PARA CONSTRUIR, GUARDAR Y DESPLEGAR - # =================================================================== - build-and-deploy: + remote-build-and-deploy: runs-on: ubuntu-latest steps: @@ -17,7 +14,7 @@ jobs: run: | set -e # Fallar inmediatamente si algo sale mal - # 1. Preparar el cliente SSH (lo único que hacemos en este contenedor) + # 1. Preparar el cliente SSH echo "Preparing SSH client..." apt-get update && apt-get install -y openssh-client mkdir -p ~/.ssh @@ -27,58 +24,65 @@ jobs: # 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 }} 'bash -s' << 'EOF' - set -e # También aquí, para fallar en la sesión remota + # Pasamos las variables como argumentos a 'bash -s'. + # El '$0', '$1', etc. dentro del script se referirán a estos. + ssh ${{ secrets.PROD_SERVER_USER }}@${{ secrets.PROD_SERVER_HOST }} 'bash -s' \ + "${{ gitea.remotes.origin.url }}" \ + "${{ gitea.sha }}" \ + "${{ gitea.run_id }}" \ + "${{ gitea.repository }}" \ + '${{ secrets.DB_SA_PASSWORD_SECRET }}' \ + '${{ secrets.JWT_KEY_SECRET }}' \ + << 'EOF' + set -e # También aquí, para fallar en la sesión remota - # --- PARTE 1: PREPARACIÓN (EN EL HOST) --- - echo "--- (HOST) Preparing temporary workspace ---" - - # El runner de Gitea pasa estas variables de entorno a la sesión SSH - # por lo que podemos usarlas directamente aquí. - TEMP_DIR="/tmp/gitea-build/${GITEA_RUN_ID}" - REPO_NAME=$(echo "$GITEA_REPOSITORY" | tr '[:upper:]' '[:lower:]') + # --- PARTE 1: PREPARACIÓN (EN EL HOST) --- + echo "--- (HOST) Preparing temporary workspace ---" + + # Leemos los argumentos que pasamos desde el comando ssh + REPO_URL="$1" + COMMIT_SHA="$2" + RUN_ID="$3" + REPO_NAME_RAW="$4" + DB_PASSWORD="$5" + JWT_KEY="$6" - echo "Cloning repository ${GITEA_REMOTE_URL}..." - rm -rf $TEMP_DIR - git clone --branch main --single-branch "${GITEA_REMOTE_URL}" $TEMP_DIR - cd $TEMP_DIR - git checkout "${GITEA_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=${REPO_NAME}-backend:latest \ - --tarPath=/workspace/backend.tar + # Convertimos a minúsculas usando shell + REPO_NAME=$(echo "$REPO_NAME_RAW" | tr '[:upper:]' '[:lower:]') + TEMP_DIR="/tmp/gitea-build/$RUN_ID" + + echo "Cloning repository $REPO_URL..." + rm -rf $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... ---" + + 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 - # Frontend - 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: DESPLEGAR (EN EL HOST) --- - echo "--- (HOST) Loading images and deploying... ---" - - docker load < backend.tar - docker load < frontend.tar - - cd /opt/gestion-integral - # Los secretos no se pasan como variables de entorno, así que los inyectamos explícitamente. - export DB_SA_PASSWORD="${{ secrets.DB_SA_PASSWORD_SECRET }}" - export JWT_KEY="${{ secrets.JWT_KEY_SECRET }}" - 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 + 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 + docker image prune -af + + echo "--- ¡¡DESPLIEGUE COMPLETADO Y VERIFICADO!! ---" EOF \ No newline at end of file