Retry .env 2239
All checks were successful
Build and Deploy / remote-build-and-deploy (push) Successful in 12m3s
All checks were successful
Build and Deploy / remote-build-and-deploy (push) Successful in 12m3s
This commit is contained in:
@@ -6,86 +6,71 @@ on:
|
||||
- main
|
||||
|
||||
jobs:
|
||||
# ===================================================================
|
||||
# UN ÚNICO JOB PARA CONSTRUIR, GUARDAR, COPIAR Y DESPLEGAR
|
||||
# ===================================================================
|
||||
build-and-deploy:
|
||||
remote-build-and-deploy:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
# -----------------------------------------------------------------
|
||||
# PASO 1: Checkout
|
||||
# -----------------------------------------------------------------
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v3
|
||||
|
||||
# -----------------------------------------------------------------
|
||||
# PASO 2: Construir imágenes con Kaniko y guardarlas como .tar
|
||||
# -----------------------------------------------------------------
|
||||
- name: Build Backend & Frontend Images
|
||||
- name: Run Entire CI/CD Process on Host via SSH
|
||||
run: |
|
||||
set -e
|
||||
echo "Building backend image..."
|
||||
docker run --rm -v ${{ gitea.workspace }}:/workspace gcr.io/kaniko-project/executor:v1.9.0 \
|
||||
--context=/workspace --dockerfile=/workspace/Backend/GestionIntegral.Api/Dockerfile --no-push \
|
||||
--destination=${gitea.repository_owner}/${gitea.repository_name}-backend:latest \
|
||||
--tarPath=/workspace/backend.tar
|
||||
|
||||
echo "Building frontend image..."
|
||||
docker run --rm -v ${{ gitea.workspace }}:/workspace gcr.io/kaniko-project/executor:v1.9.0 \
|
||||
--context=/workspace --dockerfile=/workspace/Frontend/Dockerfile --no-push \
|
||||
--destination=${gitea.repository_owner}/${gitea.repository_name}-frontend:latest \
|
||||
--tarPath=/workspace/frontend.tar
|
||||
|
||||
# -----------------------------------------------------------------
|
||||
# PASO 3: Preparar archivos de configuración y despliegue
|
||||
# -----------------------------------------------------------------
|
||||
- name: Prepare Deployment Files
|
||||
run: |
|
||||
set -e
|
||||
echo "Creating .env file for production..."
|
||||
# Creamos el fichero .env en el workspace del job
|
||||
echo "DB_SA_PASSWORD=${{ secrets.DB_SA_PASSWORD_SECRET }}" > ${{ gitea.workspace }}/.env
|
||||
echo "JWT_KEY=${{ secrets.JWT_KEY_SECRET }}" >> ${{ gitea.workspace }}/.env
|
||||
|
||||
echo "Preparing SSH client..."
|
||||
apt-get update > /dev/null && apt-get install -y openssh-client > /dev/null
|
||||
# 1. Preparar el cliente SSH (sin cambios)
|
||||
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
|
||||
|
||||
# -----------------------------------------------------------------
|
||||
# PASO 4: Copiar archivos al servidor y ejecutar despliegue
|
||||
# -----------------------------------------------------------------
|
||||
- name: Copy Files and Deploy
|
||||
run: |
|
||||
set -e
|
||||
echo "Copying image and .env files to production server..."
|
||||
# Copiamos los .tar Y el .env al host
|
||||
scp \
|
||||
${{ gitea.workspace }}/backend.tar \
|
||||
${{ gitea.workspace }}/frontend.tar \
|
||||
${{ gitea.workspace }}/.env \
|
||||
${{ secrets.PROD_SERVER_USER }}@${{ secrets.PROD_SERVER_HOST }}:/opt/gestion-integral/
|
||||
|
||||
echo "Connecting to host to load images and deploy..."
|
||||
# El script remoto es ahora muy simple y robusto
|
||||
ssh ${{ secrets.PROD_SERVER_USER }}@${{ secrets.PROD_SERVER_HOST }} << 'EOF'
|
||||
# 2. Conectarse al HOST y ejecutar el script.
|
||||
# ¡¡LA CLAVE ESTÁ AQUÍ!! << EOF sin comillas, y variables remotas escapadas.
|
||||
ssh ${{ secrets.PROD_SERVER_USER }}@${{ secrets.PROD_SERVER_HOST }} << EOF
|
||||
set -e
|
||||
echo "--- (HOST) CONECTADO ---"
|
||||
cd /opt/gestion-integral
|
||||
|
||||
echo "Loading images into Docker..."
|
||||
# --- PARTE 1: PREPARACIÓN (EN EL HOST) ---
|
||||
echo "--- (HOST) Preparing temporary workspace ---"
|
||||
|
||||
# Gitea reemplazará estas variables ANTES de enviar el script.
|
||||
TEMP_DIR="/tmp/gitea-build/${{ gitea.run_id }}"
|
||||
REPO_NAME_RAW="${{ gitea.repository }}"
|
||||
# El shell remoto ejecutará estos comandos. Escapamos los '$'.
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
echo "Starting application stack (will use .env file)..."
|
||||
# docker-compose usará el .env automáticamente
|
||||
cd /opt/gestion-integral
|
||||
# Gitea reemplaza los secretos aquí. Es seguro.
|
||||
export DB_SA_PASSWORD='${{ secrets.DB_SA_PASSWORD_SECRET }}'
|
||||
export JWT_KEY='${{ secrets.JWT_KEY_SECRET }}'
|
||||
docker compose up -d
|
||||
|
||||
echo "Cleaning up temporary files..."
|
||||
rm backend.tar frontend.tar .env
|
||||
# --- PARTE 4: LIMPIEZA (EN EL HOST) ---
|
||||
echo "--- (HOST) Cleaning up... ---"
|
||||
# Escapamos la variable para que la evalúe el host remoto
|
||||
rm -rf \$TEMP_DIR
|
||||
docker image prune -af
|
||||
|
||||
echo "--- ¡¡DESPLIEGUE COMPLETADO Y VERIFICADO!! ---"
|
||||
|
||||
Reference in New Issue
Block a user