-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
150 lines (143 loc) · 6.46 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
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
pipeline {
agent any
environment {
PATH = "/usr/bin:$PATH"
HEADLESS = 'true'
OUT = 'jmeter.save.saveservice.output_format'
JMX = '/opt/apache-jmeter-5.6.3/bin/InventoryMan_JMeter_Test.jmx'
JTL = '/opt/apache-jmeter-5.6.3/reports/InventoryMan_JMeter_Test.report.jtl'
}
stages {
stage('Build Frontend') {
steps {
sh "echo Building frontend"
sh "cd frontend && npm install && npm run build"
}
}
stage('Build Backend') {
steps {
sh "cd backend && mvn clean install -DskipTests=true" // skip tests for deployment
}
}
stage('Deploy Frontend') {
steps {
script {
try {
withAWS(region: 'us-east-1', credentials: 'AWS_CREDENTIALS') {
sh "aws s3 sync frontend/build s3://inventoryman"
}
} catch (Exception e) {
echo "${e}"
throw e
}
}
}
}
stage('Deploy Backend') {
steps {
script {
withAWS(region: 'us-east-1', credentials: 'AWS_CREDENTIALS') {
def versionLabel = "0.0.1-${new Date().format('yyyyMMddHHmmss')}" // Change version label for each deployment
def s3Key = "inventoryman-${versionLabel}.jar"
sh "aws s3 cp backend/target/inventoryman-0.0.1-SNAPSHOT.jar s3://inventoryman-backend/${s3Key}"
sh "aws elasticbeanstalk create-application-version --application-name InventoryMan --version-label ${versionLabel} --source-bundle S3Bucket=inventoryman-backend,S3Key=${s3Key}"
sh "aws elasticbeanstalk update-environment --environment-name InventoryMan-env --version-label ${versionLabel}"
}
}
}
}
stage('Run Tests') {
steps {
script {
def frontendReady = false
def backendReady = false
// Wait for frontend deployment
echo "Waiting for frontend deployment..."
retry(5) { // Retry up to 5 times
sleep(10) // Wait 10 seconds between each retry
def response = sh(script: "curl -s -o /dev/null -w \"%{http_code}\" http://inventoryman.s3-website-us-east-1.amazonaws.com/", returnStdout: true).trim()
if (response == '200') {
frontendReady = true
} else {
echo "Frontend response: ${response}"
error "Frontend not ready yet, retrying..."
}
}
// Wait for backend deployment before tests begin
echo "Waiting for backend deployment..."
retry(5) {
sleep(10)
def response = sh(script: "curl -s -o /dev/null -w \"%{http_code}\" http://inventoryman-env.us-east-1.elasticbeanstalk.com:8080/api/warehouses", returnStdout: true).trim()
if (response == '200') {
backendReady = true
} else {
echo "Backend response: ${response}"
error "Backend not ready yet, retrying..."
}
}
// If both frontend and backend are ready, proceed with tests
if (frontendReady && backendReady) {
sh "cd frontend && npm test -- --coverage" // Run Jest tests, generate coverage report
sh "cd backend && mvn test jacoco:report" // Run Selenium, Cucumber, and Mockito tests, generate coverage report
} else {
error "One or both services are not ready. Aborting tests."
}
}
}
}
stage('Archive JaCoCo Report') {
steps {
archiveArtifacts artifacts: 'backend/target/site/jacoco/*', allowEmptyArchive: true
}
}
stage('Analyze Frontend with SonarCloud') {
steps {
script {
withSonarQubeEnv('SonarCloud') {
dir('frontend') {
sh '''
npx sonar-scanner \
-Dsonar.projectKey=salmoncore_InventoryMan \
-Dsonar.projectName=InventoryMan \
-Dsonar.sources=src \
-Dsonar.exclusions=**/__tests__/** \
-Dsonar.javascript.lcov.reportPaths=coverage/lcov.info
''' // Project: https://sonarcloud.io/project/information?id=salmoncore_InventoryMan
}
}
}
}
}
stage('Analyze Backend with SonarCloud') {
steps {
script {
withSonarQubeEnv('SonarCloud') {
dir('backend') {
sh '''
mvn sonar:sonar \
-Dsonar.projectKey=salmoncore_inventoryman-backend \
-Dsonar.projectName=InventoryMan_Backend \
-Dsonar.java.binaries=target/classes \
-Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml
''' // Project: https://sonarcloud.io/project/information?id=salmoncore_inventoryman-backend
}
}
}
}
}
stage('Run JMeter Test') {
steps {
script {
sh """
/opt/apache-jmeter-5.6.3/bin/jmeter -j \$OUT=xml -n -t \$JMX -l \$JTL
"""
}
}
}
stage('Publish JMeter Report') {
steps {
perfReport filterRegex: '', sourceDataFiles: '/opt/apache-jmeter-5.6.3/reports/InventoryMan_JMeter_Test.report.jtl'
}
}
}
}