Continuous Deployment from git using Cloud Build

The following stages occur in an automated deployment orchestrated by a Cloud Build Trigger:

  • Build the Docker Image
  • Push the image to the Container Registry
  • Deploy a new revision to the Cloud Run service

Setting up continuous deployment with Cloud Build

You can automate a deployment to Cloud Run using Cloud Build when new commits are pushed to a given branch of a git repository. This includes Cloud Source Repositories, GitHub, or any other repository supported by Cloud Build. For more information, see Automating Builds using Build Triggers.

To automate deployment with Cloud Build:

  1. In your repository root, add a file named cloudbuild.yaml that has these entries:

      # build the container image
    - name: ''
      args: ['build', '-t', '$PROJECT_ID/[SERVICE-NAME]', '.']
      # push the container image to Container Registry
    - name: ''
      args: ['push', '$PROJECT_ID/[SERVICE-NAME]']
      # Deploy container image to Cloud Run
    - name: ''
      args: ['beta', 'run', 'deploy', '[SERVICE-NAME]', '--image', '$PROJECT_ID/[SERVICE-NAME]', '--region', '[REGION]','--platform', '[PLATFORM]', '--quiet']


    • [PLATFORM] with managed if deploying to fully managed Cloud Run, or with gke if deploying to Cloud Run on GKE.
    • [SERVICE-NAME] with the name of the Cloud Run service.
    • [REGION] with the region of the Cloud Run service you are deploying to. If you are using Cloud Run on GKE, use --cluster and --cluster-location instead of the --region parameter.
  2. Grant the "Cloud Run Admin" and "Service Account User" roles to the Cloud Build service account:

    1. Visit the IAM and Admin page in the GCP Console.

    2. Select the service account that has the suffix

    3. Click the service account's edit icon and add to it the following roles:

      1. Cloud Run > Cloud Run Admin to allow Cloud Build to manipulate Cloud Run resources.

      2. Service Accounts > Service Account User to allow Cloud Build to act as other service accounts, which include your Cloud Run services.

    4. Click Save to save your changes.

  3. Create a build trigger in the Cloud Build triggers page.

    1. Click Create Trigger.
    2. From the displayed repository list, select your repository and click Continue. For more information on specifying which branches to autobuild, see Creating a build trigger.
    3. Select cloudbuild.yaml in Build Configuration.
    4. Click Create.
  4. You are finished! From this point on, anytime you push to your repository, you automatically trigger a build and a deployment to your Cloud Run service.

Continuous deployment with minimal IAM permissions

When a container is deployed to a Cloud Run service, it runs with the identity of the Runtime Service Account of this Cloud Run service. Because Cloud Build can deploy new containers automatically, Cloud Build needs to be able to act as the Runtime Service Account of your Cloud Run service.

Instead of allowing Cloud Build to act as any service account, you can allow it to only "act as" your Cloud Run Runtime Service account. Follow the instructions to deploy artifacts to Cloud Build to set up least-privilege deployment.



Cloud Run Documentation