AI Hints
Home›CI/CD with GitHub Actions Tutorial›Smoke Tests, Rollbacks, and Flags
CI/CD with GitHub Actions · Lesson 96 of 138

Feature Flags

Ship it switched off.

⏱ 2 min read▶ Video at 1:50:48🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

A feature flag is a switch in your code. The new feature ships to production, but switched off. You turn it on later, for some users or for everyone, without a new deploy. Deploying and releasing become two separate steps.

Feature Flags
From the video: Feature Flags
  • A switch in the code
  • Ship it switched off
  • Turn on later

A simple flag

Here the flag is an environment variable. Cat shows the checkout function, with two versions. Without the flag, users get the old checkout. With new checkout set to on, they get the new one. Same code, same deploy, different behaviour.

Terminal
cat shop.py
import os

NEW_CHECKOUT = os.environ.get("NEW_CHECKOUT") == "on"

def checkout(prices):
    if NEW_CHECKOUT:
        return f"New checkout: {sum(prices)} in one click"
    return f"Checkout: total {sum(prices)}"

print(checkout([10, 20]))
python3 shop.py
Checkout: total 30
NEW_CHECKOUT=on python3 shop.py
New checkout: 30 in one click

Using flags well

Flags let you merge unfinished work safely, test with a few users, and switch a broken feature off in seconds. Real flag services let you change them live, per user. But clean them up. Once a feature is fully on, delete the flag and the old code, or they pile up.

keyvalue
Merge earlyfeature stays off
Graduala few users first
Kill switchoff in seconds
Clean updelete old flags

Summary

Feature flags separate deploying from releasing. Ship code switched off, and turn it on when you're ready. Next, changing the database safely.

Key takeaways
  • Deploy ≠ release
  • Kill switch
  • Remove old flags
Next up: Database Migrations — changing the schema