Cloud Run lets you run stateless images in a serverless environment. Using Cloud Build, you can deploy images from Container Registry (Deprecated) and Artifact Registry to Cloud Run. You can deploy an existing image, build and deploy an image, or automate the deployment.
Before you begin
-
Enable the Cloud Build, Cloud Run, Container Registry, and Resource Manager APIs.
Have your application source code ready. Your source code needs to be stored in a repository, such as Cloud Source Repositories, GitHub, or Bitbucket.
To run the
gcloud
commands in this page, install the Google Cloud CLI.If you're using VPC Service Controls, then set up a private pool in the VPC Service Controls perimeter. You must also configure Cloud Run for VPC Service Controls.
Required Identity and Access Management permissions
To get the permissions that you need to deploy to Cloud Run using Cloud Build, ask your administrator to grant you the following IAM roles on the default compute service account:
-
Cloud Run Developer (
roles/run.developer
) -
Logs Writer (
roles/logging.logWriter
) -
Artifact Registry Writer (
roles/artifactregistry.writer
) -
Service Account User (
roles/iam.serviceAccountUser
) -
Storage Admin (
roles/storage.admin
)
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
Building and deploying an image
Cloud Build lets you build an image, store the built image in Artifact Registry, and then deploy the image to Cloud Run.
To build and deploy an image:
In your project root directory, create a config file named
cloudbuild.yaml
.In the build config file, add
docker
build steps to build the image and push it to Artifact Registry, and then add agcloud
build step to invoke thegcloud run deploy
command to deploy the image on Cloud Run:steps: # Build the image - name: 'gcr.io/cloud-builders/docker' args: ['build', '-t', 'LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE', '.'] # Push the image to Artifact Registry - name: 'gcr.io/cloud-builders/docker' args: ['push', 'LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE'] # Deploy image to Cloud Run - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk' entrypoint: gcloud args: ['run', 'deploy', 'SERVICE_NAME', '--image', 'LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE', '--region', 'SERVICE_REGION'] images: - 'LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE'
Where:
REPOSITORY
is the name of the Artifact Registry repository from where you deploy your image.LOCATION
is the location of your Artifact Registry repository, such asus-east1
.PROJECT_ID
is your Google Cloud project ID where your image is stored.SERVICE_NAME
is the name of the Cloud Run service.SERVICE_REGION
is the region of the Cloud Run service you are deploying.IMAGE
is the name of your image in Artifact Registry.
Navigate to your project root directory and run the following command, where
LOCATION
is one of the supported build regions to run the build:gcloud builds submit --region=LOCATION
After successful completion, a success message is displayed along with the URL of the deployed service.
Continuous deployment
You can automate the deployment of your software to Cloud Run by creating Cloud Build triggers. You can configure your triggers to build and deploy images whenever you update your source code.
To automate your deployment:
In your repository root, add a config file named
cloudbuild.yaml
with steps to build the image, push the image to Container Registry, and then invoke thegcloud run deploy
command:steps: # Build the image - name: 'gcr.io/cloud-builders/docker' args: ['build', '-t', 'LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE/SERVICE_NAME:$COMMIT_SHA', '.'] # Push the image to Artifact Registry - name: 'gcr.io/cloud-builders/docker' args: ['push', 'LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE/SERVICE_NAME:$COMMIT_SHA'] # Deploy image to Cloud Run - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk' entrypoint: gcloud args: - 'run' - 'deploy' - 'SERVICE_NAME' - '--image' - 'LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE/SERVICE_NAME:$COMMIT_SHA' - 'region' - 'SERVICE_REGION' images: - 'LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE/SERVICE_NAME:$COMMIT_SHA'
Where:
REPOSITORY
is the name of the Artifact Registry repository from where you deploy your image.LOCATION
is the location of your Artifact Registry repository, such asus-east1
.PROJECT_ID
is your Google Cloud project ID where your image is stored.SERVICE_NAME
is the name of the Cloud Run service.SERVICE_REGION
is the region of the Cloud Run service you are deploying.IMAGE
is the name of your image in Artifact Registry.
The
$COMMIT_SHA
substitution variable is populated by Cloud Build when triggered from a Git repository.Create a build trigger with the config file created in the previous step:
Open the Triggers page:
Click Create Trigger.
In the Name field, enter a name for your trigger.
Under Region, select the region for your trigger.
Under Event, select the repository event to start your trigger.
Under Source, select your repository and the branch or tag name that will start your trigger. For more information on specifying which branches to autobuild, see Creating a build trigger.
Under Configuration, select Cloud Build configuration file (YAML or JSON).
In the Cloud Build configuration file location field, type
cloudbuild.yaml
after the/
.Click Create to save your build trigger.
Now, when you push new code to your repository, Cloud Build invokes a build and deploys the service to Cloud Run.
For more information on creating Cloud Build triggers, see Creating and managing build triggers.
Using minimal IAM permissions
When an image is deployed to a Cloud Run service, the image runs using the identity of the Runtime Service Account from the Cloud Run service. Because Cloud Build can deploy new images automatically, Cloud Build needs to be able to act as the Runtime Service Account of your Cloud Run service.
To grant limited access to Cloud Build to deploy to a Cloud Run service, do the following:
Console
Go to the Service accounts page of the Google Cloud console:
Click the email address of your Cloud Run service's Runtime Service Account (by default, it is
PROJECT_NUMBER-compute@developer.gserviceaccount.com
).Click the Permissions tab.
Click
Grant access.Enter the Cloud Build Service Account (
PROJECT_NUMBER@cloudbuild.gserviceaccount.com
)In the Select a role drop-down, select the Service Accounts > Service Account User role.
Click Save.
gcloud
Use the gcloud iam service-accounts add-iam-policy-binding
command, where
PROJECT_NUMBER
is the numeric ID of your project:
gcloud iam service-accounts add-iam-policy-binding \ PROJECT_NUMBER-compute@developer.gserviceaccount.com \ --member="serviceAccount:PROJECT_NUMBER@cloudbuild.gserviceaccount.com" \ --role="roles/iam.serviceAccountUser"
Replace PROJECT_NUMBER with the numeric ID of your project.
If you use Cloud Run with a
customized service identity,
then replace
PROJECT_NUMBER-compute@developer.gserviceaccount.com
with your service account address.
See Deployment permissions for more information.
Code examples
Here are some sample repositories, each of which contains a sample application and a build config file to deploy application to Cloud Run:
- deploy-prebuilt: A code example that shows how to deploy a prebuilt image to Cloud Run.
- run-example-builddeploy: A code example that shows how to build and deploy an image to Cloud Run.
What's next
- Learn how to use Cloud Deploy to deploy to Cloud Run.
- Learn how to deploy on GKE.
- Learn how to deploy on Cloud Run functions.
- Learn how to deploy on App Engine.
- Learn how to deploy on Firebase.
- Learn how to perform blue-green deployments on Compute Engine.
- Learn how to troubleshoot build errors.