How to Do a Blue-Green Deploy With CircleCI
Deploy to the idle environment (green), smoke test it while blue still serves users, then repoint the load balancer so the cutover is instant.
Detect the live color, deploy to the idle one, run a health check against it, and only then switch the load balancer target group.
Steps
- Determine which color is currently live.
- Deploy the new version to the idle color.
- Smoke test the idle color before any cutover.
- Switch the load balancer to the idle color.
Config
.circleci/config.yml
version: 2.1
jobs:
blue-green:
docker:
- image: cimg/aws:2024.03
steps:
- checkout
- run:
name: Deploy to idle color
command: |
LIVE=$(./scripts/live-color.sh)
IDLE=$([ "$LIVE" = blue ] && echo green || echo blue)
./scripts/deploy-color.sh "$IDLE" "$CIRCLE_SHA1"
./scripts/smoke-test.sh "$IDLE" || exit 1
./scripts/switch-lb.sh "$IDLE"
workflows:
release:
jobs:
- blue-green:
context: aws-prod
filters:
branches:
only: mainGotchas
- A failed smoke test must
exit 1before the switch so the job fails with blue still live. - Keep the old color running until traffic drains so you can flip back instantly.