Skip to content
Latchkey

How to Do a Blue-Green Deploy With Bitbucket Pipelines

Deploy to the idle color, health-check it, then flip the load balancer to the new color so traffic cuts over with zero downtime.

Blue-green keeps two identical targets and only one live. The pipeline releases to the idle one, verifies it, then repoints the load balancer (or DNS / target group) so the switch is instant and the old color is your instant rollback.

Steps

  • Determine the idle color (the one not currently live).
  • Deploy the new build to the idle color.
  • Run a health check against the idle color.
  • Switch the load balancer target to the idle color, then mark it live.

Pipeline

bitbucket-pipelines.yml
image: amazon/aws-cli:2
pipelines:
  branches:
    main:
      - step:
          name: Blue-green deploy
          deployment: production
          script:
            - LIVE=$(aws ssm get-parameter --name /app/live-color --query Parameter.Value --output text)
            - IDLE=$([ "$LIVE" = blue ] && echo green || echo blue)
            - ./deploy.sh --target $IDLE --tag $BITBUCKET_COMMIT
            - curl -fsS https://$IDLE.internal.example.com/health
            - aws elbv2 modify-listener --listener-arn $LISTENER_ARN --default-actions Type=forward,TargetGroupArn=$([ "$IDLE" = blue ] && echo $TG_BLUE || echo $TG_GREEN)
            - aws ssm put-parameter --name /app/live-color --value $IDLE --overwrite

Gotchas

  • Fail the step if the health check fails so traffic never switches to a broken color.
  • Keep the old color running after cutover so rollback is just another listener flip.
  • Drain connections on the old target group before tearing it down.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →