Home > Blockchain >  How to specify Google Cloud Run metadata in Github Actions?
How to specify Google Cloud Run metadata in Github Actions?

Time:07-20

I have a Github Action which builds a docker image then uploads it to the Container Registry. Next I want to deploy this container to a Cloud Run service with some specific settings for the min and max instances, ensure CPU is always on, internal ingress only, etc. The documentation says these settings are set using metadata, but no example is shown. What format should this metadata take?

name: Push code to GCP
on:
  push:
    branches: [ main ]
jobs:
  container-build-push-deploy:
    name: Build Container Push to Registry Deploy to Cloud Run
    runs-on: ubuntu-latest
    env:
      IMAGE_NAME: my-image
      PROJECT_ID: my-project-123456
      REGION: us-central1
    steps:
    - name: Checkout
      uses: actions/checkout@v2

    - name: Authenticate With GCP
      id: auth
      uses: google-github-actions/auth@v0
      with:
        credentials_json: ${{ secrets.GCP_CREDENTIALS }}

    - name: Setup Cloud SDK
      uses: google-github-actions/setup-gcloud@v0
      with:
        project_id: ${{ env.PROJECT_ID }}
    
    - name: Tag Release
      id: increment-git-tag
      run: |
        bash ./scripts/git_update.sh -v patch

    - name: Build Docker Image
      run: docker build -t $IMAGE_NAME:latest .
    
    - name: Configure Docker Client
      run: |-
        gcloud auth configure-docker --quiet

    - name: Push Docker Image to Container Registry
      env:
        GIT_TAG: ${{ steps.increment-git-tag.outputs.git-tag }}
      run: |-
        docker tag $IMAGE_NAME:latest gcr.io/$PROJECT_ID/$IMAGE_NAME:latest
        docker tag $IMAGE_NAME:latest gcr.io/$PROJECT_ID/$IMAGE_NAME:$GIT_TAG
        docker push gcr.io/$PROJECT_ID/$IMAGE_NAME:latest
        docker push gcr.io/$PROJECT_ID/$IMAGE_NAME:$GIT_TAG
    
    - name: Deploy to Cloud Run
      env:
        GIT_TAG: ${{ steps.increment-git-tag.outputs.git-tag }}
      uses: google-github-actions/deploy-cloudrun@v0
      with:
        service: my-service
        image: 'gcr.io/${{ env.PROJECT_ID }}/${{ env.IMAGE_NAME }}:${{ env.GIT_TAG }}'
        region: ${{ env.REGION }}
        secrets: |
          /app/path/to/my-secret=my-secret:latest
        metadata:
          min-instances: 1
          max-instances: 1
          ingress: internal
          tag: ${{ env.GIT_TAG }}
          no-cpu-throttling: true
          command: node
          args: |
            /app/path/to/main.js
            arg-1

Obviously this last metadata piece is wrong since with is supposed to be key-value pairs of string. What is the correct format here?

CodePudding user response:

I ended up going the pure CLI route in the interest of saving time

    - name: Deploy to Cloud Run
      env:
        GIT_TAG: ${{ steps.increment-git-tag.outputs.git-tag }}
        SERVICE: my-service
        MY_ARG: arg-1
      run: |
        gcloud run deploy $SERVICE --image=gcr.io/$PROJECT_ID/$IMAGE_NAME:$GIT_TAG \
          --platform=managed --region=$REGION --min-instances=1 --max-instances=1 \
          --ingress=internal --tag=latest --no-cpu-throttling --no-allow-unauthenticated \
          --command=node --args=/app/path/to/main.js,$MY_ARG \
          --set-secrets=/app/path/to/my-secret=my-secret:latest

It would be nice to get another answer on how to use the pre-built setup-gcloud Github Action though from someone who knows.

CodePudding user response:

According to the link that you share, the specs of your Cloud Run Service can be stored in a yaml file.

You can store your service specification in a YAML file

So I created a yaml (ex: service.yaml) file and pushed it to the github repository.

Sample service.yaml file code with min and max instances, number of cpu and internal ingress

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
      name: my-service
      annotations:
        run.googleapis.com/ingress: internal
        run.googleapis.com/cpu-throttling: 'False'
spec:
  template:
    metadata:
      annotations:
        autoscaling.knative.dev/minScale: '2'
        autoscaling.knative.dev/maxScale: '50'
    spec:
      containers:
      - image: <IMAGE_URL>
        resources:
          limits:
            cpu: '2'

And here is the Deploy to Cloud Run steps yaml file

- name: Deploy to Cloud Run
  uses: google-github-actions/deploy-cloudrun@v0
  with:
    region: ${{ env.REGION }}
    metadata: service.yaml

Additional Info: You can use the sed command in linux to edit or replace string of a files even without opening them

- name: Set Image Name
  run: your_sed_command
  • Related