A CI/CD pipeline helps you automatically test and deliver your Flask app. It saves time and reduces mistakes by making sure your app works well before it goes live.
CI/CD pipeline for Flask
name: Flask CI/CD Pipeline
on: [push]
jobs:
build:
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: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests
run: |
pytest
- name: Deploy
if: success()
run: |
echo "Deploying Flask app..."
# Add your deploy commands hereThis example uses GitHub Actions syntax for a CI/CD pipeline.
Replace the deploy step with commands specific to your hosting service.
on: [push]
jobs:
build:
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: pyteston:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Deploy to Heroku
run: |
heroku login
git push heroku mainThis pipeline runs on every push. It checks out the code, sets up Python 3.12, installs dependencies, runs tests with pytest, and if tests pass, it runs a deploy step (here just a placeholder echo command).
name: Flask CI/CD Pipeline
on: [push]
jobs:
build:
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: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests
run: |
pytest
- name: Deploy
if: success()
run: |
echo "Deploying Flask app..."Make sure your Flask app has tests written with pytest or another test tool.
Customize the deploy step to match your hosting environment (Heroku, AWS, etc.).
Use secrets or environment variables to keep sensitive info safe during deployment.
CI/CD pipelines automate testing and deployment for Flask apps.
They run on code changes to catch errors early and speed up delivery.
Use GitHub Actions or similar tools to create your pipeline easily.
Practice
Solution
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.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.Final Answer:
To automate testing and deployment whenever code changes -> Option AQuick Check:
CI/CD automates testing and deployment = C [OK]
- Thinking CI/CD writes code
- Confusing manual deployment with CI/CD
- Assuming CI/CD creates UI automatically
Solution
Step 1: Identify GitHub Actions workflow file location
GitHub Actions requires workflow files inside the repository at.github/workflows/directory.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.Final Answer:
.github/workflows/flask-ci.yml -> Option AQuick Check:
Workflow files go in .github/workflows/ = D [OK]
- Placing workflow files outside .github/workflows/
- Using wrong file extensions
- Misnaming the directory
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: pytestWhat will happen when this workflow runs?
Solution
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.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.Final Answer:
It checks out code, sets Python 3.12, installs dependencies, and runs tests -> Option BQuick Check:
Checkout + setup + install + test = B [OK]
- Assuming tests run without installing dependencies
- Thinking Python version is missing
- Ignoring the checkout step
- name: Install dependencies run: pip install requirements.txt
Why does this step fail in your Flask CI/CD pipeline?
Solution
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.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.Final Answer:
Because the command should be 'pip install -r requirements.txt' -> Option CQuick Check:
Use '-r' flag to install from file = A [OK]
- Omitting the '-r' flag
- Assuming pip install can't run in CI
- Blaming missing Python version
Solution
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.Step 2: Identify correct deployment action
The 'akhileshns/heroku-deploy@v3.12.12' action is a popular choice for Heroku deployment in GitHub workflows.Final Answer:
Add a step using 'akhileshns/heroku-deploy@v3.12.12' action with Heroku API key and app name -> Option DQuick Check:
Use Heroku deploy action with API key = A [OK]
- Trying interactive login in CI
- Copying files locally instead of deploying
- Installing unrelated packages
Ready to prove it?
Pass the test and this pattern moves unknown
