-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaction.yml
More file actions
399 lines (335 loc) · 12.7 KB
/
action.yml
File metadata and controls
399 lines (335 loc) · 12.7 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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
name: 'Kexa Action'
description: "It's Kexa version for a GitHub Action wrapped in Docker"
author: '4urcloud | Esteban MATHIA & Adrien EPPLING'
branding:
icon: 'check-circle'
color: 'blue'
inputs:
ENV_VARS:
description: 'All environment variables to be set in the .env file as a JSON object'
required: true
default: '{}'
KUBECONFIG:
description: 'Kubeconfig file content'
required: false
default: ''
KUBEPREFIX:
description: 'Kube var prefix'
required: false
default: ''
WORKSPACECRED:
description: 'Workspace credentials'
required: false
default: ''
WORKSPACECRED_PREFIX:
description: 'Workspace var prefix'
required: false
default: ''
GOOGLE_APPLICATION_CREDENTIALS:
description: 'Google application credentials'
required: false
default: ''
GOOGLE_APPLICATION_CREDENTIALS_PREFIX:
description: 'Google application credentials prefix'
required: false
default: ''
JIRA_API_KEY:
description: 'API key for Jira'
required: false
default: ''
JIRA_DONE_STATUS:
description: 'Done issue status for Jira project'
required: false
default: ''
JIRA_PROJECT_KEY:
description: 'Jira project key'
required: false
default: ''
JIRA_DOMAIN:
description: 'Jira domain used by user'
required: false
default: ''
DB_CONNECTION_STRING:
description: 'Database connection string'
required: false
default: ''
runs:
using: 'composite'
steps:
- name: Checkout code
uses: actions/checkout@v2
##################################
# ENVIRONMENT FILE CREATION #
##################################
- name: Create .env file with all environment variables
run: echo '${{ inputs.ENV_VARS }}' > .env
shell: bash
# copy JIRA crendentials into env
- name: Copy JIRA credentials into container
run: |
echo "JIRA_API_KEY=${{ inputs.JIRA_API_KEY }}" >> .env
echo "JIRA_DONE_STATUS=${{ inputs.JIRA_DONE_STATUS }}" >> .env
echo "JIRA_PROJECT_KEY=${{ inputs.JIRA_PROJECT_KEY }}" >> .env
echo "JIRA_DOMAIN=${{ inputs.JIRA_DOMAIN }}" >> .env
shell: bash
# copy DB connection string into env
- name: Copy DB connection string into container
run: |
echo "DB_CONNECTION_STRING=${{ inputs.DB_CONNECTION_STRING }}" >> .env
shell: bash
##################################
# CONFIG FILE CREATION #
##################################
###################
# KUBERNETES #
###################
- name: If Kubeconfig input exists, create kubeconfig file and set exist
id: check_kubeconfig
run: |
if [ -n "${{ inputs.KUBECONFIG }}" ]; then
echo "KUBECONFIG found."
echo "${{ inputs.KUBECONFIG }}" > kubernetes.yaml
echo "${{ inputs.KUBEPREFIX }}KUBECONFIG=/app/kubernetes.yaml" >> .env
echo "${{ inputs.KUBECONFIG }} " > kubernetes.json
chmod 700 kubernetes.json kubernetes.yaml
echo "exists=true" >> $GITHUB_OUTPUT
else
echo "KUBECONFIG not found."
echo "exists=false" >> $GITHUB_OUTPUT
fi
shell: bash
- name: Multiple kubconfig, create files and set env variables
id: check_kubeconfig_multiple
if: steps.check_kubeconfig.outputs.exists == 'false'
run: |
if [ -z "${{ inputs.KUBEPREFIX }}" ]; then
echo "KUBEPREFIX not found."
exit 0
fi
KUBEPREFIXES="${{ inputs.KUBEPREFIX }}"
IFS=',' read -r -a kubeprefixes <<< "$KUBEPREFIXES"
if [ ${#kubeprefixes[@]} -gt 0 ]; then
for idx in "${!kubeprefixes[@]}"; do
prefix="${kubeprefixes[$idx]}"
INPUT_PREFIX_KUBE="${prefix}KUBECONFIG"
newInputs='${{ toJson(inputs) }}'
kubeconfig=""
for key in $(echo "$newInputs" | jq -r 'keys[]'); do
value=$(echo "$newInputs" | jq -r ".\"$key\"")
if [ "$key" == "$INPUT_PREFIX_KUBE" ]; then
kubeconfig="${value}"
fi
done
echo "${kubeconfig}" > "kubernetes_${idx}.yaml"
echo "${prefix}KUBECONFIG=/app/kubernetes_${idx}.yaml" >> .env
echo "${kubeconfig}" | python -c "import yaml, json, sys; yaml_content = sys.stdin.read(); print(json.dumps(yaml.safe_load(yaml_content), indent=2))" > kubernetes_${idx}.json
chmod 700 "kubernetes_${idx}.yaml" "kubernetes_${idx}.json"
done
echo "exists=true" >> $GITHUB_OUTPUT
else
echo "No KUBEPREFIX values found."
echo "exists=false" >> $GITHUB_OUTPUT
fi
shell: bash
###################
# WORKSPACE #
###################
- name: If Workspace credentials input exists, create workspace credentials file and set exist
id: check_workspacecred
run: |
WORKSPACE_CREDS_COMPACT=$(echo '${{ inputs.WORKSPACECRED }}' | jq -c '.')
if [ -n "$WORKSPACE_CREDS_COMPACT" ] && [ "$WORKSPACE_CREDS_COMPACT" != "null" ]; then
echo "${{ inputs.WORKSPACECRED_PREFIX }}WORKSPACECRED=$WORKSPACE_CREDS_COMPACT" >> .env
echo "exists=true" >> $GITHUB_OUTPUT
else
echo "exists=false" >> $GITHUB_OUTPUT
fi
shell: bash
- name: Multiple workspace credentials, create files and set env variables
if: steps.check_workspacecred.outputs.exists == 'false'
run: |
echo "checking for multiple workspace credentials"
if [ -z "${{ inputs.WORKSPACECRED_PREFIX }}" ]; then
echo "WORKSPACECRED_PREFIX not found."
exit 0
fi
echo "WORKSPACECRED_PREFIX found for multiple conf."
WORKSPACEPREFIXES="${{ inputs.WORKSPACECRED_PREFIX }}"
IFS=',' read -r -a workspace_prefixes <<< "$WORKSPACEPREFIXES"
if [ ${#workspace_prefixes[@]} -gt 0 ]; then
for idx in "${!workspace_prefixes[@]}"; do
prefix="${workspace_prefixes[$idx]}"
INPUT_PREFIX_WORKSPACE="${prefix}WORKSPACECRED"
newInputs='${{ toJson(inputs) }}'
workspace=""
for key in $(echo "$newInputs" | jq -r 'keys[]'); do
value=$(echo "$newInputs" | jq -r ".\"$key\"")
if [ "$key" == "$INPUT_PREFIX_WORKSPACE" ]; then
workspace="${value}"
fi
done
WORKSPACE_CREDS_COMPACT=$(echo "${workspace}" | jq -c '.')
echo "${prefix}WORKSPACECRED=$WORKSPACE_CREDS_COMPACT" >> .env
done
echo "exists=true" >> $GITHUB_OUTPUT
else
echo "No WORKSPACECRED_PREFIX values found."
echo "exists=false" >> $GITHUB_OUTPUT
fi
shell: bash
############
# GCP #
############
- name: If Google application credentials input exists, create Google application credentials file and set exist
id: check_google_application_credentials
run: |
GCP_CREDS_COMPACT=$(echo '${{ inputs.GOOGLE_APPLICATION_CREDENTIALS }}' | jq -c '.')
if [ -n "$GCP_CREDS_COMPACT" ] && [ "$GCP_CREDS_COMPACT" != "null" ]; then
echo "${{ inputs.GOOGLE_APPLICATION_CREDENTIALS_PREFIX }}GOOGLE_APPLICATION_CREDENTIALS=$GCP_CREDS_COMPACT" >> .env
echo "exists=true" >> $GITHUB_OUTPUT
else
echo "exists=false" >> $GITHUB_OUTPUT
fi
shell: bash
- name: Multiple google application credentials, create files and set env variables
if: steps.check_google_application_credentials.outputs.exists == 'false'
run: |
echo "checking for multiple GCP credentials"
if [ -z "${{ inputs.GOOGLE_APPLICATION_CREDENTIALS_PREFIX }}" ]; then
echo "GOOGLE_APPLICATION_CREDENTIALS_PREFIX not found."
exit 0
fi
echo "GOOGLE_APPLICATION_CREDENTIALS_PREFIX found for multiple conf."
GOOGLE_APPLICATION_CREDENTIALS_PREFIXES="${{ inputs.GOOGLE_APPLICATION_CREDENTIALS_PREFIX }}"
IFS=',' read -r -a google_application_credentials_prefixes <<< "$GOOGLE_APPLICATION_CREDENTIALS_PREFIXES"
if [ ${#google_application_credentials_prefixes[@]} -gt 0 ]; then
for idx in "${!google_application_credentials_prefixes[@]}"; do
prefix="${google_application_credentials_prefixes[$idx]}"
INPUT_PREFIX_GCP="${prefix}GOOGLE_APPLICATION_CREDENTIALS"
newInputs='${{ toJson(inputs) }}'
gcp=""
for key in $(echo "$newInputs" | jq -r 'keys[]'); do
value=$(echo "$newInputs" | jq -r ".\"$key\"")
if [ "$key" == "$INPUT_PREFIX_GCP" ]; then
gcp="${value}"
fi
done
GCP_CREDS_COMPACT=$(echo "$gcp" | jq -c '.')
echo "${prefix}GOOGLE_APPLICATION_CREDENTIALS=$GCP_CREDS_COMPACT" >> .env
done
echo "exists=true" >> $GITHUB_OUTPUT
else
echo "No GOOGLE_APPLICATION_CREDENTIALS_PREFIX values found."
echo "exists=false" >> $GITHUB_OUTPUT
fi
shell: bash
##################################
# RUN DOCKER IMAGE #
##################################
- name: Pull Docker image
run: docker pull kexa/kexa:latest
shell: bash
- name: Stop and remove Docker container
run: |
containerId=$(docker ps -aqf name=^kexadockeractioncontainer$)
echo "Container ID to delete: $containerId"
if [ -n "$containerId" ]; then
docker stop "$containerId"
docker rm "$containerId"
else
echo "Container not found"
fi
if: always()
continue-on-error: true
shell: bash
- name: Run Docker container
run: |
docker run -d -p 8000:8000 --name kexadockeractioncontainer --env-file .env kexa/kexa:latest sleep infinity
shell: bash
if: ${{ success() }}
#############################################
# COPY CONFIGS INTO CONTAINER #
#############################################
- name: Copy kubeconfig into container
run: |
docker cp kubernetes.yaml kexadockeractioncontainer:/app/
shell: bash
continue-on-error: true
if: ${{ success() }}
- name: Copy multiple kubeconfig into container
run: |
for file in kubernetes_*.yaml; do
docker cp "$file" kexadockeractioncontainer:/app/
done
shell: bash
continue-on-error: true
if: ${{ success() }}
- name: Copy multiple kubernetes json into container
run: |
for file in kubernetes_*.json; do
docker cp "$file" kexadockeractioncontainer:/app/config/
done
shell: bash
continue-on-error: true
if: ${{ success() }}
- name: Copy kube json into container
run: |
docker cp kubernetes.json kexadockeractioncontainer:/app/config/
shell: bash
continue-on-error: true
if: ${{ success() }}
- name: Copy rules into container
run: |
docker cp ./rules kexadockeractioncontainer:/app/
shell: bash
if: ${{ success() }}
- name: Copy config into container
run: |
docker cp ./config kexadockeractioncontainer:/app/
shell: bash
if: ${{ success() }}
#################################
# RUN KEXA SCRIPT #
#################################
- name: Start main application inside container
run: |
docker exec kexadockeractioncontainer sh -c '
cd /app
bun run Kexa/main.ts
EXIT_CODE=$?
echo "Exit code: $EXIT_CODE"
if [ $EXIT_CODE -ne 0 ]; then
echo "bun run Kexa/main.ts failed with exit code $EXIT_CODE"
exit $EXIT_CODE
fi
'
shell: bash
# delete kube config file if created
- name: Delete kubeconfig file
if: steps.check_kubeconfig.outputs.exists == 'true'
run: rm kubernetes.yaml
continue-on-error: true
shell: bash
# delete multiple kube config files if created
- name: Delete multiple kubeconfig files
if: steps.check_kubeconfig_multiple.outputs.exists == 'true'
run: rm kubernetes_*.yaml kubernetes_*.json
continue-on-error: true
shell: bash
- name: Delete .env file
run: rm .env
continue-on-error: true
shell: bash
- name: Stop and remove Docker container
run: |
containerId=$(docker ps -qf name=^kexadockeractioncontainer$)
echo "Container ID to delete: $containerId"
if [ -n "$containerId" ]; then
docker stop "$containerId"
docker rm "$containerId"
else
echo "Container not found"
fi
if: always()
continue-on-error: true
shell: bash