CI/CD and Deployment - GCP Professional Cloud DevOps Engineer¶
Overview¶
Continuous integration, continuous deployment, release management, and deployment automation for cloud-native applications.
CI/CD Pipeline Architecture¶
Cloud Build Pipeline¶
Build Configuration:
steps:
# Unit tests
- name: 'gcr.io/cloud-builders/npm'
args: ['install']
- name: 'gcr.io/cloud-builders/npm'
args: ['test']
# Security scanning
- name: 'gcr.io/cloud-builders/gcloud'
args: ['beta', 'container', 'images', 'scan', 'gcr.io/$PROJECT_ID/app:$COMMIT_SHA']
# Build and push
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/app:$COMMIT_SHA', '.']
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/$PROJECT_ID/app:$COMMIT_SHA']
# Deploy
- name: 'gcr.io/cloud-builders/gke-deploy'
args:
- run
- --filename=k8s/
- --image=gcr.io/$PROJECT_ID/app:$COMMIT_SHA
- --cluster=production-cluster
- --location=us-central1
timeout: 1200s
options:
machineType: 'N1_HIGHCPU_8'
Automated Testing¶
Test Stages: 1. Unit Tests: Fast, isolated tests 2. Integration Tests: Component interaction tests 3. Security Scanning: Vulnerability detection 4. Performance Tests: Load and stress testing 5. Smoke Tests: Post-deployment validation
Binary Authorization¶
Policy Enforcement:
# Require attestations before deployment
admissionWhitelistPatterns:
- namePattern: gcr.io/my-project/*
defaultAdmissionRule:
requireAttestationsBy:
- projects/my-project/attestors/security-attestor
- projects/my-project/attestors/qa-attestor
enforcementMode: ENFORCED_BLOCK_AND_AUDIT_LOG
Deployment Strategies¶
Rolling Deployment¶
Kubernetes:
spec:
replicas: 10
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 2
maxUnavailable: 1
Blue-Green Deployment¶
# Deploy green environment
kubectl apply -f deployment-green.yaml
# Test green
kubectl port-forward service/app-green 8080:80
# Switch traffic
kubectl patch service app -p '{"spec":{"selector":{"version":"green"}}}'
# Rollback if needed
kubectl patch service app -p '{"spec":{"selector":{"version":"blue"}}}'
Canary Deployment¶
Cloud Deploy:
apiVersion: deploy.cloud.google.com/v1
kind: DeliveryPipeline
metadata:
name: my-pipeline
stages:
- targetId: staging
- targetId: prod-canary
profiles: [canary]
strategy:
canary:
runtimeConfig:
kubernetes:
serviceNetworking:
service: "my-app"
canaryDeployment:
percentages: [25, 50, 100]
verify: true
Infrastructure as Code¶
Terraform¶
Example Configuration:
resource "google_container_cluster" "primary" {
name = "production-cluster"
location = "us-central1"
remove_default_node_pool = true
initial_node_count = 1
}
resource "google_container_node_pool" "primary_nodes" {
cluster = google_container_cluster.primary.name
location = "us-central1"
node_count = 3
node_config {
machine_type = "n1-standard-2"
oauth_scopes = [
"https://www.googleapis.com/auth/cloud-platform"
]
}
autoscaling {
min_node_count = 1
max_node_count = 10
}
}
Cloud Deployment Manager¶
resources:
- name: production-cluster
type: container.v1.cluster
properties:
zone: us-central1-a
cluster:
initialNodeCount: 3
nodeConfig:
machineType: n1-standard-2
oauthScopes:
- https://www.googleapis.com/auth/cloud-platform
Release Management¶
GitOps Workflow¶
- Git as Single Source of Truth: All configs in Git
- Pull-Based Deployment: Agents pull from Git
- Automated Sync: Auto-deploy on Git changes
- Rollback via Git: Revert commits to rollback
Config Sync (Anthos):
apiVersion: configmanagement.gke.io/v1
kind: ConfigManagement
metadata:
name: config-management
spec:
git:
syncRepo: https://github.com/myorg/configs
syncBranch: main
secretType: none
policyDir: "/"
Feature Flags¶
from google.cloud import firestore
db = firestore.Client()
def is_feature_enabled(feature_name, user_id):
doc = db.collection('features').document(feature_name).get()
if not doc.exists:
return False
config = doc.to_dict()
if config.get('enabled_for_all'):
return True
return user_id in config.get('enabled_users', [])
# Usage
if is_feature_enabled('new_checkout', user.id):
return new_checkout_flow()
else:
return old_checkout_flow()
Best Practices¶
CI/CD¶
- Fast Feedback: Quick build and test cycles
- Automated Testing: Comprehensive test coverage
- Security Scanning: Integrate vulnerability scanning
- Artifact Management: Version and store build artifacts
- Deployment Automation: No manual deployment steps
- Rollback Capability: Easy rollback procedures
- Environment Parity: Dev, staging, prod consistency
- Audit Trail: Log all deployments and changes
Infrastructure as Code¶
- Version Control: All infrastructure in Git
- Modular Design: Reusable modules
- State Management: Secure remote state storage
- Testing: Validate before apply
- Documentation: Inline comments and README
- Drift Detection: Regular state reconciliation
- Review Process: PR reviews for infrastructure changes
Deployment¶
- Gradual Rollout: Canary or blue-green
- Health Checks: Automated health verification
- Monitoring: Real-time deployment monitoring
- Rollback Plans: Documented rollback procedures
- Feature Flags: Decouple deployment from release
- Database Migrations: Backward-compatible changes
- Zero Downtime: Design for continuous availability
Common Scenarios¶
Scenario: Multi-environment CI/CD pipeline Solution: Cloud Build with branch-based triggers, separate GKE clusters per environment, Cloud Deploy for progressive delivery
Scenario: Secure software supply chain Solution: Binary Authorization, Container Analysis, Artifact Registry vulnerability scanning, attestation-based deployments
Scenario: GitOps for Kubernetes Solution: Config Sync or Flux, Git as source of truth, automated reconciliation, PR-based changes
Scenario: Database migration in CD pipeline Solution: Separate migration step, backward-compatible changes, rollback-safe migrations, automated testing
Study Tips¶
- Hands-on practice: Build complete CI/CD pipelines
- Deployment strategies: Implement canary, blue-green, rolling
- IaC tools: Practice with Terraform and Deployment Manager
- Binary Authorization: Understand attestation-based deployments
- Cloud Deploy: Multi-environment progressive delivery
- GitOps: Config Sync and reconciliation
- Security integration: Scanning and attestation in pipeline
Key Commands¶
# Cloud Build
gcloud builds submit --config=cloudbuild.yaml
gcloud builds triggers create github --repo-name=REPO --branch-pattern="^main$"
# Cloud Deploy
gcloud deploy apply --file=clouddeploy.yaml --region=us-central1
gcloud deploy releases create release-001 --delivery-pipeline=my-pipeline --region=us-central1
# Binary Authorization
gcloud beta container binauthz attestors create ATTESTOR_NAME
gcloud beta container binauthz attestations create --artifact-url=ARTIFACT_URL --attestor=ATTESTOR_NAME
# Terraform
terraform init
terraform plan
terraform apply
terraform destroy