-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.gitlab-ci.yml
More file actions
255 lines (255 loc) · 8.49 KB
/
.gitlab-ci.yml
File metadata and controls
255 lines (255 loc) · 8.49 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
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
.dagger:
image: ghcr.io/purpleclay/dagger-cli:0.18.10
variables:
MINIO: s3.waw3-2.cloudferro.com
BUCKET: registry-inference-server-controller
GITLAB: git.apps.eo4eu.eu
REPO_NAMESPACE: eo4eu/eo4eu-inference-server
HELM_NAMESPACE: eo4eu/eo4eu-cicd/cicd-infra
REGISTRY: registry.apps.eo4eu.eu
REPO: controller
SERVICE: eo4eu-inference-server
HELM: helm-repo
COOKIECUTTER_BRANCH: inference-server-controller
YAML_RULES: rules/python_rules.yaml
CONTEXT: build
rules:
- if: $CI_COMMIT_BRANCH == "main"
variables:
REPO_BRANCH: main
HELM_BRANCH: main
- if: $CI_COMMIT_BRANCH == "dev"
variables:
REPO_BRANCH: dev
HELM_BRANCH: dev
before_script:
- "apk update && apk --no-cache add wget"
- "export VAULT_TOKEN=$(wget -qO- --method=PUT \
--body-data='{\"role_id\":\"'\"$VAULT_ROLE_ID\"'\",\"secret_id\":\"'\"$VAULT_SECRET_ID\"'\"}' \
\"$VAULT_SERVER_URL/v1/auth/approle/login\" | \
jq -r '.auth.client_token')"
- "export BEARER=\"X-Vault-Token: $VAULT_TOKEN\""
- "export CI_REPO_USERNAME=$(wget -qO- --header=\"$BEARER\" \
\"$VAULT_SERVER_URL/v1/kv/data/eo4eu-cicd/gitlab_credentials/$SERVICE/$REPO\" | \
jq -r '.data.data.username')"
- "export CI_REPO_PASSWORD=$(wget -qO- --header=\"$BEARER\" \
\"$VAULT_SERVER_URL/v1/kv/data/eo4eu-cicd/gitlab_credentials/$SERVICE/$REPO\" | \
jq -r '.data.data.password')"
- "export CI_COOKIECUTTER_USERNAME=$(wget -qO- --header=\"$BEARER\" \
\"$VAULT_SERVER_URL/v1/kv/data/gitlab_credentials/cookiecutter-repo\" | \
jq -r '.data.data.username')"
- "export CI_COOKIECUTTER_PASSWORD=$(wget -qO- --header=\"$BEARER\" \
\"$VAULT_SERVER_URL/v1/kv/data/gitlab_credentials/cookiecutter-repo\" | \
jq -r '.data.data.password')"
- "export CI_HELM_USERNAME=$(wget -qO- --header=\"$BEARER\" \
\"$VAULT_SERVER_URL/v1/kv/data/gitlab_credentials/$HELM\" | \
jq -r '.data.data.username')"
- "export CI_HELM_PASSWORD=$(wget -qO- --header=\"$BEARER\" \
\"$VAULT_SERVER_URL/v1/kv/data/gitlab_credentials/$HELM\" | \
jq -r '.data.data.password')"
- "export S3_ACCESS_KEY=$(wget -qO- --header=\"$BEARER\" \
\"$VAULT_SERVER_URL/v1/kv/data/s3_cloudferro_creds/gitlab\" | \
jq -r '.data.data.s3_access_key')"
- "export S3_SECRET_KEY=$(wget -qO- --header=\"$BEARER\" \
\"$VAULT_SERVER_URL/v1/kv/data/s3_cloudferro_creds/gitlab\" | \
jq -r '.data.data.s3_secret_key')"
- "git clone -b $REPO_BRANCH \
https://$CI_REPO_USERNAME:$CI_REPO_PASSWORD@$GITLAB/$REPO_NAMESPACE/$REPO.git"
- "git clone -b $HELM_BRANCH \
https://$CI_HELM_USERNAME:$CI_HELM_PASSWORD@$GITLAB/$HELM_NAMESPACE/$HELM.git"
- "export TAG=$(git -C $REPO rev-parse HEAD)"
update_endpoint:
extends: [.dagger]
tags: [dagger]
stage: .pre
script:
- cd $REPO
- |
if git diff HEAD~1 | grep -Eq '^(- kafka_source_endpoint:)'; then
export KAFKA_ENDPOINT=$(grep 'kafka_source_endpoint:' \
cookiecutter-config.yaml | \
awk '{print $2}' | tr -d '"')
find $CONTEXT -type f -exec sed -i \
's/\(\s*"bootstrapServers": "\)\(.*\)/\1'"$KAFKA_ENDPOINT"'",/g' {} \;
find $CONTEXT -type f -exec sed -i \
's/\(\s*"bootstrapServers": \["\)\(.*\)/\1'"$KAFKA_ENDPOINT"'"\],/g' {} \;
git config user.email "federico.fornari@ecmwf.int"
git config user.name "Cookiecutter"
git add --all
git commit -m "Updated kafka endpoint by Cookiecutter" || true
git push -o ci.skip
fi
build_image:
extends: [.dagger]
tags: [dagger]
stage: build
needs: [update_endpoint]
script:
- cd $REPO
- |
if git diff --name-only HEAD~1 | grep -Eq '^('$CONTEXT'/)'; then
dagger call build \
--bucket $BUCKET \
--endpoint https://$MINIO \
--access env:S3_ACCESS_KEY \
--secret env:S3_SECRET_KEY \
--repo $REPO \
--tag $TAG \
--wkd $CONTEXT
fi
scan_image:
rules:
- if: '$CI_COMMIT_BRANCH == "main" && $CI_COMMIT_MESSAGE =~ /trivy/'
variables:
REPO_BRANCH: main
HELM_BRANCH: main
- when: never
extends: [.dagger]
tags: [dagger]
stage: test
needs:
- job: build_image
optional: true
dependencies: [build_image]
script:
- cd $REPO
- |
if git diff --name-only HEAD~1 | grep -Eq '^('$CONTEXT'/)'; then
dagger call scan \
--bucket $BUCKET \
--endpoint https://$MINIO \
--access env:S3_ACCESS_KEY \
--secret env:S3_SECRET_KEY \
--severity UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL \
--exit 0 \
--repo $REPO \
--tag $TAG \
--wkd /builds/$REPO_NAMESPACE/$REPO \
export \
--path \
/builds/$REPO_NAMESPACE/$REPO/vulnerabilities.html
dagger call sbom \
--bucket $BUCKET \
--endpoint https://$MINIO \
--access env:S3_ACCESS_KEY \
--secret env:S3_SECRET_KEY \
--repo $REPO \
--tag $TAG \
--wkd /builds/$REPO_NAMESPACE/$REPO \
export \
--path \
/builds/$REPO_NAMESPACE/$REPO
dagger call analyze-with-sonarqube \
--yaml-rules $YAML_RULES \
--source-directory $CONTEXT \
--sonar-host-url $SONAR_HOST_URL \
--sonar-token $SONAR_TOKEN \
--sonar-project-key $SONAR_PROJECT_KEY \
--output-name sonar-report.sarif \
export --path /builds/$REPO_NAMESPACE/$REPO/
dagger call analyze-with-gitguardian \
--gitguardian-api-key env:GITGUARDIAN_API_KEY \
--source-directory $CONTEXT \
--output-name gitguardian-report.sarif \
export --path /builds/$REPO_NAMESPACE/$REPO/
dagger call synthetic-report \
--sonar-sarif /builds/$REPO_NAMESPACE/$REPO/sonar-report.sarif \
--gg-sarif /builds/$REPO_NAMESPACE/$REPO/gitguardian-report.sarif \
--sbom-file /builds/$REPO_NAMESPACE/$REPO/sbom-report.cdx.json \
--severity-threshold HIGH \
export --path /builds/$REPO_NAMESPACE/$REPO
fi
artifacts:
when: always
expire_in: 4 weeks
paths:
- vulnerabilities.html
- sbom-report.html
- sonar-report.html
- synthetic-report.html
push_image:
extends: [.dagger]
tags: [dagger]
stage: deploy
needs:
- job: build_image
optional: true
- job: scan_image
optional: true
script:
- cd $REPO
- |
if git diff --name-only HEAD~1 | grep -Eq '^('$CONTEXT'/)'; then
dagger call push \
--bucket $BUCKET \
--endpoint https://$MINIO \
--access env:S3_ACCESS_KEY \
--secret env:S3_SECRET_KEY \
--registry $REGISTRY \
--namespace $REPO_NAMESPACE \
--repo $REPO \
--srctag $TAG \
--dsttag $TAG \
--username $CI_REPO_USERNAME \
--password env:CI_REPO_PASSWORD \
--wkd $CONTEXT
fi
update_helm:
extends: [.dagger]
tags: [dagger]
stage: .post
needs: [push_image]
artifacts:
untracked: true
script:
- cd $REPO
- |
if git diff --name-only HEAD~1 | grep -Eq '^('$CONTEXT'/)'; then
sed -i 's/\( service_image_tag: "\)\(.*\)/\1'"$TAG"'"/g' cookiecutter-config.yaml
git config user.email "federico.fornari@ecmwf.int"
git config user.name "Cookiecutter"
git add --all
git commit -m "Updated image tag by Cookiecutter" || true
git push -o ci.skip
fi
- dagger call update
--gitlab $GITLAB
--repo $REPO
--branch $COOKIECUTTER_BRANCH
--username $CI_COOKIECUTTER_USERNAME
--password env:CI_COOKIECUTTER_PASSWORD
--wkd .
export
--path
/builds/$REPO_NAMESPACE/$REPO/$SERVICE-helm
sync_helm:
extends: [.dagger]
tags: [dagger]
stage: .post
needs: [update_helm]
dependencies: [update_helm]
artifacts:
untracked: true
script:
- rm -rf $HELM/$SERVICE-$REPO
- cp -r /builds/$REPO_NAMESPACE/$REPO/$SERVICE-helm $HELM/$SERVICE-$REPO
- cd $HELM
- git config user.email "federico.fornari@ecmwf.int"
- git config user.name "Cookiecutter"
- git add --all
- git commit -m "Updated helm chart by Cookiecutter" || true
- git push
clean_registry:
extends: [.dagger]
tags: [dagger]
stage: .post
script:
- cd $REPO
- |
if git diff --name-only HEAD~1 | grep -Eq '^('$CONTEXT'/)'; then
dagger call clean \
--bucket $BUCKET \
--endpoint https://$MINIO \
--access env:S3_ACCESS_KEY \
--secret env:S3_SECRET_KEY
fi