Update Functional Requirements #13
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: Build and Deploy to GKE | |
| on: | |
| push: | |
| branches: | |
| - main | |
| workflow_dispatch: | |
| env: | |
| PROJECT_ID: threadit-api | |
| CLUSTER_NAME: threadit-cluster | |
| ZONE: europe-west1-b | |
| GCS_KEY: gcs-key | |
| SERVICES: db community thread comment vote search popular | |
| jobs: | |
| check-cluster: | |
| name: Check if GKE cluster exists | |
| runs-on: ubuntu-latest | |
| outputs: | |
| exists: ${{ steps.set-exists.outputs.exists }} | |
| permissions: | |
| contents: read | |
| id-token: write | |
| steps: | |
| - name: Authenticate to Google Cloud | |
| uses: google-github-actions/auth@v2 | |
| with: | |
| credentials_json: ${{ secrets.GCP_SA_KEY }} | |
| - name: Set up Google Cloud SDK | |
| uses: google-github-actions/setup-gcloud@v2 | |
| with: | |
| project_id: ${{ env.PROJECT_ID }} | |
| - id: set-exists | |
| run: | | |
| if gcloud container clusters describe $CLUSTER_NAME --zone $ZONE --project $PROJECT_ID; then | |
| echo "exists=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "exists=false" >> $GITHUB_OUTPUT | |
| fi | |
| build-publish-deploy: | |
| name: Build, Publish, and Deploy | |
| needs: check-cluster | |
| if: needs.check-cluster.outputs.exists == 'true' | |
| runs-on: ubuntu-latest | |
| environment: production | |
| permissions: | |
| contents: read | |
| id-token: write | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Authenticate to Google Cloud | |
| uses: google-github-actions/auth@v2 | |
| with: | |
| credentials_json: ${{ secrets.GCP_SA_KEY }} | |
| - name: Set up Google Cloud SDK | |
| uses: google-github-actions/setup-gcloud@v2 | |
| with: | |
| project_id: ${{ env.PROJECT_ID }} | |
| - name: Set up GKE credentials | |
| uses: google-github-actions/get-gke-credentials@v2 | |
| with: | |
| project_id: ${{ env.PROJECT_ID }} | |
| cluster_name: ${{ env.CLUSTER_NAME }} | |
| location: ${{ env.ZONE }} | |
| - name: Configure Docker for GCR | |
| run: | | |
| gcloud auth configure-docker --quiet | |
| - name: Build and push images to GCR | |
| working-directory: code | |
| run: | | |
| for SERVICE in $SERVICES; do | |
| docker build -t gcr.io/$PROJECT_ID/${SERVICE}-service:latest -f services/${SERVICE}-service/Dockerfile . | |
| docker push gcr.io/$PROJECT_ID/${SERVICE}-service:latest | |
| done | |
| docker build -t gcr.io/$PROJECT_ID/grpc-gateway:latest -f grpc-gateway/Dockerfile . | |
| docker push gcr.io/$PROJECT_ID/grpc-gateway:latest | |
| - name: Deploy Traefik | |
| working-directory: code/kubernetes | |
| run: | | |
| helm repo add traefik https://traefik.github.io/charts | |
| helm repo update | |
| helm upgrade --install traefik traefik/traefik -n $CLUSTER_NAME -f traefik/resources.yaml | |
| kubectl apply -n $CLUSTER_NAME -f traefik/ingress.yaml | |
| kubectl apply -n $CLUSTER_NAME -f traefik/hpa-config.yaml | |
| - name: Create Kubernetes secrets | |
| run: | | |
| BUCKET_SECRET=$(gcloud secrets versions access latest --secret=$GCS_KEY) | |
| MONGO_USER=$(gcloud secrets versions access latest --secret="mongo-user") | |
| MONGO_PASS=$(gcloud secrets versions access latest --secret="mongo-pass") | |
| kubectl create secret generic "bucket-secret" \ | |
| --from-literal="$GCS_KEY.json=$BUCKET_SECRET" \ | |
| -n $CLUSTER_NAME --dry-run=client -o yaml | kubectl apply -f - | |
| kubectl create secret generic "mongo-secret" \ | |
| --from-literal="MONGO_INITDB_ROOT_USERNAME=$MONGO_USER" \ | |
| --from-literal="MONGO_INITDB_ROOT_PASSWORD=$MONGO_PASS" \ | |
| -n $CLUSTER_NAME --dry-run=client -o yaml | kubectl apply -f - | |
| - name: Deploy configuration and Mongo | |
| working-directory: code/kubernetes | |
| run: | | |
| kubectl apply -n $CLUSTER_NAME -f config.yaml | |
| kubectl apply -n $CLUSTER_NAME -f mongo/ | |
| - name: Deploy services | |
| working-directory: code/kubernetes | |
| run: | | |
| for SERVICE in $SERVICES; do | |
| kubectl apply -n $CLUSTER_NAME -f services/${SERVICE}-service/ | |
| done | |
| kubectl apply -n $CLUSTER_NAME -f grpc-gateway/ |