-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblood_bank_ml_Jenkins.py
132 lines (119 loc) · 3.82 KB
/
blood_bank_ml_Jenkins.py
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
pipeline {
agent any
environment {
APP_NAME = 'save-heart-ml-app'
APP_VERSION = sh(script: 'git describe --always --tags', returnStdout: true).trim()
DOCKER_REGISTRY = 'dockerhub'
DOCKER_IMAGE_NAME = "${DOCKER_REGISTRY}/save-heart-ml-app"
DOCKER_IMAGE_TAG = "${DOCKER_IMAGE_NAME}:${APP_VERSION}"
}
stages {
stage('Checkout') {
steps {
// Checkout the Git repository
git url: 'https://github.com/Dulmina98/save-hearts.git'
}
}
stage('Lint') {
steps {
// Run linters on the code
sh 'pip install -r requirements.txt'
sh 'flake8 .'
}
}
stage('Unit Test') {
steps {
// Run unit tests
sh 'pytest --cov=src tests/'
}
}
stage('Code Coverage') {
steps {
// Generate code coverage report
sh 'coverage xml'
sh 'coverage html'
}
post {
always {
// Archive the code coverage report
archiveArtifacts artifacts: 'htmlcov/**'
publishHTML(target: [allowMissing: false, alwaysLinkToLastBuild: true, keepAll: true, reportDir: 'htmlcov', reportFiles: 'index.html', reportName: 'Code Coverage Report'])
}
}
}
stage('Build Docker Image') {
steps {
// Build the Docker image for the application
sh "docker build -t ${DOCKER_IMAGE_TAG} ."
}
post {
always {
// Push the Docker image to Docker Hub
withCredentials([usernamePassword(credentialsId: 'docker-hub-credentials', passwordVariable: 'DOCKER_HUB_PASSWORD', usernameVariable: 'DOCKER_HUB_USERNAME')]) {
sh "docker login -u ${DOCKER_HUB_USERNAME} -p ${DOCKER_HUB_PASSWORD}"
sh "docker push ${DOCKER_IMAGE_TAG}"
}
}
}
}
stage('Deploy to Staging') {
steps {
// Deploy the application to a staging environment using Docker Compose
sh "sed -i 's#<DOCKER_IMAGE_TAG>#${DOCKER_IMAGE_TAG}#g' docker-compose.staging.yml"
sh "docker-compose -f docker-compose.staging.yml up -d"
}
}
post {
always {
// Show the application logs in Jenkins
sh 'docker-compose -f docker-compose.staging.yml logs --no-color'
}
}
stage('Functional Test') {
steps {
// Run functional tests on the deployed application
sh 'pytest --cov=src tests/functional/'
}
}
stage('Approval') {
steps {
// Send an email notification for approval
emailext (
subject: "Save Heart App Deployed to Staging - Approval Required",
body: "The Save Heart App has been deployed to the staging environment and is ready for approval. Please review the changes and approve or reject the deployment.",
to: "[email protected]",
from: "[email protected]",
replyTo: "[email protected]",
attachLog: true
)
}
post {
always {
// Archive the code coverage report
archiveArtifacts artifacts: 'htmlcov/**'
publishHTML(target: [allowMissing: false, alwaysLinkToLastBuild: true, keepAll: true, reportDir: 'htmlcov', reportFiles: 'index.html
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean package'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
}
stage('Deploy') {
steps {
withCredentials([usernamePassword(credentialsId: 'docker-hub', usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {
sh 'docker build -t your-docker-username/blood-bank-prediction .'
sh 'docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD'
sh 'docker push your-docker-username/blood-bank-prediction'
}
sh 'kubectl apply -f kubernetes.yaml'
}
}
}
}