From a383162ac0fd73902d4ae44adbd20a4628eebc99 Mon Sep 17 00:00:00 2001 From: FrancoRu Date: Fri, 18 Sep 2026 13:40:16 -0300 Subject: [PATCH] Sube Actions para CI/CD --- .github/workflows/deploy-backend.yml | 130 ++++++++++++++++++++++++++ .github/workflows/deploy-frontend.yml | 116 +++++++++++++++++++++++ 2 files changed, 246 insertions(+) create mode 100644 .github/workflows/deploy-backend.yml create mode 100644 .github/workflows/deploy-frontend.yml diff --git a/.github/workflows/deploy-backend.yml b/.github/workflows/deploy-backend.yml new file mode 100644 index 0000000..96f9c33 --- /dev/null +++ b/.github/workflows/deploy-backend.yml @@ -0,0 +1,130 @@ +name: Deploy backend + +on: + push: + branches: [develop] + paths: + - 'Club12-Backend/**' + - 'docker-compose.yml' + - '.github/workflows/deploy-backend.yml' + workflow_dispatch: + +concurrency: + group: deploy-backend-${{ github.ref }} + cancel-in-progress: false + +env: + IMAGE: ghcr.io/francoru/club12-backend + SERVICE: backend + DEPLOY_DIR: /home/docker-compose/Club12 + +jobs: + build: + runs-on: ubuntu-latest + timeout-minutes: 30 + permissions: + contents: read + packages: write + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to GHCR + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and push + uses: docker/build-push-action@v6 + with: + context: ./Club12-Backend + push: true + provenance: false + tags: ${{ env.IMAGE }}:latest + cache-from: type=gha + cache-to: type=gha,mode=max + + deploy: + needs: build + runs-on: [self-hosted, Club-12-back-runner] + timeout-minutes: 15 + permissions: + contents: read + packages: read + concurrency: + group: club12-deploy-${{ github.ref }} + cancel-in-progress: false + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + persist-credentials: false + + - name: Log in to GHCR + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Sync compose file to the server project directory + run: | + set -euo pipefail + if [ ! -d "$DEPLOY_DIR" ]; then + echo "::error::$DEPLOY_DIR does not exist on this runner host. Create it before deploying." + exit 1 + fi + if [ ! -f "$DEPLOY_DIR/.env" ]; then + echo "::error::$DEPLOY_DIR/.env is missing. Create it once by hand from .env.example (see DEPLOYMENT.md)." + exit 1 + fi + install -m 0644 docker-compose.yml "$DEPLOY_DIR/docker-compose.yml" + echo "Synced docker-compose.yml -> $DEPLOY_DIR/docker-compose.yml" + + - name: Archive the currently running image as :previous + working-directory: /home/docker-compose/Club12 + run: | + set -euo pipefail + if docker image inspect "$IMAGE:latest" >/dev/null 2>&1; then + docker tag "$IMAGE:latest" "$IMAGE:previous" + echo "Archived $IMAGE:latest -> $IMAGE:previous" + else + echo "No local $IMAGE:latest yet (first deploy) - nothing to archive." + fi + + - name: Pull the new image + working-directory: /home/docker-compose/Club12 + run: | + set -euo pipefail + docker compose pull "$SERVICE" + + - name: Restart the service + working-directory: /home/docker-compose/Club12 + run: | + set -euo pipefail + docker compose up -d --no-deps --no-build "$SERVICE" + docker compose ps "$SERVICE" + + - name: Re-point the frontend proxy at the new backend + working-directory: /home/docker-compose/Club12 + run: | + set -euo pipefail + # The frontend's nginx resolves `backend` once at start-up. Recreating + # the backend gives it a new container IP, so without this restart + # every /api/* request 502s until the frontend is bounced. + if docker compose ps --status running --services | grep -qx frontend; then + docker compose restart frontend + docker compose ps frontend + else + echo "frontend not running on this host - skipping proxy refresh" + fi + + - name: Prune dangling images + run: | + set -euo pipefail + docker image prune -f diff --git a/.github/workflows/deploy-frontend.yml b/.github/workflows/deploy-frontend.yml new file mode 100644 index 0000000..8de2326 --- /dev/null +++ b/.github/workflows/deploy-frontend.yml @@ -0,0 +1,116 @@ +name: Deploy frontend + +on: + push: + branches: [develop] + paths: + - 'Club12-WebClient/**' + - 'docker-compose.yml' + - '.github/workflows/deploy-frontend.yml' + workflow_dispatch: + +concurrency: + group: deploy-frontend-${{ github.ref }} + cancel-in-progress: false + +env: + IMAGE: ghcr.io/francoru/club12-frontend + SERVICE: frontend + DEPLOY_DIR: /home/docker-compose/Club12 + +jobs: + build: + runs-on: ubuntu-latest + timeout-minutes: 30 + permissions: + contents: read + packages: write + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to GHCR + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and push + uses: docker/build-push-action@v6 + with: + context: ./Club12-WebClient + push: true + provenance: false + tags: ${{ env.IMAGE }}:latest + cache-from: type=gha + cache-to: type=gha,mode=max + + deploy: + needs: build + runs-on: [self-hosted, Club-12-front-runner] + timeout-minutes: 15 + permissions: + contents: read + packages: read + concurrency: + group: club12-deploy-${{ github.ref }} + cancel-in-progress: false + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + persist-credentials: false + + - name: Log in to GHCR + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Sync compose file to the server project directory + run: | + set -euo pipefail + if [ ! -d "$DEPLOY_DIR" ]; then + echo "::error::$DEPLOY_DIR does not exist on this runner host. Create it before deploying." + exit 1 + fi + if [ ! -f "$DEPLOY_DIR/.env" ]; then + echo "::error::$DEPLOY_DIR/.env is missing. Create it once by hand from .env.example (see DEPLOYMENT.md)." + exit 1 + fi + install -m 0644 docker-compose.yml "$DEPLOY_DIR/docker-compose.yml" + echo "Synced docker-compose.yml -> $DEPLOY_DIR/docker-compose.yml" + + - name: Archive the currently running image as :previous + working-directory: /home/docker-compose/Club12 + run: | + set -euo pipefail + if docker image inspect "$IMAGE:latest" >/dev/null 2>&1; then + docker tag "$IMAGE:latest" "$IMAGE:previous" + echo "Archived $IMAGE:latest -> $IMAGE:previous" + else + echo "No local $IMAGE:latest yet (first deploy) - nothing to archive." + fi + + - name: Pull the new image + working-directory: /home/docker-compose/Club12 + run: | + set -euo pipefail + docker compose pull "$SERVICE" + + - name: Restart the service + working-directory: /home/docker-compose/Club12 + run: | + set -euo pipefail + docker compose up -d --no-deps --no-build "$SERVICE" + docker compose ps "$SERVICE" + + - name: Prune dangling images + run: | + set -euo pipefail + docker image prune -f