Deploy Backend to Cloud RUN #858
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 Backend to Cloud RUN | |
| on: | |
| # push: | |
| # branches: [ "main" ] | |
| # paths: | |
| # - 'backend/**' | |
| workflow_dispatch: | |
| inputs: | |
| environment: | |
| description: 'Select the environment to deploy to' | |
| required: true | |
| default: 'development' | |
| branch: | |
| description: 'Branch to deploy from' | |
| required: true | |
| default: 'main' | |
| env: | |
| SERVICE: backend | |
| REGION: us-central1 | |
| jobs: | |
| deploy: | |
| environment: ${{ github.event.inputs.environment }} | |
| permissions: | |
| contents: 'read' | |
| id-token: 'write' | |
| runs-on: ubuntu-latest-m | |
| steps: | |
| - name: Validate Environment Input | |
| run: | | |
| if [[ "${{ github.event.inputs.environment }}" != "development" && "${{ github.event.inputs.environment }}" != "prod" ]]; then | |
| echo "Invalid environment: ${{ github.event.inputs.environment }}. Must be 'development' or 'prod'." | |
| exit 1 | |
| fi | |
| # To workaround "no space left on device" issue of GitHub-hosted runner | |
| - name: Delete huge unnecessary tools folder | |
| run: rm -rf /opt/hostedtoolcache | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: ${{ github.event.inputs.branch }} | |
| - name: Google Auth | |
| id: auth | |
| uses: 'google-github-actions/auth@v2' | |
| with: | |
| credentials_json: ${{ secrets.GCP_CREDENTIALS }} | |
| - name: Login to GCR | |
| run: gcloud auth configure-docker | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Google Service Account | |
| run: echo "${{ secrets.GCP_SERVICE_ACCOUNT }}" | base64 -d > ./backend/google-credentials.json | |
| - name: Build and Push Docker image | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| file: ./backend/Dockerfile | |
| push: true | |
| tags: gcr.io/${{ vars.GCP_PROJECT_ID }}/${{ env.SERVICE }}:latest | |
| cache-from: type=registry,ref=gcr.io/${{ vars.GCP_PROJECT_ID }}/${{ env.SERVICE }}:buildcache | |
| cache-to: type=registry,ref=gcr.io/${{ vars.GCP_PROJECT_ID }}/${{ env.SERVICE }}:buildcache,mode=max | |
| - name: Deploy ${{ env.SERVICE }} to Cloud Run | |
| id: deploy-backend | |
| uses: google-github-actions/deploy-cloudrun@v2 | |
| with: | |
| service: ${{ env.SERVICE }} | |
| region: ${{ env.REGION }} | |
| image: gcr.io/${{ vars.GCP_PROJECT_ID }}/${{ env.SERVICE }} | |
| - name: Deploy ${{ env.SERVICE }}-sync to Cloud Run | |
| id: deploy-backend-sync | |
| uses: google-github-actions/deploy-cloudrun@v2 | |
| with: | |
| service: ${{ env.SERVICE }}-sync | |
| region: ${{ env.REGION }} | |
| image: gcr.io/${{ vars.GCP_PROJECT_ID }}/${{ env.SERVICE }} | |
| - name: Connect to GKE cluster | |
| run: | | |
| curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /usr/share/keyrings/cloud.google.gpg | |
| echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list | |
| sudo apt-get update && sudo apt-get install google-cloud-cli-gke-gcloud-auth-plugin -y | |
| gcloud container clusters get-credentials ${{ vars.GKE_CLUSTER }} --region ${{ env.REGION }} --project ${{ vars.GCP_PROJECT_ID }} | |
| - name: Deploy ${{ env.SERVICE }}-listen to GKE | |
| run: | | |
| kubectl -n ${{ vars.ENV }}-omi-backend rollout restart deploy ${{ vars.ENV }}-omi-backend-listen | |
| - name: Deploy ${{ env.SERVICE }}-integration to Cloud Run | |
| id: deploy-backend-integration | |
| uses: google-github-actions/deploy-cloudrun@v2 | |
| with: | |
| service: ${{ env.SERVICE }}-integration | |
| region: ${{ env.REGION }} | |
| image: gcr.io/${{ vars.GCP_PROJECT_ID }}/${{ env.SERVICE }} | |
| # If required, use the Cloud Run url output in later steps | |
| - name: Show Output | |
| run: echo ${{ steps.deploy.outputs.url }} |