Implementing Deployment Strategies
1. Deploying to AWS
| Property | Description | Example |
|---|---|---|
| aws/aws-cli | CircleCI orb for AWS CLI operations | orbs: |
| aws/ecs | CircleCI orb for ECS deployments | orbs: |
| aws/lambda | CircleCI orb for Lambda deployments | orbs: |
Example: Deploying to AWS S3
jobs:
deploy-to-s3:
docker:
- image: circleci/python:3.9
steps:
- checkout
- run:
name: Install AWS CLI
command: pip install awscli
- run:
name: Deploy to S3
command: |
aws s3 sync ./dist s3://my-bucket/dist --delete
Example: Deploying to AWS ECS
jobs:
deploy-to-ecs:
docker:
- image: circleci/python:3.9
steps:
- checkout
- run:
name: Install AWS CLI
command: pip install awscli
- run:
name: Deploy to ECS
command: |
aws ecs update-service --cluster my-cluster --service my-service --force-new-deployment
Note: Ensure that the AWS credentials are configured in CircleCI context or environment variables.
Warning: Make sure to set up proper IAM roles and permissions for CircleCI to access AWS resources securely.
2. Deploying to Google Cloud
| Option | Values | Notes |
|---|---|---|
| deploy | gcloud |
Deploy using Google Cloud CLI |
| project_id | my-gcp-project |
Google Cloud Project ID |
| service_account_key | path/to/service-account.json |
Path to service account key file |
Example: Deploying to Google App Engine
version: 2
deploy:
gcloud:
project_id: my-gcp-project
service_account_key: /path/to/service-account.json
app_yaml: app.yaml
Example: Deploying to Google Kubernetes Engine
version: 2
deploy:
gcloud:
project_id: my-gcp-project
service_account_key: /path/to/service-account.json
cluster_name: my-cluster
namespace: default
Note: Ensure the service account has appropriate permissions for deployment, such as
roles/clouddeploy.deployer.
Warning: The
service_account_key should be stored securely in CircleCI context variables.
| Deployment Type | Configuration | Notes |
|---|---|---|
| App Engine | app_yaml: app.yaml |
Standard or flexible environment |
| Kubernetes Engine | cluster_name: my-cluster |
Requires GKE cluster setup |
| Cloud Run | image: gcr.io/my-project/my-app |
Container image to deploy |
3. Deploying to Azure
| Property | Description | Example |
|---|---|---|
| azure_web_app | Deploy to Azure Web App using the Azure CLI | azure_web_app: "my-web-app" |
| azure_resource_group | Resource group name for the Azure Web App | azure_resource_group: "my-resource-group" |
| azure_subscription_id | Azure subscription ID | azure_subscription_id: "12345678-1234-1234-1234-123456789012" |
| azure_tenant_id | Azure tenant ID | azure_tenant_id: "12345678-1234-1234-1234-123456789012" |
| azure_client_id | Azure client ID for authentication | azure_client_id: "12345678-1234-1234-1234-123456789012" |
| azure_client_secret | Azure client secret for authentication | azure_client_secret: "secret-key" |
Example: Deploying to Azure Web App
jobs:
deploy-to-azure:
docker:
- image: circleci/python:3.9
steps:
- checkout
- run:
name: Install Azure CLI
command: |
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
- run:
name: Deploy to Azure Web App
command: |
az webapp deployment source config-zip \
--resource-group $AZURE_RESOURCE_GROUP \
--name $AZURE_WEB_APP \
--src-path ./app.zip
Note: Ensure that the
AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, and other environment variables are set in CircleCI project settings.
4. Deploying to Kubernetes
| Property | Description | Example |
|---|---|---|
| deployment.name | Name of the Kubernetes deployment | my-app-deployment |
| deployment.namespace | Kubernetes namespace to deploy to | production |
| deployment.image | Docker image to deploy | my-app:latest |
| deployment.replicas | Number of pod replicas to run | 3 |
| deployment.service.port | Port to expose the service on | 80 |
Example: CircleCI Kubernetes Orb Configuration
version: 2.1
orbs:
kubernetes: circleci/kubernetes@1.2.0
jobs:
deploy-to-k8s:
docker:
- image: circleci/python:3.9
steps:
- checkout
- kubernetes/deploy:
deployment-name: my-app-deployment
namespace: production
image: my-app:latest
replicas: 3
service-port: 80
Note: Ensure that the Kubernetes context is properly configured in CircleCI to allow deployment.
Warning: Make sure the image tag used in deployment matches the built image from your build job.
Deployment Steps Overview
- Build Docker image in CircleCI pipeline
- Push image to registry (e.g., Docker Hub, ECR)
- Deploy to Kubernetes using CircleCI orbs or custom scripts
- Verify deployment status and service accessibility
5. Deploying to Heroku
| Option | Values | Notes |
|---|---|---|
| heroku.app-name | my-app |
Name of the Heroku app to deploy to |
| heroku.api-key | HEROKU_API_KEY |
Heroku API key (set as environment variable) |
| heroku.branch | main |
Branch to deploy from (default: main) |
Example: CircleCI Configuration for Heroku Deployment
version: 2.1
jobs:
deploy:
docker:
- image: circleci/python:3.9
steps:
- checkout
- run:
name: Deploy to Heroku
command: |
git push https://git.heroku.com/my-app.git HEAD:main
- run:
name: Verify Deployment
command: |
curl -I https://my-app.herokuapp.com
Note: Ensure the Heroku API key is stored in CircleCI's environment variables with the name
HEROKU_API_KEY.
Warning: The Heroku CLI must be installed in the CircleCI environment to support Git operations.
Deployment Workflow Summary
- Checkout code from the repository
- Push to Heroku's Git endpoint
- Verify deployment via HTTP request
6. Using Deployment Gates
Deployment gates are checkpoints in your CI/CD pipeline that ensure specific conditions are met before a deployment can proceed. They help enforce quality standards and reduce the risk of deploying unstable code.
| Gate Type | Description | Configuration Example | Notes |
|---|---|---|---|
| Manual Approval | Requires a human to approve the deployment before it proceeds. | |
Useful for production deployments where human oversight is critical. |
| Status Check | Ensures that specific checks (e.g., tests, linting) pass before deployment. | |
Can be configured to wait for all or any checks to pass. |
| Environment Approval | Depends on environment-specific approval rules (e.g., staging vs. production). | |
Allows different approval workflows per environment. |
Example: Manual Approval Gate
deployment:
production:
gates:
- type: manual
required: true
approval_message: "Approve deployment to production"
Example: Status Check Gate
deployment:
staging:
gates:
- type: status_check
required: true
checks:
- test
- lint
Note: Deployment gates can be combined to create complex approval workflows. For example, a deployment might require both a manual approval and all status checks to pass.
7. Implementing Blue-Green Deployments
| Aspect | Description | Implementation |
|---|---|---|
| Environment Setup | Two identical environments (blue and green) running different versions of the application. | Use CircleCI orbs or custom scripts to provision and manage two environments with distinct configurations. |
| Traffic Switching | Redirect traffic from the blue environment to the green one once deployment is complete. | Utilize load balancers or service mesh tools (e.g., AWS ALB, Kubernetes Ingress) to switch traffic. |
| Rollback Capability | Ability to quickly revert to the previous version if issues arise. | Implement a rollback mechanism using deployment tools or CI/CD pipelines to switch traffic back. |
| Health Checks | Ensure that the new version is stable before switching traffic. | Use CircleCI's built-in health check steps or custom scripts to validate the new deployment. |
Example: Blue-Green Deployment Configuration in CircleCI
version: 2.1
jobs:
deploy-blue-green:
docker:
- image: circleci/python:3.9
steps:
- checkout
- run:
name: Deploy to Blue Environment
command: |
echo "Deploying to blue environment..."
# Deployment logic for blue environment
- run:
name: Validate Blue Environment
command: |
echo "Validating blue environment..."
# Health check logic
- run:
name: Switch Traffic to Green
command: |
echo "Switching traffic to green environment..."
# Logic to switch traffic (e.g., via AWS ALB or Kubernetes)
- run:
name: Deploy to Green Environment
command: |
echo "Deploying to green environment..."
# Deployment logic for green environment
Note: Blue-green deployments require infrastructure support for managing two identical environments and traffic routing.
Benefits of Blue-Green Deployments
- Zero Downtime: Traffic is switched only after validation.
- Quick Rollback: If issues occur, traffic can be quickly switched back.
- Risk Reduction: Ensures that only validated versions are live.
8. Implementing Canary Deployments
| Property | Description | Example |
|---|---|---|
| deployment.strategy | Defines the deployment strategy to use. For canary, this is set to canary. |
deployment.strategy: canary |
| deployment.canary.percentage | Percentage of traffic to route to the new version during canary deployment. | deployment.canary.percentage: 10 |
| deployment.canary.steps | Number of steps to gradually increase traffic to the new version. | deployment.canary.steps: 3 |
Example: Canary Deployment Configuration
deployment:
strategy: canary
canary:
percentage: 10
steps: 3
Note: Canary deployments help reduce risk by gradually shifting traffic to a new version, allowing for quick rollback if issues arise.
9. Implementing Rolling Updates
| Property | Description | Values | Notes |
|---|---|---|---|
| deployment.strategy | Deployment strategy to use | rolling |
Default for most deployment types |
| deployment.rolling.batch_size | Number of instances to update at once | 1 or 2 |
Higher values increase speed but reduce safety |
| deployment.rolling.timeout | Timeout for deployment steps | 300 seconds |
Adjust based on app complexity |
Note: Rolling updates ensure zero downtime by updating instances one batch at a time.
Warning: Ensure your application can handle concurrent versions during the update.
| Step | Action | Description |
|---|---|---|
| 1 | Start new instances | Spin up new containers with updated code |
| 2 | Health check | Verify new instances are healthy before proceeding |
| 3 | Update batch | Replace one batch of old instances with new ones |
| 4 | Repeat | Continue until all instances are updated |
10. Rolling Back Deployments
| Option | Values | Notes |
|---|---|---|
| deployment.rollback | true |
Enable rollback for deployment jobs |
| deployment.rollback.on_failure | false |
Automatically rollback on deployment failure |
| deployment.rollback.strategy | auto |
Rollback strategy: auto, manual |
Example: Rollback Configuration
deployment:
rollback: true
rollback_on_failure: true
rollback_strategy: auto
Note: Rollback is supported for
heroku, aws, and kubernetes deployments.
Warning: Ensure rollback is enabled only in environments where it's safe to revert changes.