This commit is contained in:
@@ -6,64 +6,89 @@ on:
|
|||||||
- main
|
- main
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
# ===================================================================
|
||||||
|
# UN ÚNICO JOB PARA CONSTRUIR, GUARDAR Y DESPLEGAR
|
||||||
|
# ===================================================================
|
||||||
build-and-deploy:
|
build-and-deploy:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
|
# -----------------------------------------------------------------
|
||||||
|
# PASO 1: PREPARACIÓN DEL ENTORNO DEL JOB
|
||||||
|
# -----------------------------------------------------------------
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
# Ya no necesitamos crear config.json. La acción de login lo maneja.
|
- name: Install necessary tools
|
||||||
- name: Login to Gitea Registry
|
|
||||||
uses: docker/login-action@v2
|
|
||||||
with:
|
|
||||||
registry: 127.0.0.1:5000
|
|
||||||
username: ${{ secrets.REGISTRY_USER }}
|
|
||||||
password: ${{ secrets.ACTIONS_PAT }}
|
|
||||||
|
|
||||||
- name: Build and Push Backend
|
|
||||||
uses: docker/build-push-action@v4
|
|
||||||
with:
|
|
||||||
context: .
|
|
||||||
file: Backend/GestionIntegral.Api/Dockerfile
|
|
||||||
push: true
|
|
||||||
# La imagen se etiqueta para el registro local
|
|
||||||
tags: 127.0.0.1:5000/dmolinari/gestionintegralweb-backend:latest,127.0.0.1:5000/dmolinari/gestionintegralweb-backend:${{ gitea.sha_short }}
|
|
||||||
|
|
||||||
- name: Build and Push Frontend
|
|
||||||
uses: docker/build-push-action@v4
|
|
||||||
with:
|
|
||||||
context: .
|
|
||||||
file: Frontend/Dockerfile
|
|
||||||
push: true
|
|
||||||
tags: 127.0.0.1:5000/dmolinari/gestionintegralweb-frontend:latest,127.0.0.1:5000/dmolinari/gestionintegralweb-frontend:${{ gitea.sha_short }}
|
|
||||||
|
|
||||||
# El despliegue ahora es un paso final en el mismo job
|
|
||||||
- name: Deploy to Production via SSH
|
|
||||||
run: |
|
run: |
|
||||||
set -e
|
set -e
|
||||||
echo "Preparing SSH client..."
|
# Instala el cliente SSH para el paso de despliegue
|
||||||
apt-get update && apt-get install -y openssh-client
|
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 "Connecting to host to deploy..."
|
echo "Copying image files to production server..."
|
||||||
|
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 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
|
||||||
echo "--- CONECTADO AL SERVIDOR DE PRODUCCIÓN ---"
|
echo "--- CONECTADO AL SERVIDOR DE PRODUCCIÓN ---"
|
||||||
cd /opt/gestion-integral
|
cd /opt/gestion-integral
|
||||||
|
|
||||||
|
echo "Loading images into Docker..."
|
||||||
|
docker load < backend.tar
|
||||||
|
docker load < frontend.tar
|
||||||
|
|
||||||
|
echo "Starting application stack..."
|
||||||
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 }}"
|
||||||
|
|
||||||
# El login ya no es necesario aquí, el demonio ya tiene la sesión
|
|
||||||
# del paso anterior del pipeline. Pero lo dejamos por robustez.
|
|
||||||
echo "${{ secrets.ACTIONS_PAT }}" | docker login 127.0.0.1:5000 -u ${{ secrets.REGISTRY_USER }} --password-stdin
|
|
||||||
|
|
||||||
docker compose pull
|
|
||||||
docker compose up -d
|
docker compose up -d
|
||||||
|
|
||||||
|
echo "Cleaning up tar files and old images..."
|
||||||
|
rm backend.tar frontend.tar
|
||||||
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