diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 83af935..f32ff2f 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -21,49 +21,57 @@ jobs: 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' + # ¡¡CAMBIO CLAVE!! Usamos << EOF para que las variables de Gitea se expandan + ssh ${{ secrets.PROD_SERVER_USER }}@${{ secrets.PROD_SERVER_HOST }} << EOF set -e - # --- PARTE 1: PREPARACIÓN Y CONSTRUCCIÓN (SIN CAMBIOS) --- - echo "--- (HOST) Preparing and Building ---" - TEMP_DIR="/tmp/gitea-build/${GITEA_RUN_ID}" - REPO_NAME=$(echo "$GITEA_REPOSITORY" | tr '[:upper:]' '[:lower:]') - GITEA_REPO_PATH="/var/lib/docker/volumes/gitea-stack_gitea-data/_data/git/repositories/${REPO_NAME}.git" + # --- PARTE 1: PREPARACIÓN (EN EL HOST) --- + echo "--- (HOST) Preparing temporary workspace ---" - 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}" + # Usamos las variables que el runner de Gitea ya ha puesto en el entorno + REPO_NAME_LOWER=\$(echo "\$GITEA_REPOSITORY" | tr '[:upper:]' '[:lower:]') + TEMP_DIR="/tmp/gitea-build/\$GITEA_RUN_ID" + GITEA_REPO_PATH="/var/lib/docker/volumes/gitea-stack_gitea-data/_data/git/repositories/\${REPO_NAME_LOWER}.git" - 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 + echo "Adding git repository to safe directories..." + git config --global --add safe.directory "\$GITEA_REPO_PATH" - # --- PARTE 2: DESPLEGAR (LÓGICA NUEVA) --- - echo "--- (HOST) Loading images and preparing deployment ---" + 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_LOWER}-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_LOWER}-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 - - # ¡¡LA CORRECCIÓN CLAVE!! Creamos el archivo .env - echo "Creating .env file with secrets..." - rm -f .env - echo "DB_SA_PASSWORD=${{ secrets.DB_SA_PASSWORD_SECRET }}" >> .env + # Creamos el archivo .env con los secretos + echo "Creating .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 en este directorio + echo "Starting application stack..." docker compose up -d - # --- PARTE 3: LIMPIEZA --- + # --- PARTE 4: LIMPIEZA (EN EL HOST) --- echo "--- (HOST) Cleaning up... ---" - rm -rf $TEMP_DIR - rm .env # Limpiamos el archivo con los secretos + rm -rf \$TEMP_DIR + rm .env docker image prune -af echo "--- ¡¡DESPLIEGUE COMPLETADO Y VERIFICADO!! ---"