Initial commit: demo Flask app + tests + Dockerfile + Jenkinsfile
This commit is contained in:
commit
ed7d8e4bf1
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
__pycache__/
|
||||
*.pyc
|
||||
.pytest_cache/
|
||||
venv/
|
||||
18
Dockerfile
Normal file
18
Dockerfile
Normal file
@ -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"]
|
||||
87
Jenkinsfile
vendored
Normal file
87
Jenkinsfile
vendored
Normal file
@ -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" }
|
||||
}
|
||||
}
|
||||
20
app/main.py
Normal file
20
app/main.py
Normal file
@ -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)
|
||||
2
requirements-dev.txt
Normal file
2
requirements-dev.txt
Normal file
@ -0,0 +1,2 @@
|
||||
-r requirements.txt
|
||||
pytest==8.3.3
|
||||
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@ -0,0 +1,2 @@
|
||||
flask==3.0.3
|
||||
gunicorn==22.0.0
|
||||
18
tests/test_main.py
Normal file
18
tests/test_main.py
Normal file
@ -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
|
||||
Loading…
Reference in New Issue
Block a user