-
Notifications
You must be signed in to change notification settings - Fork 0
executable file
·300 lines (253 loc) · 9.07 KB
/
ci.yaml
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
#
name: React.js CI
on:
push:
branches: "main"
jobs:
frontend-test:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./quiz-app
strategy:
matrix:
node-version: [20.x]
architecture: [x64]
steps:
- name: Check-out git repository
uses: actions/checkout@v4
- name: USE NODEJS ${{ matrix.node-version }} - ${{ matrix.architecture }}
uses: actions/setup-node@v4
- name: Install project dependencies
working-directory: ./quiz-app
run: |
npm i
npm run lint
npm install --save-dev --save-exact prettier
npm run prettier
env:
CI: true
- name: Build
run: npm run build
working-directory: ./quiz-app
# Setup sonar-scanner
- name: Setup SonarQube
uses: warchant/setup-sonar-scanner@v8
- name: Analyze with SonarCloud
uses: sonarsource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
with:
projectBaseDir: quiz-app
args: >
-Dsonar.organization=${{ secrets.SONAR_ORGANIZATION }}
-Dsonar.projectKey=${{ secrets.SONAR_PROJECT_KEY }}
-Dsonar.host.url=${{ secrets.SONAR_URL }}
-Dsonar.login=${{ secrets.SONAR_TOKEN }}
-Dsonar.sources=src/
-Dsonar.verbose=true
backend-test:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./backend
strategy:
matrix:
node-version: [20.x]
architecture: [x64]
steps:
- name: Check-out git repository
uses: actions/checkout@v4
- name: USE NODEJS ${{ matrix.node-version }} - ${{ matrix.architecture }}
uses: actions/setup-node@v4
- name: Install project dependencies
working-directory: ./backend
run: |
npm i
npm run lint
npm install --save-dev --save-exact prettier
npm run prettier
env:
CI: true
# Setup sonar-scanner
- name: Setup SonarQube
uses: warchant/setup-sonar-scanner@v8
- name: Analyze with SonarCloud
uses: sonarsource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
with:
projectBaseDir: backend
args: >
-Dsonar.organization=${{ secrets.SONAR_ORGANIZATION }}
-Dsonar.projectKey=${{ secrets.SONAR_PROJECT_KEY }}
-Dsonar.host.url=${{ secrets.SONAR_URL }}
-Dsonar.login=${{ secrets.SONAR_TOKEN }}
-Dsonar.sources=.
-Dsonar.verbose=true
frontend-security:
needs: frontend-test
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./quiz-app
steps:
- uses: actions/checkout@master
- name: Run Snyk to check for vulnerabilities
uses: snyk/actions/node@master
continue-on-error: true # To make sure that SARIF upload gets called
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
- name: Install Snyk CLI
uses: snyk/actions/setup@master
with:
version: latest
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
- name: Snyk Authenticate
run: snyk auth ${{ secrets.SNYK_TOKEN }}
- name: Snyk Code Test
run: snyk code test --all-projects
continue-on-error: true
backend-security:
needs: backend-test
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./backend
steps:
- uses: actions/checkout@master
- name: Run Snyk to check for vulnerabilities
uses: snyk/actions/node@master
continue-on-error: true # To make sure that SARIF upload gets called
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
- name: Install Snyk CLI
uses: snyk/actions/setup@master
with:
version: latest
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
- name: Snyk Authenticate
run: snyk auth ${{ secrets.SNYK_TOKEN }}
- name: Snyk Code Test
run: snyk code test --all-projects
continue-on-error: true
frontend-image:
needs: frontend-security
runs-on: ubuntu-latest
permissions:
contents: read
security-events: write
actions: read
steps:
- uses: actions/checkout@v4
- name: Build and push backend Docker image
working-directory: ./quiz-app
run: |
docker build . -t ${{ secrets.DOCKER_USERNAME }}/frontend-js:${{ github.run_number }}
echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
docker push ${{ secrets.DOCKER_USERNAME }}/frontend-js:${{ github.run_number }}
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
image-ref: 'docker.io/${{ secrets.DOCKER_USERNAME }}/frontend-js:${{ github.run_number }}'
format: 'sarif'
output: 'trivy-results.sarif'
severity: 'CRITICAL,HIGH'
- name: Install Snyk CLI
uses: snyk/actions/setup@master
with:
snyk-token: ${{ secrets.SNYK_TOKEN }}
- name: Snyk Authenticate
run: snyk auth ${{ secrets.SNYK_TOKEN }}
- name: Snyk Container monitor
run: snyk container monitor ${{ secrets.DOCKER_USERNAME }}/frontend-js --file=Dockerfile
working-directory: ./quiz-app
- name: Run Snyk to check for vulnerabilities in the Docker image
uses: snyk/actions/docker@master
with:
image: ${{ secrets.DOCKER_USERNAME }}/frontend-js
args: --file=quiz-app/Dockerfile --severity-threshold=high
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
continue-on-error: true
backend-image:
needs: backend-security
runs-on: ubuntu-latest
permissions:
contents: read
security-events: write
actions: read
steps:
- uses: actions/checkout@v4
- name: Build and push backend Docker image
working-directory: ./backend
run: |
docker build . -t ${{ secrets.DOCKER_USERNAME }}/backend-api:${{ github.run_number }}
echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
docker push ${{ secrets.DOCKER_USERNAME }}/backend-api:${{ github.run_number }}
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
image-ref: 'docker.io/${{ secrets.DOCKER_USERNAME }}/backend-api:${{ github.run_number }}'
format: 'sarif'
output: 'trivy-results.sarif'
severity: 'CRITICAL,HIGH'
- name: Install Snyk CLI
uses: snyk/actions/setup@master
with:
snyk-token: ${{ secrets.SNYK_TOKEN }}
- name: Snyk Authenticate
run: snyk auth ${{ secrets.SNYK_TOKEN }}
- name: Snyk Container monitor
run: snyk container monitor ${{ secrets.DOCKER_USERNAME }}/backend-api --file=Dockerfile
working-directory: ./backend
- name: Run Snyk to check for vulnerabilities in the Docker image
uses: snyk/actions/docker@master
with:
image: ${{ secrets.DOCKER_USERNAME }}/backend-api
args: --file=backend/Dockerfile --severity-threshold=high
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
continue-on-error: true
k8s-manifest-scan:
needs: [backend-security, frontend-security]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run Snyk to check Kubernetes manifest file for issues
uses: snyk/actions/iac@master
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
with:
file: kubernetes-manifest/
args: --severity-threshold=high
update-images-at-manifest-files:
needs: [k8s-manifest-scan, backend-image, frontend-image]
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Git
run: |
git config user.email "ebrahem.mohamedzaghloul@gmail.com"
git config user.name "ibrahimzaghloul"
- name: Change image version in frontend and backend deployment files
run: |
echo "change image version .."
sed -i "s|image:.*frontend-js:.*|image: ${{ secrets.DOCKER_USERNAME }}/frontend-js:${{ github.run_number }}|g" kubernetes-manifest/frontend.yaml
sed -i "s|image:.*backend-api:.*|image: ${{ secrets.DOCKER_USERNAME }}/backend-api:${{ github.run_number }}|g" kubernetes-manifest/backend.yaml
- name: Commit changes
run: |
git add .
git commit -m "Update deployment image to version ${{ github.run_number }}"
- name: Push changes
uses: ad-m/github-push-action@v0.6.0
with:
github_token: ${{ secrets._GITHUB_TOKEN }}
branch: main # Update the branch name to 'main'