📖 Scenario: You have created a simple Flask web application. Now, you want to automate testing and deployment using a CI/CD pipeline. This project will guide you through setting up the basic files and configuration to enable continuous integration and continuous deployment for your Flask app.
🎯 Goal: Build a basic CI/CD pipeline configuration for a Flask app that installs dependencies, runs tests, and deploys the app automatically.
📋 What You'll Learn
Create a simple Flask app file named app.py with a basic route.
Add a requirements file named requirements.txt listing Flask.
Create a GitHub Actions workflow file named ci-cd.yml to automate install, test, and deploy steps.
Print the deployment success message at the end.
💡 Why This Matters
🌍 Real World
Automating testing and deployment saves time and reduces errors when updating web applications.
💼 Career
CI/CD pipelines are essential skills for DevOps engineers and developers to deliver software reliably and quickly.
Progress0 / 4 steps
1
Create a simple Flask app
Create a file named app.py with a Flask app that has one route / returning the text "Hello, CI/CD!". Use the variable name app for the Flask instance.
Flask
Hint
Import Flask, create app = Flask(__name__), define a route / with a function returning the string.
2
Add requirements file
Create a file named requirements.txt containing the exact line Flask==2.3.2 to specify the Flask version dependency.
Flask
Hint
Write Flask==2.3.2 exactly in requirements.txt to specify the Flask version.
3
Create GitHub Actions workflow
Create a file named .github/workflows/ci-cd.yml with a GitHub Actions workflow that triggers on push to main. The workflow should have a job named build that runs on ubuntu-latest. It should have steps to: checkout code, set up Python 3.12, install dependencies from requirements.txt, run tests by executing python -m unittest discover, and print Deployment successful! at the end.
Flask
Hint
Use GitHub Actions syntax to define a workflow triggered on push to main. Include steps for checkout, Python setup, install, test, and a final echo message.
4
Print deployment success message
Add a print statement in app.py after the Flask app runs to print "Deployment successful!" to the console.
Flask
Hint
Use print("Deployment successful!") after app.run() in app.py.
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
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 A
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?
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
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 C
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
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 D
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 unknownstrongwhile earning +1 Grip toward your certificate.