Some checks failed
Build and Deploy / remote-build-and-deploy (push) Failing after 20s
88 lines
3.5 KiB
YAML
88 lines
3.5 KiB
YAML
name: Build and Deploy
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- main
|
|
|
|
jobs:
|
|
remote-build-and-deploy:
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- name: Run Entire CI/CD Process on Host via SSH
|
|
run: |
|
|
set -e # Fallar inmediatamente si algo sale mal
|
|
|
|
# 1. Preparar el cliente SSH
|
|
echo "Preparing SSH client..."
|
|
apt-get update && apt-get install -y openssh-client
|
|
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í
|
|
echo "Connecting to host to execute entire CI/CD process..."
|
|
# 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 ---"
|
|
|
|
# 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"
|
|
|
|
# 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
|
|
|
|
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
|
|
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!! ---"
|
|
EOF |