Server Deployment to Google Cloud
Relevant source files
The following files were used as context for generating this wiki page:The PeP5 application is designed to be deployed to Google Cloud Platform (GCP) using a combination of managed services. This ensures a scalable, secure, and maintainable environment. The deployment process leverages Docker for containerization, Cloud Build for continuous integration and deployment (CI/CD), Cloud Run for serving the web application, Cloud SQL for the database backend, and Artifact Registry for storing container images.
Deployment can be performed manually using gcloud CLI commands, as outlined in the documentation, or automated through a combination of a PowerShell script (deploy-gcloud.ps1) and a Cloud Build configuration file (cloudbuild.yaml). This infrastructure hosts the main web application, its database, and the necessary files for the client-side PeP.ExamApp.
Architecture Overview
The production environment on Google Cloud consists of several interconnected services. Cloud Run serves the containerized ASP.NET Core application, which connects to a Cloud SQL for SQL Server instance for its data persistence. Docker images are built by Cloud Build and stored in Artifact Registry. The desktop ExamApp installer is hosted in a public Cloud Storage bucket.
This diagram illustrates the high-level architecture of the PeP5 application on Google Cloud.
Sources: DEPLOY-GCLOUD.md, cloudbuild.yaml, deploy-gcloud.ps1
Deployment Components
Required Google Cloud Services
To deploy the application, several Google Cloud APIs must be enabled for the project. The deploy-gcloud.ps1 script automates this process.
| API Name | Description |
|---|---|
sqladmin.googleapis.com | Manages Cloud SQL instances. |
run.googleapis.com | Manages Cloud Run services. |
cloudbuild.googleapis.com | Runs automated builds. |
secretmanager.googleapis.com | Stores and manages secrets like connection strings. |
artifactregistry.googleapis.com | Stores and manages container images and other artifacts. |
Sources: deploy-gcloud.ps1:24-30
Cloud SQL for SQL Server
The application uses a Cloud SQL for SQL Server instance as its database backend.
- Instance: A SQL Server 2019 Standard instance is created with a custom tier (
db-custom-1-3840) and 10GB of auto-increasing storage. - Database: A database named
PePDbis created within the instance. - User: A dedicated SQL user (
pepuser) is created for the application to connect to the database.
Sources: DEPLOY-GCLOUD.md:29-43, deploy-gcloud.ps1:9
Secret Manager
To avoid hardcoding sensitive information, the database connection string is stored in Google Cloud's Secret Manager.
- Secret Name:
pep-db-connection - Content: The connection string for the Cloud SQL instance.
- Access: The Cloud Run service account is granted the
roles/secretmanager.secretAccessorrole to access this secret at runtime.
The application retrieves this secret and uses it for the ConnectionStrings__DefaultConnection configuration key.
Sources: DEPLOY-GCLOUD.md:45-54, DEPLOY-GCLOUD.md:66
Artifact Registry
An Artifact Registry repository is used to store the Docker images for the web application.
- Repository Name:
pep-docker - Format:
docker - Location: The specified region (e.g.,
us-central1).
The deploy-gcloud.ps1 script ensures this repository is created before attempting to build and push the image.
Sources: deploy-gcloud.ps1:38-45, cloudbuild.yaml:3
Cloud Build
Cloud Build is used for automating the process of building the Docker image, pushing it to Artifact Registry, and deploying it to Cloud Run.
The build process is defined in cloudbuild.yaml and consists of three main steps:
- Build Image: Builds the Docker image from the
Dockerfilein the repository root, tagging it with both the commit SHA and:latest. - Push Image: Pushes the tagged images to the Artifact Registry.
- Deploy to Cloud Run: Deploys the newly built image (referenced by commit SHA) to the
pep-webappCloud Run service.
This diagram shows the steps executed by Cloud Build.
Sources: cloudbuild.yaml:6-37
Cloud Run
The application is deployed as a stateless container on Cloud Run, which provides a fully managed, serverless platform.
Key configuration parameters for the Cloud Run service:
- Service Name:
pep-webapp - Image: Pulled from Artifact Registry.
- Memory/CPU: Configured with
512Miof memory and1CPU. - Port: The container listens on port
8080. - Scaling: Autoscales between 0 and 10 instances.
- Connectivity:
- Allows unauthenticated invocations to be publicly accessible.
- Connected to the Cloud SQL instance.
- Mounts the
pep-db-connectionsecret as an environment variable.
Sources: DEPLOY-GCLOUD.md:59-71, cloudbuild.yaml:26-37
Deployment Scripts
deploy-gcloud.ps1
This PowerShell script is designed to facilitate the initial setup and manual build process. It automates several key steps.
The flow of the script is as follows:
The script takes a mandatory ProjectId and optional Region, ServiceName, etc. It handles enabling APIs, creating the Docker repository if it doesn't exist, and submitting the current directory to Cloud Build for building and pushing the Docker image.
Sources: deploy-gcloud.ps1
cloudbuild.yaml
This file defines the CI/CD pipeline that automatically builds and deploys the application. It is typically triggered by commits to a connected source repository (e.g., on GitHub).
# cloudbuild.yaml
steps:
# Build the Docker image
- name: 'gcr.io/cloud-builders/docker'
args:
- 'build'
- '-t'
- '${_REGION}-docker.pkg.dev/$PROJECT_ID/${_REPOSITORY}/${_SERVICE_NAME}:latest'
- '.'
# Push to Artifact Registry
- name: 'gcr.io/cloud-builders/docker'
args:
- 'push'
- '${_REGION}-docker.pkg.dev/$PROJECT_ID/${_REPOSITORY}/${_SERVICE_NAME}:latest'
# Deploy to Cloud Run
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: gcloud
args:
- 'run'
- 'deploy'
- '${_SERVICE_NAME}'
- '--image'
- '${_REGION}-docker.pkg.dev/$PROJECT_ID/${_REPOSITORY}/${_SERVICE_NAME}:$COMMIT_SHA'
# ... other args
Sources: cloudbuild.yaml:6-37
Application Configuration
Environment Variables
The Cloud Run service is configured with environment variables that the ASP.NET Core application uses at runtime.
| Variable | Description | Source |
|---|---|---|
ConnectionStrings__DefaultConnection | The database connection string, injected from Secret Manager. | DEPLOY-GCLOUD.md:66 |
ASPNETCORE_ENVIRONMENT | Set to Production for the deployed environment. | DEPLOY-GCLOUD.md:77 |
ExamAppUpdate__DownloadUrl | The public URL to the PeP.ExamApp installer hosted on Cloud Storage. | DEPLOY-GCLOUD.md:78 |
Sources: DEPLOY-GCLOUD.md:75-78
ExamApp Update Endpoint
The PeP.ExamApp desktop client checks for updates by calling an API endpoint on the server. The server's configuration for this endpoint is managed through appsettings.json, which can be overridden by environment variables in Cloud Run.
- Endpoint:
GET /api/exam-app/update - Controller:
ExamAppController.cs - Function:
GetUpdateInfo
This endpoint reads configuration values from the ExamAppUpdate section and returns them as a JSON response.
// Controllers/ExamAppController.cs:28-42
[HttpGet("update")]
[AllowAnonymous]
public ActionResult<UpdateInfoResponse> GetUpdateInfo([FromQuery] string? currentVersion)
{
// Read update info from configuration
var updateSection = _configuration.GetSection("ExamAppUpdate");
var updateInfo = new UpdateInfoResponse
{
Version = updateSection["Version"] ?? "1.0.0",
DownloadUrl = updateSection["DownloadUrl"] ?? "",
// ... other properties
};
return Ok(updateInfo);
}
The DownloadUrl property is set via the ExamAppUpdate__DownloadUrl environment variable in Cloud Run, pointing to the installer in Cloud Storage.
Sources: Controllers/ExamAppController.cs:28-42, PeP.ExamApp/Installer/README.md:90-100, DEPLOY-GCLOUD.md:78
Health Check
The application exposes a health check endpoint at /health, which is standard practice for services running on Cloud Run to allow the platform to verify the service is responsive.
// Program.cs:81-81
app.MapGet("/health", () => Results.Ok(new { status = "healthy", timestamp = DateTime.UtcNow }));
Sources: Program.cs:81
ExamApp Installer Hosting
The compiled installer for the PeP.ExamApp is hosted on Google Cloud Storage to be publicly downloadable.
- A Cloud Storage bucket (e.g.,
gs://pep-downloads) is created. - The bucket's IAM policy is updated to grant
roles/storage.objectViewertoallUsers, making objects publicly readable. - The installer
.exefile is uploaded to the bucket. - The public URL (e.g.,
https://storage.googleapis.com/pep-downloads/PeP.ExamApp_Setup_1.0.0.exe) is then used as the value for theExamAppUpdate__DownloadUrlenvironment variable in Cloud Run.
Sources: DEPLOY-GCLOUD.md:83-95
Summary
The deployment architecture for PeP5 on Google Cloud provides a robust and automated system for running the application. By leveraging managed services like Cloud Run and Cloud SQL, the infrastructure minimizes operational overhead. The CI/CD pipeline, defined in cloudbuild.yaml, ensures that every code change is automatically built, tested (implicitly by building), and deployed, leading to a streamlined development and release cycle. The use of Secret Manager for connection strings and Cloud Storage for hosting client application installers further enhances the security and functionality of the overall system.