-
Notifications
You must be signed in to change notification settings - Fork 0
332 lines (290 loc) · 11.8 KB
/
mainPipeline.yml
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
name: frontend-master-workflow
on:
push:
branches:
- L00171125_dissertation
# pull_request:
# types:
# - "opened"
jobs:
ShiftLeft_test:
runs-on: ubuntu-latest
if: false
permissions:
actions: write
security-events: write
steps:
- name: Checkout codes
uses: actions/checkout@v4
- name: Install Dependencies
run: |
ls
pwd
cd /home/runner/work/L00171125_Dissertationproject/L00171125_Dissertationproject/WWA-backend-master
npm install --force --save-dev jest
- name: Run Tests
run: |
cd /home/runner/work/L00171125_Dissertationproject/L00171125_Dissertationproject/WWA-backend-master
npm run test -- --coverage
- name: Run Linter
run: |
cd /home/runner/work/L00171125_Dissertationproject/L00171125_Dissertationproject/WWA-backend-master
npm install eslint --save-dev
npx eslint . --fix
- name: Run Linter and Generate Report
run: |
cd /home/runner/work/L00171125_Dissertationproject/L00171125_Dissertationproject/WWA-backend-master
npx eslint . -f json -o eslint-report.json
- name: Upload Linting Report
uses: actions/upload-artifact@v2
with:
name: eslint-report
path: /home/runner/work/L00171125_Dissertationproject/L00171125_Dissertationproject/WWA-backend-master/eslint-report.json
if-no-files-found: warn
- name: Upload Unit Test Coverage Report
uses: actions/upload-artifact@v2
with:
name: unit-test-coverage
path: /home/runner/work/L00171125_Dissertationproject/L00171125_Dissertationproject/WWA-backend-master/coverage/
if-no-files-found: warn
# Snyk security scan step for web
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '20'
- name: Install Snyk CLI
run: npm install -g snyk
- name: Snyk Security Scans
run: snyk test --org=abhi9705 --project-name='L00171125_Dissertationproject'
continue-on-error: true
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
working-directory: WWA-backend-master
- name: Snyk Monitor
run: snyk monitor --project-name='L00171125_Dissertationproject'
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
working-directory: WWA-backend-master
- name: Snyk Monitor Frontend
run: |
npm install --force
snyk monitor --org=abhi9705 --project-name='L00171125_Dissertationproject'
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
working-directory: WWA-frontend-master
# Snyk security scan step for Code Scanning
- name: Run Snyk to check for vulnerabilitie
uses: snyk/actions/node@master
continue-on-error: true
with:
args: ./WWA-backend-master --sarif-file-output=snyk.sarif
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
- name: Upload SARIF to GitHub Security Center
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: snyk.sarif
category: 'Snyk OpenSource Unique Category'
SonarCloudScan:
runs-on: ubuntu-latest
needs: ShiftLeft_test
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '16'
- name: Install dependencies
run: cd WWA-backend-master && npm install
- name: Run Jest tests with coverage
run: cd WWA-backend-master && npm test -- --coverage || true
# Run tests with coverage, ignoring failures with '|| true'
- name: Set up JDK 17
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'adopt'
- name: SonarCloud Scan
# uses: sonarsource/sonarcloud-github-action@master
uses: SonarSource/sonarcloud-github-action@master
# uses: SonarSource/sonarcloud-github-action@de2e56b42aa84d0b1c5b622644ac17e505c9a049
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONARCLOUD_TOKEN }}
with:
args: >
-Dsonar.organization=abhi9705
-Dsonar.projectKey=Abhi9705_L00171125_Dissertationproject
-Dsonar.sources=.
-Dsonar.login=${{ secrets.SONARCLOUD_TOKEN }}
-Dsonar.javascript.lcov.reportPaths=WWA-backend-master/coverage/lcov.info
Build_docker_Images:
runs-on: ubuntu-latest
# needs: ShiftLeft_test
# if: false
steps:
- name: Checkout codes
uses: actions/checkout@v4
- name: Sign in to DockerHub
run: docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{secrets.DOCKER_PASSWORD}}
- name: Deleting old Images
run: |
docker rmi -f $(docker image ls -a -q)
- name: Docker Frontend Images
run: |
cd /home/runner/work/L00171125_Dissertationproject/L00171125_Dissertationproject/WWA-frontend-master
docker build -t wwag3/wednesdaywickedadventures:latest .
- name: Build Docker Backend Images
run: |
cd /home/runner/work/L00171125_Dissertationproject/L00171125_Dissertationproject/WWA-backend-master
docker build -t wwag3/wednesdaywickedadvent:backend .
- name: Publish Image to docker Hub
run: |
docker push wwag3/wednesdaywickedadventures:latest
docker push wwag3/wednesdaywickedadvent:backend
Running_Stagging_Environment:
runs-on: self-hosted
needs: Build_docker_Images
# if: false
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: removing Disk space
run: |
sudo docker system prune -a -f --volumes
- name: Running application on Stagging environment
run: |
echo "will run when new images is published"
# sudo docker pull wwag3/wednesdaywickedadventures:latest
- name: Kill and Remove Existing Container
run: |
if [ "$(sudo docker ps -q -f name=forntend)" ]; then
echo "Stopping and removing existing container 'forntend'."
sudo docker stop forntend
sudo docker rm forntend
elif [ "$(sudo docker ps -aq -f name=forntend)" ]; then
echo "Removing existing container 'forntend'."
sudo docker rm forntend
fi
echo "Starting new container 'forntend'."
sudo docker run -d -p 3000:3000 --name forntend wwag3/wednesdaywickedadventures:latest
Chaos_Engineering:
runs-on: ubuntu-latest
# needs: Running_Stagging_Environment
if: false
steps:
- name: Checkout codes
uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: eu-west-1
- name: Trigger FIS Experiments
id: start_fis
run: |
for i in {1..3}; do
echo "Running Chaos Engineering Experiment Iteration #$i"
FIS_EXP_ID=$(aws fis start-experiment --experiment-template-id EXTBY5y1A6MqkfjXn --query 'experiment.id' --output text)
echo "FIS_EXP_ID=$FIS_EXP_ID" >> $GITHUB_ENV
if [ -z "$FIS_EXP_ID" ]; then
echo "Failed to start experiment. Exiting."
exit 1
fi
echo "FIS_EXP_ID=$FIS_EXP_ID"
echo "Wait for FIS Experiment to Complete"
INSTANCE_ID=i-0609b0b289067d17d
INSTANCE_STATUS=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --query 'Reservations[0].Instances[0].State.Name' --output text)
echo "Current instance status: $INSTANCE_STATUS"
echo "Start MTTR Scriptss"
chmod +x ./measure_mttr.sh
./measure_mttr.sh "$FIS_EXP_ID" &
while true; do
STATUS=$(aws fis get-experiment --id "$FIS_EXP_ID" --query 'experiment.state.status' --output text)
if [ -z "$STATUS" ]; then
echo "Failed to get experiment status. Exiting."
exit 1
fi
echo "Current experiment status: $STATUS"
INSTANCE_STATUS=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --query 'Reservations[0].Instances[0].State.Name' --output text)
echo "Current instance status: $INSTANCE_STATUS"
echo "-----------------------------------------------------------"
if [ "$STATUS" == "completed" ]; then
break
elif [ "$STATUS" == "failed" ] || [ "$STATUS" == "stopped" ]; then
echo "Experiment $FIS_EXP_ID did not complete successfully. Status: $STATUS"
exit 1
fi
sleep 10
done
echo "Wait for EC2 Instance to Return to Running"
INSTANCE_ID=i-0609b0b289067d17d
while true; do
INSTANCE_STATUS=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --query 'Reservations[0].Instances[0].State.Name' --output text)
echo "Current instance status: $INSTANCE_STATUS"
if [ "$INSTANCE_STATUS" == "running" ]; then
echo "Instance $INSTANCE_ID is back to running."
break
fi
sleep 15
done
echo "Iteration #$i completed."
echo "----------------------------------------------"
done
Production_Environment:
runs-on: ubuntu-latest
if: false
# needs: [Chaos_Engineering]
permissions: write-all
outputs:
target_url: ${{ steps.running-terraform-commands.outputs.target }}
steps:
- name: Checkout codess
uses: actions/checkout@v4
- name: Configure AWS
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{secrets.AWS_ACCESS_KEY_ID}}
aws-secret-access-key: ${{secrets.AWS_SECRET_ACCESS_KEY}}
aws-region: ${{secrets.AWS_REGION}}
- name: Setup Terraforms
uses: hashicorp/setup-terraform@v3
with:
terraform_version: "1.1.7"
- name: Running Terraform Commands
id: running-terraform-commands
working-directory: ./terraform
run: |
cd /home/runner/work/L00171125_Dissertationproject/L00171125_Dissertationproject/terraform
terraform init
terraform validate -no-color
terraform plan
terraform apply -auto-approve | tee terraform_output.txt
TARGET_URL=$(terraform output ec2_public_ip)
echo "::set-output name=target::$TARGET_URL"
echo "EC2 IP Address: $TARGET_URL" # Print EC2 IP in the pipeline
echo "##[group]Terraform Output"
cat terraform_output.txt
echo "##[endgroup]"
# terraform destroy -auto-approve
# Pulling_docker_image:
# runs-on: self-hosted
# needs: Build_docker_FrontendImage
# steps:
# - name: Checkout codess
# uses: actions/checkout@v4
#- name: Configure AWS
# uses: aws-actions/configure-aws-credentials@v4
#with:
# aws-access-key-id: ${{secrets.AWS_ACCESSKEY}}
#aws-secret-access-key: ${{secrets.AWS_SECRETEKEY}}
# aws-region: ${{secrets.AWS_REGION}}
#- name: pulling frontend image from docker hub
# run: sudo docker pull wwag3/wednesdaywickedadventures:latest
# - name: building backend container
# run: sudo docker run --name wwabackendapp -p 5000:5000 -d wwag3/wednesdaywickedadventures:backend
# sudo docker system prune --all --force --volumes, docker system prune -a -y