Retry 1306
Some checks failed
Build and Deploy / remote-build-and-deploy (push) Failing after 20s

This commit is contained in:
2025-06-17 13:06:10 -03:00
parent adef45c466
commit cd7b51b29a

View File

@@ -6,10 +6,7 @@ on:
- main - main
jobs: jobs:
# =================================================================== remote-build-and-deploy:
# UN ÚNICO JOB PARA CONSTRUIR, GUARDAR Y DESPLEGAR
# ===================================================================
build-and-deploy:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
@@ -17,7 +14,7 @@ jobs:
run: | run: |
set -e # Fallar inmediatamente si algo sale mal 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..." echo "Preparing SSH client..."
apt-get update && apt-get install -y openssh-client apt-get update && apt-get install -y openssh-client
mkdir -p ~/.ssh mkdir -p ~/.ssh
@@ -27,58 +24,65 @@ jobs:
# 2. Conectarse al HOST y ejecutar todo el proceso allí # 2. Conectarse al HOST y ejecutar todo el proceso allí
echo "Connecting to host to execute entire CI/CD process..." echo "Connecting to host to execute entire CI/CD process..."
ssh ${{ secrets.PROD_SERVER_USER }}@${{ secrets.PROD_SERVER_HOST }} 'bash -s' << 'EOF' # Pasamos las variables como argumentos a 'bash -s'.
set -e # También aquí, para fallar en la sesión remota # 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) --- # --- PARTE 1: PREPARACIÓN (EN EL HOST) ---
echo "--- (HOST) Preparing temporary workspace ---" echo "--- (HOST) Preparing temporary workspace ---"
# El runner de Gitea pasa estas variables de entorno a la sesión SSH # Leemos los argumentos que pasamos desde el comando ssh
# por lo que podemos usarlas directamente aquí. REPO_URL="$1"
TEMP_DIR="/tmp/gitea-build/${GITEA_RUN_ID}" COMMIT_SHA="$2"
REPO_NAME=$(echo "$GITEA_REPOSITORY" | tr '[:upper:]' '[:lower:]') RUN_ID="$3"
REPO_NAME_RAW="$4"
DB_PASSWORD="$5"
JWT_KEY="$6"
echo "Cloning repository ${GITEA_REMOTE_URL}..." # Convertimos a minúsculas usando shell
rm -rf $TEMP_DIR REPO_NAME=$(echo "$REPO_NAME_RAW" | tr '[:upper:]' '[:lower:]')
git clone --branch main --single-branch "${GITEA_REMOTE_URL}" $TEMP_DIR TEMP_DIR="/tmp/gitea-build/$RUN_ID"
cd $TEMP_DIR
git checkout "${GITEA_SHA}"
# --- PARTE 2: CONSTRUIR IMÁGENES CON KANIKO (EN EL HOST) --- echo "Cloning repository $REPO_URL..."
echo "--- (HOST) Building images... ---" rm -rf $TEMP_DIR
git clone --branch main --single-branch "$REPO_URL" $TEMP_DIR
cd $TEMP_DIR
git checkout "$COMMIT_SHA"
# Backend # --- PARTE 2: CONSTRUIR IMÁGENES CON KANIKO (EN EL HOST) ---
docker run --rm -v "$(pwd)":/workspace gcr.io/kaniko-project/executor:v1.9.0 \ echo "--- (HOST) Building images... ---"
--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 \
docker run --rm -v "$(pwd)":/workspace gcr.io/kaniko-project/executor:v1.9.0 \ --context=/workspace --dockerfile=/workspace/Backend/GestionIntegral.Api/Dockerfile --no-push \
--context=/workspace \ --destination=${REPO_NAME}-backend:latest --tarPath=/workspace/backend.tar
--dockerfile=/workspace/Frontend/Dockerfile \
--no-push \
--destination=${REPO_NAME}-frontend:latest \
--tarPath=/workspace/frontend.tar
# --- PARTE 3: DESPLEGAR (EN EL HOST) --- docker run --rm -v "$(pwd)":/workspace gcr.io/kaniko-project/executor:v1.9.0 \
echo "--- (HOST) Loading images and deploying... ---" --context=/workspace --dockerfile=/workspace/Frontend/Dockerfile --no-push \
--destination=${REPO_NAME}-frontend:latest --tarPath=/workspace/frontend.tar
docker load < backend.tar # --- PARTE 3: DESPLEGAR (EN EL HOST) ---
docker load < frontend.tar echo "--- (HOST) Loading images and deploying... ---"
cd /opt/gestion-integral docker load < backend.tar
# Los secretos no se pasan como variables de entorno, así que los inyectamos explícitamente. docker load < frontend.tar
export DB_SA_PASSWORD="${{ secrets.DB_SA_PASSWORD_SECRET }}"
export JWT_KEY="${{ secrets.JWT_KEY_SECRET }}"
docker compose up -d
# --- PARTE 4: LIMPIEZA (EN EL HOST) --- cd /opt/gestion-integral
echo "--- (HOST) Cleaning up... ---" export DB_SA_PASSWORD="$DB_PASSWORD"
rm -rf $TEMP_DIR export JWT_KEY="$JWT_KEY"
docker image prune -af docker compose up -d
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 EOF