-
Notifications
You must be signed in to change notification settings - Fork 1
175 lines (157 loc) · 5.99 KB
/
cicd.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
name: cicd
on:
push:
branches: [ '*' ]
tags: [ 'v*' ]
pull_request:
permissions:
contents: read
pull-requests: read
env:
# renovate: dev-tag
DEV_TAG: '19'
jobs:
envs:
runs-on: ubuntu-22.04
steps:
- run: echo "This is needed because sometimes env is not accessible, whereas the output of this step is"
outputs:
DEV_TAG: ${{ env.DEV_TAG }}
detect-changes:
runs-on: ubuntu-22.04
outputs:
services: ${{ steps.aggregate-changes.outputs.services }}
libs: ${{ steps.aggregate-changes.outputs.libs }}
images: ${{ steps.aggregate-changes.outputs.images }}
commit_hash: ${{ steps.commit.outputs.short }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Detect changes in libs
id: changes-in-libs
uses: tj-actions/changed-files@v44
with:
files: |
.github/**
libs/**
.dockerignore
Dockerfile.service
- name: Detect changes in services
id: changes-in-services
uses: tj-actions/changed-files@v44
with:
files: |
services/*-svc/**
- name: Detect changes in images
id: changes-in-images
uses: tj-actions/changed-files@v44
with:
files: |
images/**
- name: Aggregate changes
id: aggregate-changes
run: |
echo "images=$(echo $changesInImages | awk -F / '$1 ~ /images/ {print $2}' | uniq | grep -v dev-go-custom | jq -R . | jq -sc .)" >> $GITHUB_OUTPUT
if [[ "$libsChanged" == "true" ]]; then
echo "services=$(ls services | uniq | jq -R . | jq -sc .)" >> $GITHUB_OUTPUT
echo "libs=$(ls libs | uniq | jq -R . | jq -sc .)" >> $GITHUB_OUTPUT
echo "images=$(ls images | grep -v dev-go-custom | uniq | jq -R . | jq -sc .)" >> $GITHUB_OUTPUT
else
echo "services=$(echo $changesInServices | awk -F / '$1 ~ /services/ {print $2}' | uniq | jq -R . | jq -sc .)" >> $GITHUB_OUTPUT
fi
env:
libsChanged: ${{ steps.changes-in-libs.outputs.any_changed }}
changesInServices: ${{ steps.changes-in-services.outputs.all_changed_files }}
changesInImages: ${{ steps.changes-in-images.outputs.all_changed_files }}
- name: Get commit hash
id: commit
uses: pr-mpt/actions-commit-hash@v3
with:
commit: "${{ github.sha }}"
- name: Summarize
run: |
echo "services: $services" >> $GITHUB_STEP_SUMMARY
echo "images: $images" >> $GITHUB_STEP_SUMMARY
env:
services: ${{ steps.aggregate-changes.outputs.services }}
images: ${{ steps.aggregate-changes.outputs.images }}
cicd-go:
needs: detect-changes
if: ${{ needs.detect-changes.outputs.services != '[]' && needs.detect-changes.outputs.services != '' }}
strategy:
matrix:
service: ${{ fromJson(needs.detect-changes.outputs.services) }}
permissions:
contents: read
pull-requests: read
packages: write
uses: ./.github/workflows/cicd-go.yaml
with:
deploy: ${{ github.event_name != 'pull_request' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')) }}
service: ${{ matrix.service }}
secrets: inherit
ci-go-libs:
needs: detect-changes
if: ${{ needs.detect-changes.outputs.libs != '[]' && needs.detect-changes.outputs.libs != '' }}
strategy:
matrix:
lib: ${{ fromJson(needs.detect-changes.outputs.libs) }}
uses: ./.github/workflows/ci-go-libs.yaml
with:
lib: ${{ matrix.lib }}
secrets: inherit
cicd-images:
needs:
- detect-changes
- envs
if: ${{ needs.detect-changes.outputs.images != '[]' && needs.detect-changes.outputs.images != '' }}
strategy:
matrix:
image: ${{ fromJson(needs.detect-changes.outputs.images) }}
permissions:
contents: read
pull-requests: read
packages: write
uses: ./.github/workflows/cicd-images.yaml
with:
deploy: ${{ github.event_name != 'pull_request' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')) }}
image: ${{ matrix.image }}
custom_tag: ${{ matrix.image == 'dev-go' && needs.envs.outputs.DEV_TAG || '' }}
secrets: inherit
fly-deploy-staging:
needs: detect-changes
if: ${{ github.event_name != 'pull_request' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')) && needs.detect-changes.outputs.services != '[]' && needs.detect-changes.outputs.services != '' }}
strategy:
fail-fast: false
max-parallel: 2
matrix:
service: ${{ fromJson(needs.detect-changes.outputs.services) }}
uses: ./.github/workflows/fly-deploy.yaml
with:
service: ${{ matrix.service }}
app: ${{ vars.FLY_ORGANIZATION_STAGING }}-${{ matrix.service }}
organization: ${{ vars.FLY_ORGANIZATION_STAGING }}
version: ${{ needs.detect-changes.outputs.commit_hash }}
environment: "staging"
secrets: inherit
fly-deploy-prod:
needs: detect-changes
# TODO
# For now, it's ok to deploy if ANY version tag is pushed to (we assume it's the newest version and needs deployment)
# In the future we need to be able to deploy to different, API incompatible, production environments
# Maybe even automate resource creation
if: ${{ github.event_name != 'pull_request' && startsWith(github.ref, 'refs/tags/v') && needs.detect-changes.outputs.services != '[]' && needs.detect-changes.outputs.services != '' }}
strategy:
fail-fast: false
max-parallel: 2
matrix:
service: ${{ fromJson(needs.detect-changes.outputs.services) }}
uses: ./.github/workflows/fly-deploy.yaml
with:
service: ${{ matrix.service }}
app: ${{ vars.FLY_ORGANIZATION_PROD }}-${{ matrix.service }}
organization: ${{ vars.FLY_ORGANIZATION_PROD }}
version: ${{ needs.detect-changes.outputs.commit_hash }}
environment: "production"
secrets: inherit