Retry 1259
All checks were successful
Build and Deploy / remote-build-and-deploy (push) Successful in 15s
All checks were successful
Build and Deploy / remote-build-and-deploy (push) Successful in 15s
This commit is contained in:
@@ -6,89 +6,73 @@ on:
|
|||||||
- main
|
- main
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
# ===================================================================
|
# Solo tenemos un job que lo hace todo, remotamente.
|
||||||
# UN ÚNICO JOB PARA CONSTRUIR, GUARDAR Y DESPLEGAR
|
remote-build-and-deploy:
|
||||||
# ===================================================================
|
|
||||||
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: PREPARACIÓN DEL ENTORNO DEL JOB
|
run: |
|
||||||
# -----------------------------------------------------------------
|
set -e # Fallar inmediatamente si algo sale mal
|
||||||
- name: Checkout code
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
|
|
||||||
- name: Install necessary tools
|
# 1. Preparar el cliente SSH dentro del contenedor del job (esto es lo único que hacemos aquí)
|
||||||
run: |
|
echo "Preparing SSH client..."
|
||||||
set -e
|
apt-get update && apt-get install -y openssh-client git
|
||||||
# Instala el cliente SSH para el paso de despliegue
|
|
||||||
apt-get update && apt-get install -y openssh-client
|
|
||||||
|
|
||||||
# -----------------------------------------------------------------
|
|
||||||
# PASO 2: CONSTRUIR IMÁGENES CON KANIKO Y GUARDARLAS
|
|
||||||
# -----------------------------------------------------------------
|
|
||||||
- name: Build Backend Image and Save as Tar
|
|
||||||
run: |
|
|
||||||
set -e
|
|
||||||
# Ejecutamos Kaniko y le decimos que guarde el .tar en el workspace
|
|
||||||
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.actor }}/${{ toLower(gitea.repository_name) }}-backend:latest \
|
|
||||||
--tarPath=/workspace/backend.tar
|
|
||||||
|
|
||||||
- name: Build Frontend Image and Save as Tar
|
|
||||||
run: |
|
|
||||||
set -e
|
|
||||||
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.actor }}/${{ toLower(gitea.repository_name) }}-frontend:latest \
|
|
||||||
--tarPath=/workspace/frontend.tar
|
|
||||||
|
|
||||||
- name: Verify that image files exist
|
|
||||||
run: |
|
|
||||||
echo "--- Verifying contents of workspace ---"
|
|
||||||
ls -lh ${{ gitea.workspace }}
|
|
||||||
# Este paso nos dará la prueba definitiva de que los .tar existen
|
|
||||||
|
|
||||||
# -----------------------------------------------------------------
|
|
||||||
# PASO 3: DESPLEGAR (COPIAR FICHEROS, CARGAR IMÁGENES Y LEVANTAR)
|
|
||||||
# -----------------------------------------------------------------
|
|
||||||
- name: Deploy to Production via SCP and SSH
|
|
||||||
run: |
|
|
||||||
set -e
|
|
||||||
echo "Preparing SSH credentials..."
|
|
||||||
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
|
||||||
|
|
||||||
echo "Copying image files to production server..."
|
# 2. Conectarse al HOST y ejecutar todo el proceso allí
|
||||||
scp ${{ gitea.workspace }}/backend.tar ${{ gitea.workspace }}/frontend.tar ${{ secrets.PROD_SERVER_USER }}@${{ secrets.PROD_SERVER_HOST }}:/opt/gestion-integral/
|
echo "Connecting to host to execute entire CI/CD process..."
|
||||||
|
|
||||||
echo "Connecting to host to load images and deploy..."
|
|
||||||
ssh ${{ secrets.PROD_SERVER_USER }}@${{ secrets.PROD_SERVER_HOST }} << 'EOF'
|
ssh ${{ secrets.PROD_SERVER_USER }}@${{ secrets.PROD_SERVER_HOST }} << 'EOF'
|
||||||
set -e
|
set -e # También aquí, para fallar en la sesión remota
|
||||||
echo "--- CONECTADO AL SERVIDOR DE PRODUCCIÓN ---"
|
|
||||||
cd /opt/gestion-integral
|
# --- PARTE 1: PREPARACIÓN (EN EL HOST) ---
|
||||||
|
echo "--- (HOST) Preparing temporary workspace ---"
|
||||||
|
REPO_URL="${{ gitea.remotes.origin.url }}"
|
||||||
|
COMMIT_SHA="${{ gitea.sha }}"
|
||||||
|
TEMP_DIR="/tmp/gitea-build/${{ gitea.run_id }}"
|
||||||
|
|
||||||
|
rm -rf $TEMP_DIR
|
||||||
|
mkdir -p $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... ---"
|
||||||
|
|
||||||
|
# Backend
|
||||||
|
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=${{ gitea.actor }}/${{ toLower(gitea.repository_name) }}-backend:latest \
|
||||||
|
--tarPath=/workspace/backend.tar
|
||||||
|
|
||||||
|
# Frontend
|
||||||
|
docker run --rm -v "$(pwd)":/workspace gcr.io/kaniko-project/executor:v1.9.0 \
|
||||||
|
--context=/workspace \
|
||||||
|
--dockerfile=/workspace/Frontend/Dockerfile \
|
||||||
|
--no-push \
|
||||||
|
--destination=${{ gitea.actor }}/${{ toLower(gitea.repository_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..."
|
cd /opt/gestion-integral
|
||||||
export DB_SA_PASSWORD="${{ secrets.DB_SA_PASSWORD_SECRET }}"
|
export DB_SA_PASSWORD="${{ secrets.DB_SA_PASSWORD_SECRET }}"
|
||||||
export JWT_KEY="${{ secrets.JWT_KEY_SECRET }}"
|
export JWT_KEY="${{ secrets.JWT_KEY_SECRET }}"
|
||||||
docker compose up -d
|
docker compose up -d
|
||||||
|
|
||||||
echo "Cleaning up tar files and old images..."
|
# --- PARTE 4: LIMPIEZA (EN EL HOST) ---
|
||||||
rm backend.tar frontend.tar
|
echo "--- (HOST) Cleaning up... ---"
|
||||||
|
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