nxtgauge-backend-rust/.github/workflows/build-and-deploy-ghcr.yml
2026-06-14 23:00:37 +05:30

115 lines
3.9 KiB
YAML

name: build-and-deploy-ghcr
on:
push:
branches:
- main
- high-performance
workflow_dispatch:
permissions:
contents: read
packages: write
env:
K8S_NAMESPACE: nxtgauge
GITOPS_REPO: Traceworks2023/nxtgauge-gitops
jobs:
build-deploy:
runs-on: ubuntu-latest
strategy:
fail-fast: false
max-parallel: 4
matrix:
service:
- gateway
- users
- companies
- jobs
- leads
- job-seekers
- customers
- payments
- employees
- photographers
- makeup-artists
- tutors
- developers
- video-editors
- graphic-designers
- social-media-managers
- fitness-trainers
- catering-services
- ugc-content-creators
- cron
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ secrets.GHCR_USERNAME }}
password: ${{ secrets.DEPLOY_GITHUB_TOKEN }}
- name: Build and push image
id: build
uses: docker/build-push-action@v6
with:
context: .
file: Dockerfile.simple
push: true
platforms: linux/amd64
build-args: |
SERVICE_NAME=${{ matrix.service }}
tags: ghcr.io/traceworks2023/nxtgauge-rust-${{ matrix.service }}:${{ github.sha }}
- name: Configure kubeconfig
run: |
set -euo pipefail
mkdir -p ~/.kube
printf '%s' '${{ secrets.KUBE_CONFIG_DATA }}' | base64 -d > ~/.kube/config
chmod 600 ~/.kube/config
- name: Install kubectl
uses: azure/setup-kubectl@v4
- name: Deploy to Kubernetes
env:
GHCR_USERNAME: ${{ secrets.GHCR_USERNAME }}
GHCR_TOKEN: ${{ secrets.DEPLOY_GITHUB_TOKEN }}
run: |
set -euo pipefail
deployment="nxtgauge-rust-${{ matrix.service }}"
container="${{ matrix.service }}"
image_ref="ghcr.io/traceworks2023/nxtgauge-rust-${{ matrix.service }}@${{ steps.build.outputs.digest }}"
kubectl -n "$K8S_NAMESPACE" create secret docker-registry ghcr-regcred --docker-server=ghcr.io --docker-username="$GHCR_USERNAME" --docker-password="$GHCR_TOKEN" --dry-run=client -o yaml | kubectl apply -f -
kubectl -n "$K8S_NAMESPACE" patch deployment "$deployment" --type merge -p '{"spec":{"template":{"spec":{"imagePullSecrets":[{"name":"ghcr-regcred"}]}}}}'
kubectl -n "$K8S_NAMESPACE" set image deployment/"$deployment" "$container"="$image_ref"
kubectl -n "$K8S_NAMESPACE" rollout status deployment/"$deployment" --timeout=15m
- name: Sync GitOps release
env:
GITOPS_TOKEN: ${{ secrets.DEPLOY_GITHUB_TOKEN }}
run: |
set -euo pipefail
git clone "https://${{ secrets.GHCR_USERNAME }}:${GITOPS_TOKEN}@github.com/${GITOPS_REPO}.git" /tmp/nxtgauge-gitops
cd /tmp/nxtgauge-gitops
./scripts/set-backend-rust-release.sh "${{ matrix.service }}" "${{ steps.build.outputs.digest }}"
if git diff --quiet; then
echo "GitOps repo already up to date."
exit 0
fi
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add apps/nxtgauge-backend-rust/overlays/prod/backend-release-state.tsv apps/nxtgauge-backend-rust/overlays/prod/release-patches.yaml apps/nxtgauge-backend-rust/overlays/prod/disabled-deployments.yaml
git commit -m "chore(gitops): deploy backend ${{ matrix.service }}@${{ github.sha }}"
git pull --rebase origin main
git push origin HEAD:main