Merge pull request #2 from Orctatech-Engineering-Team/chore/ci-changes #10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Deploy | |
| on: | |
| push: | |
| branches: [master] | |
| env: | |
| REGISTRY: ghcr.io | |
| IMAGE_NAME: orctatech-engineering-team/orcta-backend | |
| jobs: | |
| build-and-push: | |
| name: Build & push Docker image | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| outputs: | |
| image_tag: ${{ steps.meta.outputs.version }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Log in to GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Extract image metadata | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
| tags: | | |
| type=sha,prefix=,format=short | |
| type=raw,value=latest | |
| - name: Build and push | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| file: apps/backend/Dockerfile | |
| push: true | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| build-args: | | |
| SERVICE_VERSION=${{ github.sha }} | |
| deploy: | |
| name: Deploy to VPS | |
| runs-on: ubuntu-latest | |
| needs: build-and-push | |
| environment: production | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Deploy via SSH | |
| uses: appleboy/ssh-action@v1 | |
| with: | |
| host: ${{ secrets.VPS_HOST }} | |
| username: ${{ secrets.VPS_USER }} | |
| key: ${{ secrets.VPS_SSH_KEY }} | |
| script: | | |
| cd /opt/orcta-stack | |
| # Pull the new image | |
| echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin | |
| docker pull ghcr.io/orctatech-engineering-team/orcta-backend:latest | |
| # Run DB migrations before swapping the container | |
| IMAGE_TAG=latest docker compose -f docker-compose.prod.yml run --rm \ | |
| backend node src/db/migrate.js | |
| # Swap the backend container with zero-downtime restart | |
| IMAGE_TAG=latest docker compose -f docker-compose.prod.yml up -d backend | |
| # Clean up old images | |
| docker image prune -f |