Google Cloud Build is cool. Google Cloud Run is awesome. But when configuring Google Cloud Build to automatically deploy your built container to Google Cloud Run you might see this error:
ERROR: (gcloud.run.deploy) PERMISSION_DENIED: The caller does not have permission
If you’re seeing this error you forgot to set up the required IAM Permissions for the Cloud Build Service Account. Below I’ll show you the commands to fix this error.
~
As detailed in the Cloud Run documentation, a user needs the following permissions to deploy new Cloud Run services or revisions:
run.services.create
andrun.services.update
on the project level. Typically assigned through theroles/run.admin
role.iam.serviceAccounts.actAs
for the Cloud Run runtime service account. By default, this isPROJECT_NUMBER-compute@developer.gserviceaccount.com
. The permission is typically assigned through theroles/iam.serviceAccountUser
role.
By default – for security reasons – the Cloud Build Service Account does not have the permissions to manage Cloud Run, explaining why you’re getting errors.
~
You can set up these required permissions using the Google Cloud Console, yet I prefer to do this from the CLI using the Google Cloud SDK (gcloud
), by invoking the commands below:
# Config
GC_PROJECT=your-gcp-project-id
GC_PROJECT_NUMBER=your-gcp-project-number
# Grant the Cloud Run Admin role to the Cloud Build service account
gcloud projects add-iam-policy-binding $GC_PROJECT \
--member "serviceAccount:$GC_PROJECT_NUMBER@cloudbuild.gserviceaccount.com" \
--role roles/run.admin
# Grant the IAM Service Account User role to the Cloud Build service account on the Cloud Run runtime service account
gcloud iam service-accounts add-iam-policy-binding \
$GC_PROJECT_NUMBER-compute@developer.gserviceaccount.com \
--member="serviceAccount:$GC_PROJECT_NUMBER@cloudbuild.gserviceaccount.com" \
--role="roles/iam.serviceAccountUser"
💡 To know the values for GC_PROJECT(_ID)
and GC_PROJECT_NUMBER
, run gcloud projects list
or go to the Home of your project inside Cloud Console.
After running these two commands, re-trigger a build and Google Cloud Build will be able to deploy your built container onto Google Cloud Run.
~
Thank me with a coffee.
I don\'t do this for profit but a small one-time donation would surely put a smile on my face. Thanks!
To stay in the loop you can follow @bramus or follow @bramusblog on Twitter.
Thank you !! Struggling with this for hours. Working now, but I have to tackle the PORT issue and how to set it in YAML.