0
0
GcpHow-ToBeginner · 4 min read

How to Deploy from Source to Cloud Run on GCP

To deploy from source to Cloud Run, use the gcloud run deploy command with your source directory. This command builds a container from your source code and deploys it to Cloud Run automatically.
📐

Syntax

The basic command to deploy source code to Cloud Run is:

gcloud run deploy SERVICE_NAME --source=SOURCE_DIRECTORY --region=REGION --platform=managed

Here’s what each part means:

  • SERVICE_NAME: The name you want to give your Cloud Run service.
  • --source=SOURCE_DIRECTORY: The folder path where your source code is located.
  • --region=REGION: The Google Cloud region to deploy your service.
  • --platform=managed: Specifies you are using the fully managed Cloud Run service.
bash
gcloud run deploy SERVICE_NAME --source=SOURCE_DIRECTORY --region=REGION --platform=managed
💻

Example

This example deploys a simple Node.js app located in the ./my-app folder to Cloud Run in the us-central1 region with the service name my-node-service.

bash
gcloud run deploy my-node-service --source=./my-app --region=us-central1 --platform=managed
Output
Deploying container to Cloud Run service [my-node-service] in project [YOUR_PROJECT_ID] region [us-central1] ✓ Deploying new service... Done. ✓ Creating Revision... ✓ Routing traffic... Done. Service [my-node-service] revision [my-node-service-00001-abc] has been deployed and is serving 100 percent of traffic. Service URL: https://my-node-service-abcde.uc.r.appspot.com
⚠️

Common Pitfalls

Some common mistakes when deploying from source to Cloud Run include:

  • Not specifying the --source flag, which causes deployment to fail because Cloud Run doesn’t know where your code is.
  • Using an unsupported runtime or missing a Dockerfile or build configuration in your source folder.
  • Forgetting to set the correct --region matching your project setup.
  • Not having the gcloud CLI authenticated or configured with the right project.

Example of a wrong command and the fix:

# Wrong: missing --source flag
gcloud run deploy my-service --region=us-central1 --platform=managed

# Right: include --source
gcloud run deploy my-service --source=./app --region=us-central1 --platform=managed
📊

Quick Reference

Remember these tips for smooth deployment:

  • Always specify --source with your code folder.
  • Use --region where your Cloud Run service will live.
  • Ensure your source has a supported runtime or Dockerfile.
  • Authenticate with gcloud auth login and set your project with gcloud config set project PROJECT_ID.

Key Takeaways

Use the gcloud CLI with --source to deploy your source code directly to Cloud Run.
Specify the service name, source directory, region, and platform in the deploy command.
Ensure your source code has a supported runtime or Dockerfile for automatic container build.
Authenticate and set your Google Cloud project before deploying.
Double-check flags like --source and --region to avoid deployment errors.