The application is based on the Flask framework, handling a single endpoint (/) that displays a message on the home page.
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
data = {"message": "Welcome to my Flask app!"}
return render_template('index.html', data=data)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)- **Endpoint **``:
- Renders the
index.htmltemplate. - Passes data (
data) containing a welcome message.
- Renders the
- The application runs an HTTP server on port
5000, listening on all interfaces (host='0.0.0.0'). - Debug mode is enabled (
debug=True) for easier error diagnostics.
Automate the process of building, testing, performing security scans, and publishing the Docker image to GitHub Container Registry (GHCR).
name: CI workflow
on:
push:
branches: ["main"]
pull_request:
branches: ["main"]- The workflow triggers automatically on any
pushto themainbranch. - It also triggers for pull requests targeting the
mainbranch.
jobs:
build:
runs-on: ubuntu-latest- The workflow runs on an
Ubuntu-latestmachine.
- name: Checkout code
uses: actions/checkout@v3Fetches the latest version of the source code from the repository.
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.10"Installs Python version 3.10.
- name: Install dependencies
run: |
python -m pip install --upgrade pipUpgrades the pip package manager.
- name: Run basic test
run: echo "Test"Placeholder for running application tests.
- name: Build Docker image
run: docker build -t flask-app .Builds the Docker image using the Dockerfile.
- name: Run Trivy scan
run: |
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock aquasec/trivy:latest image flask-appScans the Docker image for vulnerabilities using Trivy.
- name: Push to GitHub Container Registry
if: github.event_name == 'push'
run: |
docker login --username dstudnicki --password ${{ secrets.GH_PAT }} ghcr.io
docker build . --tag ghcr.io/dstudnicki/flask-app:latest
docker push ghcr.io/dstudnicki/flask-app:latest- Logs in to GHCR using a Personal Access Token (GH_PAT).
- Builds the Docker image and tags it as
ghcr.io/dstudnicki/flask-app:latest. - Pushes the image to GitHub Container Registry.
docker build -t flask-app .docker run -p 5000:5000 flask-appThe application will be available at: http://localhost:5000.
docker pull ghcr.io/dstudnicki/flask-app:latestdocker run -p 5000:5000 ghcr.io/dstudnicki/flask-app:latestThe application will be available at: http://localhost:5000.