During source deployments, Cloud Run leverages Cloud Build
when building and deploying your Cloud Run service.

This page shows how to set a
[user-specified service account](https://docs.cloud.google.com/build/docs/cloud-build-service-account.md.txt#user_access_to_triggers)
for Cloud Build to use when executing builds of the service on your behalf.
This guide is relevant for platform developers who are deploying
Cloud Run [services](https://docs.cloud.google.com/run/docs/deploying-source-code.md.txt) or
[functions](https://docs.cloud.google.com/run/docs/deploy-functions.md.txt) using the Google Cloud CLI and need to
customize the build service account used by Cloud Build. The build service
account gcloud CLI flag is supported for
[source deployments](https://docs.cloud.google.com/run/docs/deploying-source-code.md.txt) (`--source`), and not
supported for [container image deployments](https://docs.cloud.google.com/run/docs/deploying.md.txt) (`--image`).

## Before you begin

1. Enable the Cloud Build API:

   ```bash
   gcloud services enable cloudbuild.googleapis.com
   ```
2. [Create a service account](https://docs.cloud.google.com/iam/docs/service-accounts-create.md.txt), or have an
   existing service account, to use as the Cloud Build service
   account.

### Required roles

You or your administrator must grant the deployer account and the
Cloud Build service account the following IAM roles.

#### Click to view required roles for the deployer account

To get the permissions that you need to build and deploy from source,
ask your administrator to grant you the following IAM
roles:

- [Cloud Run Source Developer](https://docs.cloud.google.com/iam/docs/roles-permissions/run.md.txt#run.sourceDeveloper) (`roles/run.sourceDeveloper`) on your project
- [Service Usage Consumer](https://docs.cloud.google.com/iam/docs/roles-permissions/serviceusage.md.txt#serviceusage.serviceUsageConsumer) (`roles/serviceusage.serviceUsageConsumer`) on your project
- [Service Account User](https://docs.cloud.google.com/iam/docs/roles-permissions/iam.md.txt#iam.serviceAccountUser) (`roles/iam.serviceAccountUser`) on the Cloud Run service identity

#### Click to view required roles for the Cloud Build service account

To allow the Cloud Build service account to perform the build
when deploying a function, ask your administrator to grant the
[Cloud Run Builder](https://docs.cloud.google.com/iam/docs/roles-permissions/run.md.txt#run.builder)
(`roles/run.builder`) role to the Cloud Build service
account on the project.

> [!NOTE]
> **Note:** If you need to use a service account from a different project, you or your administrator must grant additional access to the service account. See [Cross-project set up](https://docs.cloud.google.com/build/docs/securing-builds/configure-user-specified-service-accounts.md.txt#cross-project_set_up) for more details.

For a list of IAM roles and permissions that are associated with
Cloud Run, see
[Cloud Run IAM roles](https://docs.cloud.google.com/run/docs/reference/iam/roles.md.txt)
and [Cloud Run IAM permissions](https://docs.cloud.google.com/run/docs/reference/iam/permissions.md.txt).
If your Cloud Run service interfaces with
Google Cloud APIs, such as Cloud Client Libraries, see the
[service identity configuration guide](https://docs.cloud.google.com/run/docs/configuring/services/service-identity.md.txt).
For more information about granting roles, see
[deployment permissions](https://docs.cloud.google.com/run/docs/reference/iam/roles.md.txt#additional-configuration)
and [manage access](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).

## Specify a Cloud Build service account

By default, if a Cloud Build service account isn't specified when
deploying a [service](https://docs.cloud.google.com/run/docs/deploying-source-code.md.txt) or
[function](https://docs.cloud.google.com/run/docs/deploy-functions.md.txt#gcloud_1) from source,
Cloud Build uses the
[default Cloud Build service account](https://docs.cloud.google.com/build/docs/cloud-build-service-account.md.txt).

As a best practice for following the principle of least privilege to improve the
security posture of your service, we recommend that you specify your own service
account to run your builds when deploying a service from source.

### gcloud

To specify the Cloud Build service account when deploying a
service from source code, use the `--build-service-account` flag:

```bash
gcloud run deploy SERVICE \
    --source . \
    --build-service-account projects/PROJECT_ID/serviceAccounts/BUILD_SERVICE_ACCOUNT
```

Replace:

- <var translate="no">SERVICE</var> with name of your Cloud Run service.
- <var translate="no">PROJECT_ID</var> the project ID where the build service account is created.
- <var translate="no">BUILD_SERVICE_ACCOUNT</var> with a user-specified service account.

If you are deploying a function, add the `--function` flag with the function
entry point from your source code.