Loading
Portfolio
Rayden Li
Google Cloud Platform
Cloud Run and Cloud Build are often mentioned together, but they solve different problems:
The common workflow is:
Source code
↓
Cloud Build: test and build a container image
↓
Artifact Registry: store the image
↓
Cloud Run: deploy and run the image
↓
HTTPS endpoint
This guide explains both products, compares them, and deploys a small Node.js service using Docker, Artifact Registry, Cloud Build, and Cloud Run.
Cloud Build is Google Cloud's managed build service. It imports source code, runs a sequence of build steps in temporary workers, and produces artifacts such as container images or Java archives. A build can also run tests, security checks, database migrations, or deployment commands. Each build step runs in a container. Cloud Build overview
A typical cloudbuild.yaml might tell Cloud Build to:
You can start a build manually with the CLI, through the API, or automatically with a repository trigger after a commit or pull request.
Cloud Build does not host your application after the build completes. Its workers are temporary and are removed after the build.
Use Cloud Build when you need repeatable CI/CD, cloud-based Docker builds, automated tests, or deployment pipelines without maintaining a Jenkins server or another build machine.
Cloud Run is a fully managed serverless application platform. It runs code or container images on Google's infrastructure without requiring you to create a VM or Kubernetes cluster. Cloud Run supports services, jobs, worker pools, and instances. This guide uses a service, which provides a stable HTTPS endpoint and automatically scales its container instances. What is Cloud Run?
A Cloud Run service is useful for:
Cloud Run services are private by default. You can keep authentication enabled or deliberately make a public website or API accessible without authentication.
For a service, the container must listen on 0.0.0.0 and use the port supplied through the PORT environment variable. Cloud Run uses port 8080 by default. Cloud Run container contract
| Question | Cloud Build | Cloud Run |
|---|---|---|
| Main purpose | Build, test, package, and deploy software | Run applications and workloads |
| Input | Source code and a build configuration | A container image or supported source deployment |
| Output | Images, packages, logs, test results, or deployments | A running service, job, worker, or instance |
| Lifetime | Temporary; ends when the build finishes | Continues serving or executing after deployment |
| Public endpoint | No application endpoint | A service receives a stable HTTPS endpoint |
| Scaling | Starts workers for builds | Scales application instances according to demand |
| Common billing unit | Build minutes | CPU, memory, requests, and networking |
| Typical use | CI/CD and artifact creation | Application hosting and workload execution |
The shortest accurate summary is:
Cloud Build turns source code into a deployable artifact. Cloud Run runs the deployed artifact.
No. They integrate well, but they are independent.
gcloud run deploy --source . combines them for convenience: Cloud Run sends the source to Cloud Build, which builds an image and stores it in Artifact Registry before deployment. Deploy Cloud Run from sourceUse both when you want a managed path from source code to a running, automatically scaled application.
For supported languages, Cloud Run can deploy directly from source:
gcloud run deploy hello-service \
--source . \
--region us-central1 \
--no-invoker-iam-check
Cloud Build performs the build behind the scenes. If needed, Google Cloud creates an Artifact Registry repository named cloud-run-source-deploy. This approach is excellent for a first deployment or a small service.
Use an explicit cloudbuild.yaml when you need visible build steps, tests, stable artifact naming, multiple environments, or repository triggers. The rest of this guide demonstrates that path.
Create server.js:
const http = require('node:http')
const port = Number(process.env.PORT || 8080)
http
.createServer((_request, response) => {
response.writeHead(200, { 'Content-Type': 'application/json' })
response.end(JSON.stringify({ message: 'Hello from Cloud Run' }))
})
.listen(port, '0.0.0.0', () => {
console.log(`Listening on port ${port}`)
})
This example uses only Node.js built-in modules, so it does not need dependencies or a package.json.
Create Dockerfile:
FROM node:24-slim
WORKDIR /app
ENV NODE_ENV=production
COPY --chown=node:node server.js ./
USER node
EXPOSE 8080
CMD ["node", "server.js"]
The container runs as the image's non-root node user. The application reads PORT at runtime and listens on every network interface, as required by Cloud Run.
Create .dockerignore:
.git
.env*
node_modules
npm-debug.log*
README.md
Install the Google Cloud CLI, create or select a project with billing enabled, and run:
gcloud init
export PROJECT_ID="your-google-cloud-project-id"
export REGION="us-central1"
export REPOSITORY="apps"
export SERVICE_NAME="hello-service"
export RUNTIME_SA_NAME="cloud-run-hello"
export RUNTIME_SA_EMAIL="${RUNTIME_SA_NAME}@${PROJECT_ID}.iam.gserviceaccount.com"
gcloud config set project "$PROJECT_ID"
gcloud services enable \
cloudbuild.googleapis.com \
run.googleapis.com \
artifactregistry.googleapis.com \
iam.googleapis.com \
cloudresourcemanager.googleapis.com
Create a Docker repository in Artifact Registry:
gcloud artifacts repositories create "$REPOSITORY" \
--project="$PROJECT_ID" \
--location="$REGION" \
--repository-format=docker \
--description="Application container images"
Artifact Registry stores the image between the build and the deployment. Create an Artifact Registry repository
Create a dedicated identity for the running application:
gcloud iam service-accounts create "$RUNTIME_SA_NAME" \
--project="$PROJECT_ID" \
--display-name="Cloud Run hello service"
Cloud Build's default execution account can differ between projects. Newer projects commonly use the Compute Engine default account, while older projects may use the legacy Cloud Build account. Discover the actual account instead of guessing it: Cloud Build default-account changes
export BUILD_SA_EMAIL="$(gcloud builds get-default-service-account \
--project="$PROJECT_ID")"
echo "$BUILD_SA_EMAIL"
Grant the build account the roles in Google's documented Cloud Build-to-Cloud-Run baseline:
for ROLE in \
roles/run.admin \
roles/storage.admin \
roles/artifactregistry.writer \
roles/logging.logWriter \
roles/cloudbuild.builds.editor
do
gcloud projects add-iam-policy-binding "$PROJECT_ID" \
--member="serviceAccount:${BUILD_SA_EMAIL}" \
--role="$ROLE"
done
gcloud iam service-accounts add-iam-policy-binding "$RUNTIME_SA_EMAIL" \
--project="$PROJECT_ID" \
--member="serviceAccount:${BUILD_SA_EMAIL}" \
--role="roles/iam.serviceAccountUser"
The last binding lets the build account attach the runtime identity to the Cloud Run service. Your signed-in account must also be allowed to submit Cloud Build builds. Organization-managed projects may require an administrator to grant these roles. Deploying to Cloud Run using Cloud Build
For a production pipeline, Google recommends a dedicated user-managed Cloud Build account with permissions narrowed to the required repository, Cloud Run service, log/source storage, and runtime identity. The default account keeps this introductory example shorter. User-managed Cloud Build service accounts
cloudbuild.yamlCreate cloudbuild.yaml:
steps:
- id: build
name: gcr.io/cloud-builders/docker
args:
- build
- --tag
- ${_REGION}-docker.pkg.dev/$PROJECT_ID/${_REPOSITORY}/${_IMAGE}:$BUILD_ID
- .
- id: push
name: gcr.io/cloud-builders/docker
args:
- push
- ${_REGION}-docker.pkg.dev/$PROJECT_ID/${_REPOSITORY}/${_IMAGE}:$BUILD_ID
- id: deploy
name: gcr.io/google.com/cloudsdktool/cloud-sdk
entrypoint: gcloud
args:
- run
- deploy
- ${_SERVICE}
- --image
- ${_REGION}-docker.pkg.dev/$PROJECT_ID/${_REPOSITORY}/${_IMAGE}:$BUILD_ID
- --region
- ${_REGION}
- --service-account
- ${_RUNTIME_SA}
- --no-invoker-iam-check
- --quiet
images:
- ${_REGION}-docker.pkg.dev/$PROJECT_ID/${_REPOSITORY}/${_IMAGE}:$BUILD_ID
substitutions:
_REGION: us-central1
_REPOSITORY: apps
_IMAGE: hello
_SERVICE: hello-service
_RUNTIME_SA: cloud-run-hello@$PROJECT_ID.iam.gserviceaccount.com
options:
dynamicSubstitutions: true
$PROJECT_ID and $BUILD_ID are built-in Cloud Build substitutions. $BUILD_ID is available for manual and triggered builds, while $COMMIT_SHA may be empty for a manually submitted build. A unique tag makes each deployed image traceable. Cloud Build substitutions
The deploy command uses --no-invoker-iam-check because this example is a public HTTP service. Omit that flag to keep the service private. Google currently recommends disabling the Invoker IAM check when unauthenticated public access is intentional, although an organization policy can prohibit it. Cloud Run public access
gcloud builds submit . \
--project="$PROJECT_ID" \
--region="$REGION" \
--config=cloudbuild.yaml \
--substitutions="_REGION=${REGION},_REPOSITORY=${REPOSITORY},_SERVICE=${SERVICE_NAME},_RUNTIME_SA=${RUNTIME_SA_EMAIL}"
Cloud Build now builds the image, pushes it to Artifact Registry, and deploys a new Cloud Run revision.
export SERVICE_URL="$(gcloud run services describe "$SERVICE_NAME" \
--project="$PROJECT_ID" \
--region="$REGION" \
--format='value(status.url)')"
echo "$SERVICE_URL"
curl --fail --show-error "$SERVICE_URL"
Expected response:
{"message":"Hello from Cloud Run"}
To deploy an update, change the source and run the same gcloud builds submit command again. Cloud Run creates an immutable revision for each successful deployment.
Use Cloud Run without a custom Cloud Build pipeline when you already have a container image or when gcloud run deploy --source . provides enough automation.
Use Cloud Build without Cloud Run when you need CI, tests, container or package builds, or deployments to another platform.
Use both when you want a managed CI/CD path from a commit to an Artifact Registry image and a running Cloud Run revision.
Cloud Run and Cloud Build are billed separately:
Always check the linked pricing pages rather than treating a quoted allowance as permanent.
localhost instead of 0.0.0.0, or ignoring the injected PORT.$COMMIT_SHA as the only tag for manual builds; use $BUILD_ID when the build might not come from a trigger.The following commands are destructive. Confirm the service and repository names before running them:
gcloud run services delete "$SERVICE_NAME" \
--project="$PROJECT_ID" \
--region="$REGION"
gcloud artifacts repositories delete "$REPOSITORY" \
--project="$PROJECT_ID" \
--location="$REGION"
gcloud iam service-accounts delete "$RUNTIME_SA_EMAIL" \
--project="$PROJECT_ID"
Deleting a Cloud Run service does not delete its images. Deleting the Artifact Registry repository deletes every artifact stored in that repository, including images used by other services. Manage Cloud Run services · Delete Artifact Registry repositories
Cloud Build and Cloud Run occupy two different stages of delivery:
Cloud Build = prepare and deliver
Cloud Run = execute and serve
Start with gcloud run deploy --source . when you need the shortest path. Add an explicit Cloud Build pipeline when tests, repeatability, traceable images, automated triggers, or multiple deployment environments justify it.