Isbister
Isbister

Reputation: 946

How do you specify a custom service.yaml for cloud run in a cloudbuild.yaml?

I have a docker container deployed on Googles Cloud Run service. It has a very basic cloudbuild.yaml file that triggers from a git push to main branch.

I wish to automatically increase the ram of the cloud run machine from 512mb to 8gb. I know this is possible in the Cloud Run UI by clicking "EDIT @ DEPLOY NEW REVISION" and then manually selecting 8gb. But I would like to have this setup automatically.

You can fetch the .yaml from Cloud Run by:

gcloud run services describe SERVICE --format export > service.yaml
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  annotations:
    client.knative.dev/user-image: 'gcr.io/project/service:ebbe555'
    run.googleapis.com/ingress: all
    run.googleapis.com/ingress-status: all
    run.googleapis.com/launch-stage: BETA
  labels:
    cloud.googleapis.com/location: europe-north1
  name: service
  namespace: '467851153648'
spec:
  template:
    metadata:
      annotations:
        autoscaling.knative.dev/maxScale: '100'
        autoscaling.knative.dev/minScale: '1'
        client.knative.dev/user-image: 'gcr.io/project/service:ebbe555'
        run.googleapis.com/client-name: gcloud
        run.googleapis.com/client-version: 378.0.0
        run.googleapis.com/execution-environment: gen2
      name: faq-engine-00004-vov
    spec:
      containerConcurrency: 80
      containers:
        - image: 'gcr.io/project/service:ebbe555'
          ports:
            - containerPort: 8081
              name: http1
          resources:
            limits:
              cpu: 4000m
              memory: 8Gi
      serviceAccountName: service@project.iam.gserviceaccount.com
      timeoutSeconds: 300
  traffic:
    - latestRevision: true
      percent: 100

And you can replace the current .yaml semi automatically with:

gcloud run services replace service.yaml

However, is there any way to make the actual Cloud Build load the custom service.yaml in the Deploy container image to Cloud Run step?

cloudbuild.yaml

timeout: 1800s
substitutions:
  _SERVICE_NAME: service
  _REGION: europe-north1
images:
  - 'gcr.io/${PROJECT_ID}/${_SERVICE_NAME}:${SHORT_SHA}'
options:
  machineType: N1_HIGHCPU_32
  dynamic_substitutions: true
steps:
  - id: Build the container image
    name: gcr.io/cloud-builders/docker
    args:
      - build
      - '-t'
      - 'gcr.io/${PROJECT_ID}/${_SERVICE_NAME}:${SHORT_SHA}'
      - .
  - id: Push the container image to Container Registry
    name: gcr.io/cloud-builders/docker
    args:
      - push
      - 'gcr.io/${PROJECT_ID}/${_SERVICE_NAME}:${SHORT_SHA}'
  - id: Deploy container image to Cloud Run
    name: gcr.io/google.com/cloudsdktool/cloud-sdk
    entrypoint: gcloud
    args:
      - run
      - deploy
      - '${_SERVICE_NAME}'
      - '--platform'
      - managed
      - '--region'
      - '${_REGION}'
      - '--allow-unauthenticated'
      - '--service-account'
      - '${_SERVICE_NAME}@${PROJECT_ID}.iam.gserviceaccount.com'
      - '--image'
      - 'gcr.io/${PROJECT_ID}/${_SERVICE_NAME}:${SHORT_SHA}'

Thanks!

Upvotes: 6

Views: 8364

Answers (1)

Rafael Lemos
Rafael Lemos

Reputation: 5829

Posting Comments from @GuillaumeBlaqueire and @Lsbister as a community wiki for increased visibility:

To deploy a Cloud Run service, use either the YAML (service.yaml) with gcloud run services replace OR the gcloud command gcloud run deploy. You can't use the service YAML with the "deploy" action.

If you only want to set the memory of your container to 8GBi using the deploy command, you should use the flag --memory for that.

Upvotes: 3

Related Questions