https://github.com/theleopard65/jenkins-demo-flask-app
https://github.com/theleopard65/jenkins-demo-flask-app
Last synced: 12 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/theleopard65/jenkins-demo-flask-app
- Owner: TheLeopard65
- License: mit
- Created: 2025-06-13T11:22:57.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-06-13T11:39:07.000Z (12 months ago)
- Last Synced: 2025-06-13T12:42:30.266Z (12 months ago)
- Language: Python
- Size: 7.81 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## ๐งพ Flask User Form App
A simple CRUD-based Flask web application with secure user management using SQLAlchemy, Flask-Migrate, and WTForms. Users can register, update, and delete profiles. The app is fully containerized with Docker and integrated with Jenkins for automated CI/CD pipeline execution.
---
### ๐ Project Structure
```
Flask-App/
โโโ app.py # Main Flask application
โโโ config.py # App configuration using environment variables
โโโ requirements.txt # Python dependencies
โโโ Dockerfile # Docker image definition
โโโ docker-entrypoint.sh # Container entrypoint script
โโโ start.sh # Local helper script for app setup
โโโ templates/ # HTML templates (index, update, error pages)
โโโ .env # Environment variables (excluded from VCS)
โโโ .gitignore # Ignored files
โโโ Jenkinsfile # Jenkins pipeline definition
```
---
## ๐ Features
* User registration with email & password (bcrypt hashed)
* CSRF-protected forms via Flask-WTF
* Server-side validation with WTForms
* SQLite-backed database with migrations
* Clean HTML templates with Bootstrap 5
* Dockerized with health checks
* Jenkins CI/CD pipeline for automation
---
## ๐งช Tech Stack
* Python 3.10
* Flask, SQLAlchemy, Flask-Migrate
* WTForms, Flask-Bcrypt
* Docker
* Jenkins (Multibranch Pipeline)
* SQLite
* Bootstrap (via CDN)
---
## ๐ฆ Setup & Run
### โถ๏ธ Run Locally (Manual)
```bash
# Create virtual environment
python3 -m venv virtual-venv
source virtual-venv/bin/activate
# Install dependencies
pip install -r requirements.txt
# Set environment variables
cp .env.example .env
# Initialize DB
flask db init
flask db migrate -m "Initial"
flask db upgrade
# Run the app
flask run
```
### ๐ณ Run with Docker
```bash
# Build the Docker image
docker build -t flask-app-image .
# Run the container
docker run -d -p 5000:5000 --name flask-app flask-app-image
```
---
## โ๏ธ Jenkins CI/CD Pipeline
This project includes a `Jenkinsfile` for automating build and deployment using Jenkins.
### ๐ Pipeline Stages
1. **Clone Repository**: Jenkins fetches your GitHub repo
2. **Build Docker Image**: Builds container from Dockerfile
3. **Run Container**: Starts Flask app in Docker
4. **Health Check**: Validates service is up
5. **Post Actions**: Cleans up Docker resources
### ๐งพ Jenkinsfile
Click to view
```groovy
pipeline {
agent any
environment {
DOCKER_IMAGE = 'flask-app-image'
CONTAINER_NAME = 'flask-app'
APP_PORT = '5000'
}
parameters {
booleanParam(name: 'EXECUTE_TESTS', defaultValue: true, description: 'Run post-deployment tests?')
}
stages {
stage('Clone Repository') {
steps {
echo '[#] Cloning Repository'
sh 'ls -la'
}
}
stage('Build Docker Image') {
steps {
echo '[#] Building Docker Image'
sh "docker build -t $DOCKER_IMAGE ."
}
}
stage('Run Container') {
steps {
echo '[#] Running Docker Container'
sh """
docker rm -f $CONTAINER_NAME || true
docker run -d --name $CONTAINER_NAME -p $APP_PORT:5000 $DOCKER_IMAGE
"""
sleep 10
}
}
stage('Run Tests') {
when {
expression { return params.EXECUTE_TESTS == true }
}
steps {
echo '[#] Running Health Check Test'
script {
def result = sh(script: "curl -s -o /dev/null -w \"%{http_code}\" http://localhost:$APP_PORT/", returnStdout: true).trim()
if (result != '200') {
error "Health check failed with status: ${result}"
} else {
echo "Health check passed with status: ${result}"
}
}
}
}
}
post {
always {
echo '[#] Cleaning up Docker resources...'
sh "docker stop $CONTAINER_NAME || true"
sh "docker rm $CONTAINER_NAME || true"
}
success {
echo '[+] Pipeline completed successfully.'
}
failure {
echo '[!] Pipeline failed.'
}
}
}
```
---
## โ
Requirements
* Python 3.10+
* Docker
* Jenkins (latest preferred)
* Git
---
## ๐ License
This project is licensed under the [MIT License](./LICENSE).