name: Build & Deploy on: push: branches: [main] workflow_dispatch: inputs: deploy: description: 'Desplegar en el VPS despues de construir' required: true default: true type: boolean env: REGISTRY: ghcr.io BACKEND_IMAGE: ghcr.io/${{ github.repository }}/backend FRONTEND_IMAGE: ghcr.io/${{ github.repository }}/frontend jobs: build: name: Build & Push Images runs-on: ubuntu-latest permissions: contents: read packages: write steps: - name: Checkout code uses: actions/checkout@v4 - name: Login to GitHub Container Registry uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Build & push backend uses: docker/build-push-action@v5 with: context: ./back push: true cache-from: type=gha cache-to: type=gha,mode=max tags: | ${{ env.BACKEND_IMAGE }}:latest ${{ env.BACKEND_IMAGE }}:${{ github.sha }} - name: Build & push frontend uses: docker/build-push-action@v5 with: context: ./front push: true cache-from: type=gha cache-to: type=gha,mode=max build-args: | VITE_API_URL=/api tags: | ${{ env.FRONTEND_IMAGE }}:latest ${{ env.FRONTEND_IMAGE }}:${{ github.sha }} deploy: name: Deploy to VPS needs: build if: ${{ github.event_name == 'push' || inputs.deploy }} runs-on: ubuntu-latest steps: - name: Deploy via SSH uses: appleboy/ssh-action@v1 with: host: ${{ secrets.VPS_HOST }} username: ${{ secrets.VPS_USER }} key: ${{ secrets.VPS_SSH_KEY }} port: ${{ secrets.VPS_PORT || 22 }} command_timeout: 30m script: | cd ${{ secrets.VPS_PROJECT_PATH }} # Sincronizar archivos del host con el repo (sin conflictos) git fetch origin main && git reset --hard origin/main # Login al registry echo ${{ secrets.CR_PAT }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin # Descargar imagenes nuevas docker pull ${{ env.BACKEND_IMAGE }}:latest docker pull ${{ env.FRONTEND_IMAGE }}:latest # Reiniciar servicios docker compose --env-file .env.prod -f docker-compose.prod.yml up -d --force-recreate backend frontend nginx # Ejecutar migraciones si hay pendientes docker exec admision_prod_backend php artisan migrate --force # Limpiar imagenes viejas docker image prune -f