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
|
- main
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
# ===================================================================
|
remote-build-and-deploy:
|
||||||
# UN ÚNICO JOB PARA CONSTRUIR, GUARDAR, COPIAR Y DESPLEGAR
|
|
||||||
# ===================================================================
|
|
||||||
build-and-deploy:
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
# -----------------------------------------------------------------
|
- name: Run Entire CI/CD Process on Host via SSH
|
||||||
# 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
|
|
||||||
run: |
|
run: |
|
||||||
set -e
|
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
|
|
||||||
|
|
||||||
# -----------------------------------------------------------------
|
# 1. Preparar el cliente SSH (sin cambios)
|
||||||
# PASO 3: Preparar archivos de configuración y despliegue
|
apt-get update > /dev/null && apt-get install -y openssh-client git > /dev/null
|
||||||
# -----------------------------------------------------------------
|
|
||||||
- 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
|
|
||||||
mkdir -p ~/.ssh
|
mkdir -p ~/.ssh
|
||||||
echo "${{ secrets.PROD_SERVER_SSH_KEY }}" > ~/.ssh/id_rsa
|
echo "${{ secrets.PROD_SERVER_SSH_KEY }}" > ~/.ssh/id_rsa
|
||||||
chmod 600 ~/.ssh/id_rsa
|
chmod 600 ~/.ssh/id_rsa
|
||||||
ssh-keyscan -H ${{ secrets.PROD_SERVER_HOST }} >> ~/.ssh/known_hosts
|
ssh-keyscan -H ${{ secrets.PROD_SERVER_HOST }} >> ~/.ssh/known_hosts
|
||||||
|
|
||||||
# -----------------------------------------------------------------
|
# 2. Conectarse al HOST y ejecutar el script.
|
||||||
# PASO 4: Copiar archivos al servidor y ejecutar despliegue
|
# ¡¡LA CLAVE ESTÁ AQUÍ!! << EOF sin comillas, y variables remotas escapadas.
|
||||||
# -----------------------------------------------------------------
|
ssh ${{ secrets.PROD_SERVER_USER }}@${{ secrets.PROD_SERVER_HOST }} << EOF
|
||||||
- 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'
|
|
||||||
set -e
|
set -e
|
||||||
echo "--- (HOST) CONECTADO ---"
|
|
||||||
cd /opt/gestion-integral
|
# --- 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... ---"
|
||||||
|
|
||||||
echo "Loading images into Docker..."
|
|
||||||
docker load < backend.tar
|
docker load < backend.tar
|
||||||
docker load < frontend.tar
|
docker load < frontend.tar
|
||||||
|
|
||||||
echo "Starting application stack (will use .env file)..."
|
cd /opt/gestion-integral
|
||||||
# docker-compose usará el .env automáticamente
|
# 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
|
docker compose up -d
|
||||||
|
|
||||||
echo "Cleaning up temporary files..."
|
# --- PARTE 4: LIMPIEZA (EN EL HOST) ---
|
||||||
rm backend.tar frontend.tar .env
|
echo "--- (HOST) Cleaning up... ---"
|
||||||
|
# Escapamos la variable para que la evalúe el host remoto
|
||||||
|
rm -rf \$TEMP_DIR
|
||||||
docker image prune -af
|
docker image prune -af
|
||||||
|
|
||||||
echo "--- ¡¡DESPLIEGUE COMPLETADO Y VERIFICADO!! ---"
|
echo "--- ¡¡DESPLIEGUE COMPLETADO Y VERIFICADO!! ---"
|
||||||
|
|||||||
Reference in New Issue
Block a user