cloudrun.go
Overview
The cloudrun.go file implements the command-line interface and deployment logic for deploying an application to Google Cloud Run. It automates the process of compiling a Go server executable, preparing a Dockerfile, deploying the containerized application to Cloud Run, and running a local proxy that adds authentication headers for secure access. This file uses the cobra package for CLI commands and integrates tightly with Google Cloud SDK commands (gcloud) for deployment and proxy management.
The deployment workflow includes:
Parsing command-line flags to configure deployment parameters.
Compiling the Go entry point into a statically linked Linux executable.
Creating a Dockerfile tailored for Cloud Run deployment.
Executing
gcloudcommands to deploy the service and run a local authenticated proxy.
Types and Structures
gCloudFlags
Holds Google Cloud Platform specific configuration flags.
Field | Type | Description |
|---|---|---|
| string | GCP region for deployment. |
string | GCP project name. |
cloudRunServiceFlags
Contains flags specifying the Cloud Run service configuration.
Field | Type | Description |
|---|---|---|
string | Name of the Cloud Run service to deploy. | |
int | Port on which the server listens inside Cloud Run. | |
string | URL for Agent-to-Agent (A2A) agent card. | |
| bool | Enable A2A communication (true/false). |
| bool | Enable API functionality (true/false). |
| bool | Enable Web UI functionality (true/false). |
localProxyFlags
Specifies configuration for the local proxy that adds authentication headers.
Field | Type | Description |
|---|---|---|
| int | Port on which the local proxy listens. |
buildFlags
Contains build-related flags and paths.
Field | Type | Description |
|---|---|---|
| string | Temporary directory for the build process. Defaults to OS temp dir if empty. |
string | Full path to the compiled executable output. | |
string | Executable filename derived from entry point. | |
| string | Path to the temporary Dockerfile created for deployment. |
sourceFlags
Holds source code location flags.
Field | Type | Description |
|---|---|---|
string | Directory of the entry point source file. | |
string | Entry point file path for the Go server. |
deployCloudRunFlags
Aggregates all the above flag groups for complete deployment configuration.
Field | Type | Description |
|---|---|---|
| Google Cloud deployment flags. | |
Cloud Run service configuration. | ||
| Local proxy configuration. | |
| Build process configuration. | |
| Source code location flags. |
Variables
flags- An instance ofdeployCloudRunFlagsholding all CLI-flag values.cloudrunCmd- A cobra.Command representing thecloudrunCLI subcommand.
Functions and Methods
init()
Registers the cloudrun subcommand under the parent deploy.DeployCmd and binds all persistent flags to the flags structure.
Flags include region, project name, service name, ports, entry point path, and feature toggles (A2A, API, WebUI).
Enables seamless user CLI input mapping to internal configuration.
(f *deployCloudRunFlags) computeFlags() error
Processes and validates CLI flag values to prepare for deployment.
Converts relative paths (entryPointPath,
tempDir) to absolute paths.Creates a temporary directory inside the specified or default temp directory.
Derives executable names by stripping
.goextension from the entry point file.Sets execPath, execFile, and
dockerfileBuildPathbased on temp directory and source paths.
Usage example:
err := flags.computeFlags()
if err != nil {
// handle error
}
(f *deployCloudRunFlags) cleanTemp() error
Removes the temporary directory created during build and deployment.
Ensures cleanup of artifacts and temp files after deployment.
(f *deployCloudRunFlags) compileEntryPoint() error
Compiles the Go entry point into a statically linked Linux executable targeted for Cloud Run.
Uses
go buildwith-ldflags="-s -w"to strip debug info and reduce binary size.Sets environment variables for static linking (
CGO_ENABLED=0) and target OS/arch (linux/amd64).Executes build command in the directory of the source entry point.
(f *deployCloudRunFlags) prepareDockerfile() error
Generates a Dockerfile inside the temporary directory for building the container image.
Uses
gcr.io/distroless/static-debian11base image.Copies the compiled executable into
/app/directory inside the container.Exposes the configured server port.
Constructs the
CMDto run the executable with flags enabling Web UI, API, and A2A features based on user flags.Writes the Dockerfile content to
dockerfileBuildPath.
(f *deployCloudRunFlags) gcloudDeployToCloudRun() error
Executes gcloud run deploy command to deploy the containerized application to Cloud Run.
Uses the temporary directory as source context.
Sets region, project, ingress, and authentication flags.
Injects secrets (
GOOGLE_API_KEY) for secure deployment.
(f *deployCloudRunFlags) runGcloudProxy() error
Runs a local gcloud run services proxy command.
Starts an authenticated proxy on the local machine that forwards requests to the Cloud Run service.
Proxy adds authentication headers ensuring secure access to the deployed service.
Prints informative messages about URLs for Web UI and REST API endpoints.
Runs until interrupted (Ctrl-C).
(f *deployCloudRunFlags) deployOnCloudRun() error
Orchestrates the entire deployment sequence:
Computes flags and prepares environment.
Compiles the Go server executable.
Prepares the Dockerfile.
Deploys the container to Cloud Run using
gcloud.Cleans up temporary build files.
Launches the local proxy for authenticated access.
Each step logs its status and returns errors on failure.
Interactions with Other System Components
Integrates with the
deploypackage by registering itself as a subcommand (deploy.DeployCmd.AddCommand).Uses
utilpackage functions for logging command execution, printing, and path utilities.Relies on the
os/execpackage to run external commands (go build,gcloud).Depends on Google Cloud SDK (
gcloud) CLI tools for deploying and proxying services.Deploys a Docker container based on a locally compiled binary and Dockerfile prepared dynamically.
The local proxy enables secure interaction with Cloud Run services during development or testing, facilitating Web UI and REST API access.
Implementation Details and Algorithms
Temporary directories are created with timestamped naming to avoid collisions.
Executable naming derives from the Go entry point filename, stripping
.goextension.The Dockerfile is built as a multi-line string with conditional flags to enable/disable features.
The
go buildcommand uses explicit environment variables to ensure compatibility with Cloud Run Linux environment.Deployment parameters such as region, project, service name, ports, and feature toggles are fully configurable via CLI flags.
Proxy run uses fixed port or user-defined port and prints user-friendly instructions.
Usage Example
Deploy an application to Cloud Run with all features enabled and custom parameters:
cloudrun \
--project_name=my-gcp-project \
--region=us-central1 \
--service_name=my-cloudrun-service \
--entry_point_path=./cmd/server/main.go \
--proxy_port=8081 \
--server_port=8080 \
--a2a=true \
--api=true \
--webui=true
Visual Diagram
flowchart TD
A[cloudrunCmd] --> B[deployOnCloudRun]
B --> C[computeFlags]
B --> D[compileEntryPoint]
B --> E[prepareDockerfile]
B --> F[gcloudDeployToCloudRun]
B --> G[cleanTemp]
B --> H[runGcloudProxy]
style A fill:#f9f,stroke:#333,stroke-width:1px
style B fill:#bbf,stroke:#333,stroke-width:1px
classDef success fill:#bbf,stroke:#333,stroke-width:1px
This diagram illustrates the main workflow triggered by the cloudrun CLI command, showing the sequential execution of key methods responsible for deployment and running the local proxy.