-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
145 lines (130 loc) · 5.2 KB
/
Jenkinsfile
File metadata and controls
145 lines (130 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
pipeline {
agent any
environment {
DOCKER_IMAGE = 'todo-app:latest'
}
stages {
// Setup Stage: Prepares environment and installs dependencies
stage('Setup') {
steps {
echo 'Setting up environment and installing dependencies...'
// Ensure Python and pip are available
sh 'python3 --version'
// Remove existing virtual environment and create a new one
sh 'rm -rf venv'
sh 'python3 -m venv venv'
// Activate virtual environment and upgrade pip
sh '. venv/bin/activate && pip install --upgrade pip'
// Install all required dependencies including pytest
sh '. venv/bin/activate && pip install -r requirements.txt'
// Ensure pytest is installed
echo 'Installing pytest...'
sh '. venv/bin/activate && pip install pytest'
// Validate pytest installation
echo 'Validating pytest installation...'
sh '. venv/bin/activate && pytest --version'
}
}
// Build Stage: Creates a zip archive of the project as a build artifact
stage('Build') {
steps {
echo 'Creating build artifact...'
sh 'zip -r todo-app.zip *'
archiveArtifacts artifacts: 'todo-app.zip'
}
}
// Linting Stage: Runs flake8 to check code formatting and syntax issues
stage('Linting') {
steps {
echo 'Running flake8 for linting...'
sh '. venv/bin/activate && pip install flake8'
sh '. venv/bin/activate && flake8 --max-line-length=120 > lint-report.txt || true'
archiveArtifacts artifacts: 'lint-report.txt'
}
}
// Static Code Analysis: Uses pylint to identify code quality issues
stage('Static Code Analysis') {
steps {
echo 'Running pylint...'
sh '. venv/bin/activate && pip install pylint'
sh '. venv/bin/activate && pylint app.py > pylint-report.txt || true'
archiveArtifacts artifacts: 'pylint-report.txt'
}
}
// Security Scanning: Uses bandit to perform security checks on the code
stage('Security Scanning') {
steps {
echo 'Running bandit...'
sh '. venv/bin/activate && pip install bandit'
sh '. venv/bin/activate && bandit -r . > security-report.txt || true'
archiveArtifacts artifacts: 'security-report.txt'
}
}
// Test Stage: Runs unit tests using pytest
stage('Test') {
steps {
echo 'Running tests...'
// Activate virtual environment and run pytest
sh '. venv/bin/activate && python -m pytest tests/'
}
}
// Deploy Stage using Gunicorn
stage('Deploy') {
steps {
echo 'Deploying on port 5000...'
// Stop any app running on port 5000
sh 'fuser -k 5000/tcp || true'
// Deploy the app using Gunicorn
sh '. venv/bin/activate && gunicorn --bind 0.0.0.0:5000 app:app --daemon --log-file gunicorn.log --access-logfile gunicorn-access.log'
// Check if the app is running
sh 'curl http://localhost:5000 || echo "App did not start successfully"'
}
}
// Performance Testing Stage
stage('Performance Testing') {
steps {
echo 'Running performance tests...'
sh 'ab -n 100 -c 10 http://localhost:5000/'
}
}
// Backup Stage
stage('Backup') {
steps {
echo 'Backing up logs...'
sh 'mkdir -p backups && cp gunicorn.log backups/gunicorn-$(date +%F).log'
archiveArtifacts artifacts: 'backups/*.log'
}
}
// Release Stage
stage('Release') {
steps {
echo 'Releasing to production...'
// Stop any app running on port 5000 and start a new one
sh 'fuser -k 5000/tcp || true'
// Deploy the app in production mode
sh '. venv/bin/activate && gunicorn --bind 0.0.0.0:5000 app:app --daemon --log-file gunicorn-production.log --access-logfile gunicorn-production-access.log'
// Check if the app is running in production
sh 'curl http://localhost:5000 || echo "App did not start successfully"'
}
}
// Monitoring and Alerting Stage
stage('Monitoring and Alerting') {
steps {
echo 'Monitoring application...'
// Ensure the app is still running
sh 'curl http://localhost:5000 || echo "App is down!"'
}
}
}
post {
always {
echo 'Pipeline completed.'
}
success {
echo 'Pipeline completed successfully!'
}
failure {
echo 'Pipeline failed!'
}
}
}