-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
194 lines (170 loc) · 6.24 KB
/
Jenkinsfile
File metadata and controls
194 lines (170 loc) · 6.24 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
pipeline {
agent any
options { timestamps(); ansiColor('xterm') }
environment {
IMAGE = 'osaidbahabri/hd-jenkins-demo'
XDG_CONFIG_HOME = "${WORKSPACE}/.xdg"
SONAR_TOKEN = credentials('sonar-token')
}
stages {
stage('Checkout') {
steps {
checkout scm
sh 'mkdir -p "$XDG_CONFIG_HOME/jgit" || true'
}
}
stage('Build & Unit Tests') {
steps {
sh '''
node -v && npm ci
mkdir -p reports/junit
export NODE_OPTIONS=--experimental-vm-modules
export JEST_JUNIT_OUTPUT=reports/junit/junit-results.xml
npm test -- --ci --coverage --reporters=default --reporters=jest-junit
'''
}
post {
always {
junit allowEmptyResults: true, testResults: 'reports/junit/junit-results.xml'
}
}
}
stage('Mutation Tests (Stryker)') {
steps {
sh '''
export NODE_OPTIONS=--experimental-vm-modules
npx stryker run || true
'''
archiveArtifacts 'reports/mutation/**/*, **/stryker*.json'
}
}
stage('Lint & SAST') {
environment {
SONAR_TOKEN = credentials('sonar-token')
}
steps {
sh 'npm run lint || true'
sh '''
if [ -n "$SONAR_TOKEN" ]; then
sonar-scanner
else
echo "SONAR_TOKEN not set, skipping Sonar analysis"
fi
'''
}
}
stage('Contract Test (Pact))') {
steps {
sh '''
echo "Verifying pact file (stub step)"
test -f pact/pacts/consumer-provider.json || true
'''
archiveArtifacts 'pact/pacts/*.json'
}
}
stage('Docker Build + SBOM & Vuln Scan') {
steps {
sh '''
docker build -t $IMAGE:commit-$BUILD_NUMBER .
trivy image --format table --output trivy.txt $IMAGE:commit-$BUILD_NUMBER || true
syft scan $IMAGE:commit-$BUILD_NUMBER -o spdx-json > sbom.spdx.json || true
grype $IMAGE:commit-$BUILD_NUMBER -o table > grype.txt || true
'''
archiveArtifacts 'trivy.txt, grype.txt, sbom.spdx.json'
}
}
stage('Policy Gate (OPA)') {
steps {
sh 'conftest test Dockerfile --parser dockerfile -p policies || true'
}
}
stage('DAST (ZAP Baseline)') {
steps {
withCredentials([usernamePassword(credentialsId: 'dockerhub-creds', usernameVariable: 'DOCKERHUB_USER', passwordVariable: 'DOCKERHUB_PASS')]) {
sh '''
docker compose -f docker-compose.staging.yml down || true
docker compose -f docker-compose.staging.yml up -d --build
sleep 4
curl -fsS http://localhost:3001/health
echo "$DOCKERHUB_PASS" | docker login -u "$DOCKERHUB_USER" --password-stdin
docker pull owasp/zap2docker-stable || true
docker run --rm -t -v "$PWD":/zap/wrk owasp/zap2docker-stable \
zap-baseline.py -t http://host.docker.internal:3001 -r zap.html || true
docker logout || true
[ -f zap.html ] || echo "<html><body><h3>ZAP run skipped or failed to pull image.</h3></body></html>" > zap.html
'''
}
archiveArtifacts allowEmptyArchive: false, artifacts: 'zap.html'
echo "ZAP report: ${env.BUILD_URL}artifact/zap.html"
}
post { always { sh 'docker compose -f docker-compose.staging.yml down || true' } }
}
stage('Release Image') {
steps {
withCredentials([usernamePassword(credentialsId: 'dockerhub-creds', usernameVariable: 'DOCKERHUB_USER', passwordVariable: 'DOCKERHUB_PASS')]) {
sh '''
echo "$DOCKERHUB_PASS" | docker login -u "$DOCKERHUB_USER" --password-stdin
docker tag $IMAGE:commit-$BUILD_NUMBER $IMAGE:prod
docker push $IMAGE:prod
docker logout || true
'''
}
}
}
stage('Blue/Green Deploy + Health & Rollback') {
steps {
script {
withCredentials([usernamePassword(credentialsId: 'dockerhub-creds', usernameVariable: 'DOCKERHUB_USER', passwordVariable: 'DOCKERHUB_PASS')]) {
sh '''
set -e
docker rm -f api-green >/dev/null 2>&1 || true
docker rm -f api-blue >/dev/null 2>&1 || true
docker compose -f docker-compose.prod.green.yml down || true
docker compose -f docker-compose.prod.blue.yml down || true
docker ps -q --filter "publish=3000" | xargs -r docker rm -f || true
docker ps -q --filter "publish=3001" | xargs -r docker rm -f || true
echo "$DOCKERHUB_PASS" | docker login -u "$DOCKERHUB_USER" --password-stdin
docker pull $IMAGE:prod || true
docker compose -f docker-compose.prod.green.yml up -d --force-recreate
sleep 5
curl -fsS http://localhost:3001/health
docker compose -f docker-compose.prod.blue.yml down || true
docker rm -f api-green >/dev/null 2>&1 || true
docker run -d --name api-green -p 3000:3000 $IMAGE:prod
sleep 3
curl -fsS http://localhost:3000/health
docker logout || true
'''
}
}
}
post {
unsuccessful {
echo 'Health check failed. Rolling back to BLUE.'
sh '''
docker rm -f api-green >/dev/null 2>&1 || true
docker compose -f docker-compose.prod.green.yml down || true
docker ps -q --filter "publish=3000" | xargs -r docker rm -f || true
docker compose -f docker-compose.prod.blue.yml up -d --force-recreate
sleep 3
curl -fsS http://localhost:3000/health || true
'''
}
}
}
stage('Monitoring (light)') {
steps {
sh '''
echo "Health: $(curl -fsS http://localhost:3000/health || echo 'unreachable')"
docker logs --since 5m api-green | tail -n 50 > logs_tail.txt || true
'''
archiveArtifacts allowEmptyArchive: true, artifacts: 'logs_tail.txt'
}
}
}
post {
success { echo 'Secure, policy-gated, blue/green CI/CD complete.' }
failure { echo 'Pipeline failed. Check gates and scans above.' }
always { archiveArtifacts allowEmptyArchive: true, artifacts: 'zap.html' }
}
}