forked from filipeforattini/ff-iac-github-actions
-
Notifications
You must be signed in to change notification settings - Fork 0
418 lines (346 loc) · 14 KB
/
app.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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
name: app
concurrency:
group: ${{github.workflow}}-${{github.event_name}}
#--------------------------------------------------#
# Triggers #
#--------------------------------------------------#
on:
workflow_call:
inputs:
mainBranch:
type: string
required: false
default: master
description: "Main repository branch may interfere with versioning"
ecosystem:
type: string
required: false
description: "Special prefix that will be added to the image name"
containerRegistry:
type: string
required: false
default: ghcr.io
description: "Container registry to upload container images"
environmentsAsNamespaces:
type: boolean
required: false
default: false
description: "Separate environments as namespaces"
staticAnalysis:
type: boolean
required: false
default: false
description: "Enable static analysis scans"
autoVersioning:
type: boolean
required: false
default: true
description: "Enable auto versioning with semantic versioning"
platforms:
type: string
required: false
default: "linux/386,linux/amd64,linux/arm/v7,linux/arm/v8,linux/arm64,linux/ppc64le,linux/s390x"
description: "Multi-plataform container builds"
buildDirectory:
type: string
required: false
default: "dist"
description: "Output directory from build"
#--------------------------------------------------#
# Workflow Jobs #
#--------------------------------------------------#
jobs:
#--------------------------------------------------#
# Setup Jobs #
#--------------------------------------------------#
Setup:
runs-on: ubuntu-latest
outputs:
Event: ${{steps.analysis.outputs.event}}
Analysis: ${{steps.analysis.outputs.result}}
Environment: ${{steps.analysis.outputs.environment}}
steps:
# pre-job
- name: Setup | Cloning repository
uses: actions/checkout@v3
# job
- name: Pipeline config scrapper
id: analysis
uses: filipeforattini/ff-iac-github-actions/.github/actions/config-scrapper@main
env:
PIPESECRET_KUBE_CONFIG: ${{ secrets.KUBE_CONFIG }}
PIPESECRET_REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
PIPESECRET_REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
PIPESECRET_PIPELINE_DEPLOY_TOKEN: ${{ secrets.PIPELINE_DEPLOY_TOKEN }}
with:
writeSummary: true
evaluateSecrets: true
containerRegistry: ${{inputs.containerRegistry}}
#--------------------------------------------------#
# Static Analysis #
#--------------------------------------------------#
Static-Analysis:
runs-on: ubuntu-latest
if: needs.Setup.outputs.Event == 'push'
needs: Setup
permissions:
security-events: write
steps:
# pre-job
- name: Setup | Cloning repository
uses: actions/checkout@v3
if: inputs.staticAnalysis
with:
token: "${{secrets.GITHUB_TOKEN}}"
fetch-depth: '0'
persist-credentials: false
#job
- name: Run code analysis
uses: filipeforattini/ff-iac-github-actions/.github/actions/pack-static-code-analysis@main
if: inputs.staticAnalysis
with:
token: "${{secrets.PIPELINE_DEPLOY_TOKEN}}"
language: ${{needs.Setup.outputs.Language}}
#--------------------------------------------------#
# Release #
#--------------------------------------------------#
Release:
runs-on: ubuntu-latest
if: needs.Setup.outputs.Event == 'push'
needs:
- Static-Analysis
outputs:
Version: ${{ steps.versioning.outputs.version }}
steps:
# pre-job
- name: Setup | Cloning repository
uses: actions/checkout@v3
with:
fetch-depth: 0
# persist-credentials: false
- name: Setup | Pipeline config scrapper
uses: filipeforattini/ff-iac-github-actions/.github/actions/config-scrapper@main
id: analysis
# release
- name: Semantic Release
uses: filipeforattini/ff-iac-github-actions/.github/actions/pack-semantic-release@main
if: inputs.autoVersioning
with:
writeSummary: true
token: ${{secrets.PIPELINE_DEPLOY_TOKEN}}
installNode: true
npmPlugin: true
defaultBranch: ${{ inputs.mainBranch }}
author: ${{steps.analysis.outputs.commiter_name}}
email: ${{steps.analysis.outputs.commiter_email}}
files: '["package.json","dist/**/*.{js,css}"]'
- name: Current Version
id: versioning
run: |
echo "version=$(cat package.json | jq -r '.version')" >> $GITHUB_OUTPUT
echo -e "\n\nINFO current version = $(cat package.json | jq -r '.version')\n\n"
#--------------------------------------------------#
# Trigger #
#--------------------------------------------------#
Trigger-Build:
runs-on: ubuntu-latest
if: github.event_name == 'push'
needs:
- Release
steps:
# pre-job
- name: Setup | Cloning repository
uses: actions/checkout@v3
with:
token: ${{ secrets.PIPELINE_DEPLOY_TOKEN }}
- name: Pipeline config scrapper
id: analysis
uses: filipeforattini/ff-iac-github-actions/.github/actions/config-scrapper@main
- name: Trigger
run: |
git config --global user.name "${{steps.analysis.outputs.commiter_name}}"
git config --global user.email "${{steps.analysis.outputs.commiter_email}}"
git fetch
git checkout -B env/dev
git commit --allow-empty -am "deploy: to dev [skip ci]\\nVersion: ${{needs.Release.outputs.Version}}\nCommit: ${{steps.setup.outputs.commit_hash}}"
git push -f -u origin env/dev
- uses: altinukshini/deployment-action@releases/v1
name: Create GitHub deployment
id: deployment
with:
token: "${{ secrets.PIPELINE_DEPLOY_TOKEN }}"
environment: dev
#--------------------------------------------------#
# Trigger Manual #
#--------------------------------------------------#
Trigger-Manual:
runs-on: ubuntu-latest
if: github.event_name == 'workflow_dispatch'
needs:
- Setup
steps:
# pre-job
- name: Setup | Cloning repository
uses: actions/checkout@v3
with:
token: ${{ secrets.PIPELINE_DEPLOY_TOKEN }}
- name: Pipeline config scrapper
id: analysis
uses: filipeforattini/ff-iac-github-actions/.github/actions/config-scrapper@main
- name: Trigger
run: |
git config --global user.name "pipeline-bot"
git config --global user.email "pipeline-bot"
git fetch
git checkout -B env/${{steps.analysis.outputs.environment}}
git commit --allow-empty -am "deploy: to ${{steps.analysis.outputs.environment}} [skip ci]\nCommit: ${{steps.setup.outputs.commit_hash}}"
git push -f -u origin env/${{steps.analysis.outputs.environment}}
- uses: altinukshini/deployment-action@releases/v1
name: Create GitHub deployment
id: deployment
with:
token: "${{ secrets.PIPELINE_DEPLOY_TOKEN }}"
environment: ${{steps.analysis.outputs.environment}}
#--------------------------------------------------#
# Build #
#--------------------------------------------------#
Build:
runs-on: ubuntu-latest
if: github.event_name == 'deployment'
needs:
- Setup
steps:
# pre-job
- name: Setup | Cloning tools
uses: actions/checkout@v3
with:
ref: env/${{needs.Setup.outputs.Environment}}
- name: Pipeline config scrapper
id: analysis
uses: filipeforattini/ff-iac-github-actions/.github/actions/config-scrapper@main
with:
containerRegistry: ${{inputs.containerRegistry}}
- name: Setup | Dependencies
uses: filipeforattini/ff-iac-github-actions/.github/actions/setup-binaries@main
with:
docker-qmeu: true
docker-buildx: true
# pre-build
- name: Docs | Write summary title
run: |
echo "### 🚢 Build" >> $GITHUB_STEP_SUMMARY
- name: Create Dockerfile
uses: filipeforattini/ff-iac-github-actions/.github/actions/create-dockerfile@main
if: steps.analysis.outputs.feature_has_dockerfile == 'false'
with:
writeSummary: true
preset: spa
- name: Create .dockerignore
uses: filipeforattini/ff-iac-github-actions/.github/actions/create-dockerignore@main
if: steps.analysis.outputs.feature_has_dockerignore == 'false'
with:
globsToIgnore: '["node_modules"]'
# login
- name: Config | Login to Container Registry
uses: docker/login-action@v2
with:
logout: false
registry: ${{ inputs.containerRegistry }}
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
# build
- name: Copy config
run: |
if test -f ./manifests/configs/${{needs.Setup.outputs.Environment}}.env; then cp ./manifests/configs/${{needs.Setup.outputs.Environment}}.env ./.env; fi
- name: Build container image and push
uses: filipeforattini/ff-iac-github-actions/.github/actions/pack-docker-build@main
with:
cached: true
platforms: ${{inputs.platforms}}
containerRegistry: ${{inputs.containerRegistry}}
#--------------------------------------------------#
# Deploy #
#--------------------------------------------------#
Deploy:
runs-on: ubuntu-latest
environment: ${{needs.Setup.outputs.Environment}}
needs:
- Setup
- Build
steps:
# pre-job
- name: Setup | Cloning repository
uses: actions/checkout@v3
with:
fetch-depth: 0
# persist-credentials: false
- name: Setup | Cloning tools
uses: actions/checkout@v3
with:
ref: main
path: .pipeline
repository: filipeforattini/ff-iac-github-actions
- name: Setup | Dependencies
uses: filipeforattini/ff-iac-github-actions/.github/actions/setup-binaries@main
with:
token: ${{ secrets.GITHUB_TOKEN }}
docker-qmeu: true
docker-buildx: true
kubectl: true
ytt: true
yq: true
- name: Pipeline config scrapper
id: analysis
uses: filipeforattini/ff-iac-github-actions/.github/actions/config-scrapper@main
with:
containerRegistry: ${{inputs.containerRegistry}}
- name: Config | Version
id: versioning
run: |
NEXT_VERSION=$(git for-each-ref --sort=authordate --format '%(refname)' refs/tags | tail -n 1 | sed -n -e 's/^.*refs\/tags\/v//p')
echo "version=$NEXT_VERSION" >> $GITHUB_OUTPUT
echo "next version = $NEXT_VERSION"
- name: Config | Kubectl config file
env:
KUBE_CONFIG: ${{ secrets.KUBE_CONFIG }}
run: |
mkdir -p ~/.kube
echo "$KUBE_CONFIG" | base64 -d > ~/.kube/config
# namespace
- name: K8s create namespace
run: kubectl create namespace ${{steps.analysis.outputs.deploy_namespace}} --dry-run=client --validate=false --output=yaml | kubectl apply -f -
# secrets for registry auth
- name: Config | Login to Container Registry
uses: docker/login-action@v2
with:
logout: false
registry: ${{ inputs.containerRegistry }}
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
- name: Config | Gives runner access to docker config file
run: |
sudo chown $(whoami):docker /home/$(whoami)/.docker/config.json
cp /home/$(whoami)/.docker/config.json ./manifests/docker-config.json
- name: K8s create registry-token secret
run: kubectl create secret generic -n ${{steps.analysis.outputs.deploy_namespace}} registry-token --type=kubernetes.io/dockerconfigjson --from-file=.dockerconfigjson=./manifests/docker-config.json --dry-run=client --validate=false --output=yaml | kubectl apply -f -
# generate k8s manifests
- name: K8s generates final yml
run: |
ytt \
-f ./.pipeline/deploy/as-k8s/service.schema.yml \
-f ./.pipeline/deploy/as-k8s/service \
-f ./manifests/k8s.yml \
--data-value ecosystem=${{steps.analysis.outputs.ecosystem}} \
--data-value organization=${{steps.analysis.outputs.organization}} \
--data-value repository=${{steps.analysis.outputs.repository}} \
--data-value environment=${{steps.analysis.outputs.environment}} \
--data-value containerRegistry=${{inputs.containerRegistry}} \
--data-value tag=${{steps.analysis.outputs.deploy_tag}} \
--data-value-yaml deployment.imagePullSecrets=true \
--data-value pipelineControl.datetime=${{steps.analysis.outputs.run_started_at}} \
--data-value-yaml pipelineControl.environmentsAsNamespaces=${{inputs.environmentsAsNamespaces}} \
> ./manifests/k8s-to-apply.yml
- name: K8s apply yml
run: |
kubectl apply -f ./manifests/k8s-to-apply.yml
kubectl get pods -o wide -n ${{steps.analysis.outputs.deploy_namespace}}