Security & CI/CD on GCP
Secret Manager stores credentials with versioning and IAM; Cloud KMS manages encryption keys; Cloud Build automates the build-test-deploy pipeline; Terraform and Deployment Manager express infrastructure as code. Learn the secure, automated delivery stack and how to keep cost under control.
What is this?
Secret Manager stores API keys, passwords and certificates as versioned secrets with IAM-controlled access and audit logging. Cloud KMS manages the encryption keys that protect data at rest. Cloud Build compiles and packages code into artifacts using a pipeline defined in cloudbuild.yaml. Deployment Manager and Terraform are the infrastructure-as-code tools. Together they give you a secure, automated delivery path from commit to production.
Why do you need it?
- Secrets in code and config are the number one leak vector — Secret Manager removes them.
- Cloud KMS centralizes key management so you can encrypt, rotate and audit keys consistently.
- Cloud Build automates the build-test-package flow in a reproducible pipeline.
- Infrastructure as code (Terraform, Deployment Manager) makes environments reproducible and reviewable.
- Cost optimization — committed use discounts, spot VMs, right-sizing — is part of the job.
Mental model
A secure vault and a factory floor
GCP security and CI/CD
Secret Manager is the bank vault: every credential has its own box (version), access is logged, and only badge holders (IAM) can open it. Cloud KMS is the master locksmith: it owns the master keys that encrypt everything else and rotates them on schedule. Cloud Build is the factory line: raw code goes in one end and a tested package comes out, with every step recorded. Terraform and Deployment Manager are the blueprints department: the whole factory is rebuilt exactly from drawings.
How it works
- Secret Manager stores secret versions; IAM controls who may access or manage them, and version access is logged to Cloud Audit Logs.
- Cloud KMS creates key rings and keys; you encrypt data with the key and rotate it on a schedule, with the old key kept for decryption.
- Cloud Build reads cloudbuild.yaml from your repo, runs build and test steps in containers, and pushes artifacts to Artifact Registry.
- Cloud Build can trigger on pushes, schedules or pull requests, and grants each build a service account with its own permissions.
- Terraform or Deployment Manager declares the infrastructure; a CI pipeline applies it and re-plans to detect drift.
A secure CI/CD path from commit to production on GCP.
Key commands
gcloud secrets create my-db-password --replication-policy=automaticCreate a secret with automatic replication.
gcloud secrets versions add my-db-password --data-file=-Add a new version of a secret from stdin.
gcloud secrets versions access latest --secret=my-db-passwordRetrieve the latest version of a secret.
gcloud secrets listList the secrets in the project.
gcloud builds submit --config=cloudbuild.yamlRun a build pipeline defined in cloudbuild.yaml.
gcloud services enable run.googleapis.comEnable an API for the project — often the first step in a pipeline.
Examples
Store a secret and run a Cloud Build
$ printf "db-password-xyz" | gcloud secrets create my-db-password \ --data-file=- --replication-policy=automaticCreated version [1] of the secret [my-db-password]. $ gcloud secrets versions access latest --secret=my-db-passworddb-password-xyz $ gcloud builds submit --config=cloudbuild.yamlCreating temporary tarball archive...SUCCESSDONEgcloud secrets create with --data-file=- reads the value from stdin and creates version 1 without the secret ever landing in a file. versions access retrieves the current value for a verified consumer. gcloud builds submit runs the steps declared in cloudbuild.yaml and reports SUCCESS when the pipeline completes.
In production
- Reference Secret Manager from Cloud Run, Cloud Functions and GKE so credentials never live in images.
- Encrypt sensitive data and backups with Cloud KMS customer-managed keys and rotate them on a schedule.
- Build a CI/CD pipeline with Cloud Build that runs tests, scans images, and deploys on merge.
- Manage all infrastructure as code with Terraform and store state in a versioned Cloud Storage bucket.
- Optimize cost with committed use discounts, spot VMs and right-sizing as part of the deployment workflow.
Best practices
- Never commit secrets; load them from Secret Manager at runtime.
- Grant the build service account the least privilege it needs to push artifacts and deploy.
- Use Terraform with a remote state backend so the team shares one source of truth.
- Rotate secrets and keys on a schedule and audit access with Cloud Audit Logs.
- Enable deletion protection on secrets and databases so a mistaken command cannot destroy them.
Common mistakes
- Hardcoding credentials in config files or container images and calling it temporary.
- Checking service account JSON keys into the repository.
- Giving one broad role to the CI service account so a compromised build can do anything.
- Running builds that are not reproducible because they depend on mutable tags.
- Applying Terraform without a plan review and letting the environment drift.
Common issues
Secret access deniedThe caller's IAM role lacks secretmanager.versions.access, or the secret version is disabled.
Grant roles/secretmanager.secretAccessor to the identity and check version states with gcloud secrets versions list.
Cloud Build step failedThe step's command or config is wrong, or the build service account lacks permission.
Read the build log for the exact step and error, fix the config, and verify the service account roles.
Terraform plan shows unexpected driftSomeone changed infrastructure outside of IaC.
Re-run the plan to see the difference, then update the Terraform config to match reality.
Debugging tips
- Check Cloud Build logs in the console or with gcloud builds log <build-id>.
- Verify the build's service account has IAM permission for the deploy target before debugging the pipeline.
- Use gcloud secrets versions access and gcloud secrets versions list to confirm which version is current.
- For KMS errors, check the key ring, location and key version state — disabled keys break encryption.
- Test gcloud builds submit in a small repo before running the full pipeline.
Interview checkpoint
Hands-on lab
- Store a secret with locked-down file permissions
- Verify the secret is readable only by the owner
- Compute and store an integrity checksum
- Write and run a strict-mode build pipeline script
Secure a File and Run a Local Pipeline
30 min · advanced
Quick quiz
Which GCP service stores API keys and passwords as versioned, IAM-protected entries?
Cloud KMS is a managed service for creating and rotating encryption keys.
A Cloud Build step cannot push an image to Artifact Registry. What should you check first?
Cheat sheet
Secret ManagerVersioned, IAM-protected secretsCloud KMSManaged encryption keys with rotationCloud Audit LogsWho did what, whenOrg policiesConstraints applied to the hierarchyCloud BuildContainerized build pipelineArtifact RegistryStores images and packagesTerraformDeclarative IaC with remote stateDeployment ManagerNative GCP IaC