Some checks failed
Build and Deploy / build-and-deploy (push) Failing after 27s
92 lines
4.0 KiB
YAML
92 lines
4.0 KiB
YAML
name: Build and Deploy
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- main
|
|
|
|
jobs:
|
|
# ===================================================================
|
|
# UN ÚNICO JOB PARA CONSTRUIR, GUARDAR, COPIAR Y DESPLEGAR
|
|
# ===================================================================
|
|
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
|
|
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
|
|
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'
|
|
set -e
|
|
echo "--- (HOST) CONECTADO ---"
|
|
cd /opt/gestion-integral
|
|
|
|
echo "Loading images into Docker..."
|
|
docker load < backend.tar
|
|
docker load < frontend.tar
|
|
|
|
echo "Starting application stack (will use .env file)..."
|
|
# docker-compose usará el .env automáticamente
|
|
docker compose up -d
|
|
|
|
echo "Cleaning up temporary files..."
|
|
rm backend.tar frontend.tar .env
|
|
docker image prune -af
|
|
|
|
echo "--- ¡¡DESPLIEGUE COMPLETADO Y VERIFICADO!! ---"
|
|
EOF |