From ed7d8e4bf121bd0a1326689f81c4912dcc161e14 Mon Sep 17 00:00:00 2001 From: devops Date: Wed, 16 Sep 2026 11:22:57 +0000 Subject: [PATCH] Initial commit: demo Flask app + tests + Dockerfile + Jenkinsfile --- .gitignore | 4 ++ Dockerfile | 18 +++++++++ Jenkinsfile | 87 ++++++++++++++++++++++++++++++++++++++++++++ app/main.py | 20 ++++++++++ requirements-dev.txt | 2 + requirements.txt | 2 + tests/test_main.py | 18 +++++++++ 7 files changed, 151 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 Jenkinsfile create mode 100644 app/main.py create mode 100644 requirements-dev.txt create mode 100644 requirements.txt create mode 100644 tests/test_main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0b1b10d --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +__pycache__/ +*.pyc +.pytest_cache/ +venv/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d546e66 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +FROM python:3.12-slim + +WORKDIR /srv +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +COPY app/ ./app/ + +ARG APP_VERSION=dev +ENV APP_VERSION=${APP_VERSION} + +RUN useradd -r -u 1001 appuser +USER appuser + +EXPOSE 8000 +HEALTHCHECK --interval=30s --timeout=3s CMD python -c "import urllib.request;urllib.request.urlopen('http://127.0.0.1:8000/health')" + +CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "2", "app.main:app"] diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..4a61ab3 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,87 @@ +pipeline { + agent any + + environment { + REGISTRY = '51.195.4.170:5000' + IMAGE_NAME = 'swim/demo-app' + IMAGE_TAG = "${env.BUILD_NUMBER}" + } + + options { + timestamps() + buildDiscarder(logRotator(numToKeepStr: '20')) + timeout(time: 20, unit: 'MINUTES') + } + + stages { + stage('Checkout') { + steps { + checkout scm + sh 'echo "commit: $(git rev-parse --short HEAD)"' + } + } + + stage('Test') { + steps { + sh ''' + docker run --rm -v "$PWD":/src -w /src python:3.12-slim sh -c " + pip install --quiet -r requirements-dev.txt && + python -m pytest tests/ -v + " + ''' + } + } + + stage('Build image') { + steps { + sh ''' + docker build \ + --build-arg APP_VERSION=${IMAGE_TAG} \ + -t ${REGISTRY}/${IMAGE_NAME}:${IMAGE_TAG} \ + -t ${REGISTRY}/${IMAGE_NAME}:latest . + docker images ${REGISTRY}/${IMAGE_NAME} + ''' + } + } + + stage('Smoke test image') { + steps { + sh ''' + CID=$(docker run -d -P ${REGISTRY}/${IMAGE_NAME}:${IMAGE_TAG}) + sleep 5 + docker exec $CID python -c " +import urllib.request, json, sys +r = json.loads(urllib.request.urlopen('http://127.0.0.1:8000/version').read()) +print('version endpoint:', r) +assert r['version'] == '${IMAGE_TAG}', 'version galat!' +" + docker rm -f $CID + ''' + } + } + + stage('Push to registry') { + steps { + withCredentials([usernamePassword(credentialsId: 'registry-creds', + usernameVariable: 'REG_USER', + passwordVariable: 'REG_PASS')]) { + sh ''' + echo "$REG_PASS" | docker login ${REGISTRY} -u "$REG_USER" --password-stdin + docker push ${REGISTRY}/${IMAGE_NAME}:${IMAGE_TAG} + docker push ${REGISTRY}/${IMAGE_NAME}:latest + docker logout ${REGISTRY} + ''' + } + } + } + } + + post { + always { + sh 'docker image prune -f --filter "until=24h" || true' + cleanWs() + } + success { echo "BUILD ${env.BUILD_NUMBER} SAFAL -> ${env.REGISTRY}/${env.IMAGE_NAME}:${env.IMAGE_TAG}" } + failure { echo "BUILD ${env.BUILD_NUMBER} FAIL" } + } +} diff --git a/app/main.py b/app/main.py new file mode 100644 index 0000000..5d00f7a --- /dev/null +++ b/app/main.py @@ -0,0 +1,20 @@ +import os +from flask import Flask, jsonify + +app = Flask(__name__) +VERSION = os.getenv("APP_VERSION", "dev") + +@app.route("/health") +def health(): + return jsonify(status="ok"), 200 + +@app.route("/version") +def version(): + return jsonify(version=VERSION, app="swim-demo"), 200 + +@app.route("/") +def index(): + return jsonify(message="SWIM DevOps demo app", version=VERSION), 200 + +if __name__ == "__main__": + app.run(host="0.0.0.0", port=8000) diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..60e163f --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,2 @@ +-r requirements.txt +pytest==8.3.3 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..b733f8f --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +flask==3.0.3 +gunicorn==22.0.0 diff --git a/tests/test_main.py b/tests/test_main.py new file mode 100644 index 0000000..9808d82 --- /dev/null +++ b/tests/test_main.py @@ -0,0 +1,18 @@ +import sys, os +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) +from app.main import app + +def test_health(): + c = app.test_client() + r = c.get("/health") + assert r.status_code == 200 + assert r.get_json()["status"] == "ok" + +def test_version(): + c = app.test_client() + r = c.get("/version") + assert r.status_code == 200 + assert "version" in r.get_json() + +def test_index(): + assert app.test_client().get("/").status_code == 200