Bird
Raised Fist0
Flaskframework~10 mins

CI/CD pipeline for Flask - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - CI/CD pipeline for Flask
Code Commit to Repo
↓
Trigger CI Pipeline
↓
Run Tests
↓
Build Docker Image
↓
Push Image to Registry
↓
Trigger CD Pipeline
↓
Deploy to Server
↓
Run Smoke Tests
↓
Release Live
This flow shows how code changes trigger automated testing, building, and deployment steps to safely update a Flask app.
Execution Sample
Flask
git push origin main
# CI pipeline runs tests
# Build Docker image
# Push image to registry
# Deploy to server
# Run smoke tests
This sequence shows pushing code triggers tests, build, deploy, and verification steps automatically.
Execution Table
StepActionResultNext Step
1Code pushed to repoTrigger CI pipelineRun tests
2Run testsTests passBuild Docker image
3Build Docker imageImage built successfullyPush image to registry
4Push image to registryImage pushedTrigger CD pipeline
5Trigger CD pipelineStart deploymentDeploy to server
6Deploy to serverApp deployedRun smoke tests
7Run smoke testsSmoke tests passRelease live
8Release liveApp live with new versionEnd
9If tests fail at step 2Pipeline stopsNotify developer
10If smoke tests fail at step 7Rollback deploymentNotify ops
💡 Pipeline ends after successful release or stops on test failures.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
Code Versionv1.0v1.1 (pushed)v1.1 (image pushed)v1.1 (deployed)v1.1 (live)
Test StatusN/APassN/AN/AN/A
Deployment StatusNot deployedNot deployedNot deployedDeployedLive
Smoke Test StatusN/AN/AN/APassPass
Key Moments - 3 Insights
Why does the pipeline stop if tests fail at step 2?
Because failing tests mean the code is not ready, so the pipeline stops to prevent bad code from deploying, as shown in execution_table row 9.
What happens if smoke tests fail after deployment?
The pipeline triggers a rollback to the previous stable version to avoid downtime, as shown in execution_table row 10.
Why do we build a Docker image after tests pass?
Building a Docker image packages the app consistently for deployment, ensuring the tested code is what runs live, as shown in execution_table row 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result after step 4?
AImage pushed to registry
BTests fail
CApp deployed
DRollback triggered
💡 Hint
Check the 'Result' column for step 4 in the execution_table.
At which step does the pipeline stop if tests fail?
AStep 7
BStep 3
CStep 2
DStep 6
💡 Hint
Look for the failure condition related to tests in the execution_table rows.
If smoke tests fail, what is the immediate next action?
ARelease live
BRollback deployment
CNotify developer
DBuild Docker image
💡 Hint
Refer to the execution_table row describing smoke test failure.
Concept Snapshot
CI/CD pipeline for Flask:
- Code push triggers CI pipeline
- Run automated tests; stop if fail
- Build Docker image if tests pass
- Push image to registry
- Deploy image to server
- Run smoke tests; rollback if fail
- Release live if all pass
Full Transcript
A CI/CD pipeline for a Flask app starts when code is pushed to the repository. This triggers the continuous integration pipeline, which runs automated tests. If tests pass, the pipeline builds a Docker image of the app and pushes it to a container registry. Then, the continuous deployment pipeline deploys the image to the server. After deployment, smoke tests run to verify the app is working. If smoke tests pass, the new version is released live. If any tests fail, the pipeline stops or rolls back to keep the app stable.

Practice

(1/5)
1. What is the main purpose of a CI/CD pipeline for a Flask application?
easy
A. To automate testing and deployment whenever code changes
B. To manually deploy code after testing
C. To write Flask application code faster
D. To create user interfaces automatically

Solution

  1. Step 1: Understand CI/CD pipeline role

    A CI/CD pipeline automates the process of testing and deploying code changes to catch errors early and speed up delivery.
  2. Step 2: Match purpose with options

    To automate testing and deployment whenever code changes correctly states automation of testing and deployment on code changes, which is the main goal of CI/CD.
  3. Final Answer:

    To automate testing and deployment whenever code changes -> Option A
  4. Quick Check:

    CI/CD automates testing and deployment = C [OK]
Hint: CI/CD means automatic test and deploy on code change [OK]
Common Mistakes:
  • Thinking CI/CD writes code
  • Confusing manual deployment with CI/CD
  • Assuming CI/CD creates UI automatically
2. Which of the following is a correct minimal GitHub Actions workflow file name and location for a Flask CI/CD pipeline?
easy
A. .github/workflows/flask-ci.yml
B. github/actions/flask-ci.yaml
C. .github/actions/flask-ci.yml
D. workflows/.github/flask-ci.yml

Solution

  1. Step 1: Identify GitHub Actions workflow file location

    GitHub Actions requires workflow files inside the repository at .github/workflows/ directory.
  2. Step 2: Check file name and extension

    The file should have a .yml or .yaml extension. .github/workflows/flask-ci.yml uses correct directory and file name.
  3. Final Answer:

    .github/workflows/flask-ci.yml -> Option A
  4. Quick Check:

    Workflow files go in .github/workflows/ = D [OK]
Hint: GitHub Actions workflows live in .github/workflows/ [OK]
Common Mistakes:
  • Placing workflow files outside .github/workflows/
  • Using wrong file extensions
  • Misnaming the directory
3. Given this GitHub Actions snippet for a Flask app:
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.12'
      - name: Install dependencies
        run: pip install -r requirements.txt
      - name: Run tests
        run: pytest

What will happen when this workflow runs?
medium
A. It only installs dependencies without running tests
B. It checks out code, sets Python 3.12, installs dependencies, and runs tests
C. It runs tests without installing dependencies
D. It fails because Python version is missing

Solution

  1. Step 1: Analyze each step in the workflow

    The workflow checks out the code, sets up Python 3.12, installs dependencies from requirements.txt, then runs pytest tests.
  2. Step 2: Confirm all steps are present and correct

    All steps are correctly ordered and complete, so the workflow will perform all these actions successfully.
  3. Final Answer:

    It checks out code, sets Python 3.12, installs dependencies, and runs tests -> Option B
  4. Quick Check:

    Checkout + setup + install + test = B [OK]
Hint: Check steps order: checkout, setup, install, test [OK]
Common Mistakes:
  • Assuming tests run without installing dependencies
  • Thinking Python version is missing
  • Ignoring the checkout step
4. You wrote this GitHub Actions step to install dependencies:
- name: Install dependencies
  run: pip install requirements.txt

Why does this step fail in your Flask CI/CD pipeline?
medium
A. Because pip install cannot run in GitHub Actions
B. Because requirements.txt file is missing
C. Because the command should be 'pip install -r requirements.txt'
D. Because Python version is not specified

Solution

  1. Step 1: Identify the pip install syntax error

    The command 'pip install requirements.txt' tries to install a package named 'requirements.txt' instead of reading the file.
  2. Step 2: Correct the command syntax

    The correct command to install from a requirements file is 'pip install -r requirements.txt'. This flag tells pip to read the file.
  3. Final Answer:

    Because the command should be 'pip install -r requirements.txt' -> Option C
  4. Quick Check:

    Use '-r' flag to install from file = A [OK]
Hint: Use 'pip install -r requirements.txt' to install dependencies [OK]
Common Mistakes:
  • Omitting the '-r' flag
  • Assuming pip install can't run in CI
  • Blaming missing Python version
5. You want to deploy your Flask app automatically after tests pass using GitHub Actions. Which step should you add to your workflow to deploy to Heroku?
hard
A. Add a step that runs 'pip install flask-heroku'
B. Add a step that runs 'heroku login' interactively
C. Add a step that copies files to a local folder
D. Add a step using 'akhileshns/heroku-deploy@v3.12.12' action with Heroku API key and app name

Solution

  1. Step 1: Understand automated deployment in GitHub Actions

    To deploy automatically, use a GitHub Action designed for Heroku deployment that accepts API keys and app name securely.
  2. Step 2: Identify correct deployment action

    The 'akhileshns/heroku-deploy@v3.12.12' action is a popular choice for Heroku deployment in GitHub workflows.
  3. Final Answer:

    Add a step using 'akhileshns/heroku-deploy@v3.12.12' action with Heroku API key and app name -> Option D
  4. Quick Check:

    Use Heroku deploy action with API key = A [OK]
Hint: Use Heroku deploy GitHub Action with API key for auto deploy [OK]
Common Mistakes:
  • Trying interactive login in CI
  • Copying files locally instead of deploying
  • Installing unrelated packages
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard