-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
126 lines (112 loc) · 3.65 KB
/
Jenkinsfile
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
@Library("my-shared-library") _
pipeline {
agent any
options {
disableConcurrentBuilds()
buildDiscarder(logRotator(daysToKeepStr: '30'))
timestamps()
}
environment {
APP_IMAGE_NAME = 'python-app-image'
WEB_IMAGE_NAME = 'web-image'
DOCKER_COMPOSE_FILE = 'docker-compose.yml'
BUILD_DATE = new Date().format('yyyyMMdd-HHmmss')
IMAGE_TAG = "v1.0.0-${BUILD_NUMBER}-${BUILD_DATE}"
SNYK_TOKEN = credentials('snyk-token')
NEXUS_PROTOCOL = "http"
NEXUS_URL = "172.24.216.163:8888"
NEXUS_REPOSITORY = "my-docker-repo"
NEXUS_CREDENTIALS_ID = "nexus"
}
stages {
stage('Hello') {
steps {
wrap([$class: 'BuildUser']) {
greet()
}
}
}
stage('Unit Tests') {
steps {
// Ensure Python requirements are installed
sh 'pip3 install pytest'
// Run pytest for unit tests
sh 'python3 -m pytest --junitxml=results.xml app/tests/*.py'
}
post {
always {
junit allowEmptyResults: true, testResults: 'results.xml'
}
}
}
stage('Lint') {
steps {
echo "Pylint running"
//sh 'pylint src/'
}
}
stage('Build Docker Image') {
steps {
script {
// Build Docker image using docker-compose
sh 'docker-compose -f ${DOCKER_COMPOSE_FILE} build'
}
}
}
stage('Snyk login') {
steps {
snykLogin('${SNYK_TOKEN}')
}
}
stage('Snyk Container Test') {
steps {
script {
// Test Docker image for vulnerabilities
sh 'snyk container test ${APP_IMAGE_NAME}:latest --policy-path=.snyk'
}
}
}
stage('Nexus login') {
steps {
nexusLogin("${NEXUS_CREDENTIALS_ID}","${NEXUS_PROTOCOL}","${NEXUS_URL}", "${NEXUS_REPOSITORY}")
}
}
stage('Tag and Push To Nexus') {
steps {
script {
sh '''
docker tag ${APP_IMAGE_NAME}:latest ${NEXUS_URL}/${APP_IMAGE_NAME}:${IMAGE_TAG}
docker push ${NEXUS_URL}/${APP_IMAGE_NAME}:${IMAGE_TAG}
docker tag ${WEB_IMAGE_NAME}:latest ${NEXUS_URL}/${WEB_IMAGE_NAME}:${IMAGE_TAG}
docker push ${NEXUS_URL}/${WEB_IMAGE_NAME}:${IMAGE_TAG}
'''
}
}
}
stage('Deploy') {
steps {
echo "Deploying to k8s cluster"
}
}
}
post {
always {
// Clean up the workspace!
cleanWs()
}
success {
// send notification about succeded build
echo "sending notification: Build ${BUILD_NUMBER} has succeeded"
//mail to: 'team@example.com',
// subject: "Pipeline Success: ${currentBuild.fullDisplayName}",
// body: "Good news! The pipeline has succeeded."
}
failure {
// send notification about failed build
echo "sending notification: Build ${BUILD_NUMBER} has failed"
//mail to: 'team@example.com',
// subject: "Pipeline Failure: ${currentBuild.fullDisplayName}",
// body: "Unfortunately, the pipeline has failed. Please check the logs."
}
}
}