-
Notifications
You must be signed in to change notification settings - Fork 6
254 lines (231 loc) · 8.26 KB
/
build.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
name: Build and publish Docker images to GCR
on:
push:
tags:
- "v[0-9]+.[0-9]+.[0-9]+"
branches:
- main
- production
pull_request:
jobs:
golangci:
name: Run golangci-lint
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
# Required: setup-go, for all versions v3.0.0+ of golangci-lint
- uses: actions/setup-go@v3
with:
go-version: 1.18
check-latest: true
- uses: actions/checkout@v3
- uses: technote-space/[email protected]
with:
PATTERNS: |
**/**.go
go.mod
go.sum
- uses: golangci/[email protected]
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: latest
args: --timeout 10m
github-token: ${{ secrets.github_token }}
# Check only if there are differences in the source code
if: env.GIT_DIFF
test-unit:
needs: golangci
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v4
with:
go-version: "1.19"
check-latest: true
- uses: actions/checkout@v3
- uses: technote-space/[email protected]
with:
PATTERNS: |
**/**.sol
**/**.go
go.mod
go.sum
- name: Test
run: |
make test
if: env.GIT_DIFF
set-environment:
runs-on: ubuntu-latest
needs: [golangci, test-unit]
outputs:
env-variable: ${{ steps.set-env-var.outputs.patch_env }}
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0 # Note: This fetches all branches and tags
- name: Check base ref
run: |
BASE_REF=$(git describe --contains --all HEAD)
echo "BASE_REF=$BASE_REF" >> $GITHUB_ENV
- name: Set ENV variable
id: set-env-var
run: |
PATCH_ENV="unknown" # Default value
case $GITHUB_REF in
refs/tags/*)
BRANCH=$(git branch --contains $GITHUB_SHA | sed 's/ *origin\///' | grep -v "HEAD" | grep main)
if [ "$BRANCH" == "main" ]; then
PATCH_ENV="production"
fi
;;
refs/heads/main)
PATCH_ENV="non-production"
;;
esac
echo "PATCH_ENV=$PATCH_ENV" >> $GITHUB_ENV
echo "BRANCH=$BRANCH" >> $GITHUB_ENV
echo "::set-output name=patch_env::$PATCH_ENV"
build:
if: (github.event_name == 'push') && (needs.set-environment.outputs.env-variable == 'non-production')
needs: [set-environment]
permissions:
contents: read
id-token: write
packages: write
runs-on: ubuntu-latest
strategy:
matrix:
component:
- name: "api"
path: "."
dockerfile: "dockerfile"
image_name: "dashboard-backend_api"
- name: "cron"
path: "./cronjobs"
dockerfile: "dockerfile"
image_name: "dashboard-backend_cron"
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Docker metadata
id: metadata
uses: docker/[email protected]
with:
images: |
ghcr.io/${{ github.repository }}/${{ matrix.component.name }}
tags: |
type=semver,pattern={{version}}
type=raw,value={{sha}},enable=${{ github.ref_type != 'tag' }}
flavor: |
latest=${{ github.ref == 'refs/heads/main' }}
# Login to GitHub Container Registry (GHCR)
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push
uses: docker/[email protected]
with:
push: true
tags: ${{ steps.metadata.outputs.tags }}
context: ${{ matrix.component.path }}
file: ${{ matrix.component.path }}/${{ matrix.component.dockerfile }}
platforms: linux/amd64
- name: Write image tag to file
run: |
echo "${{ matrix.component.name }} ${{ steps.metadata.outputs.tags }}" >> metadata-${{ matrix.component.name }}.txt
- name: Upload image tags
uses: actions/upload-artifact@v2
with:
name: image-tag-${{ matrix.component.name }}
path: metadata-${{ matrix.component.name }}.txt
- name: Prune old images on ghcr.io
uses: vlaurin/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}
organization: ${{ github.repository_owner }}
container: backend/${{ matrix.component.name }}
dry-run: false
keep-younger-than: 14 # days
keep-last: 5
prune-untagged: true
prune-tags-regexes: ^[a-zA-Z0-9-\.]+$
retag-and-push:
if: needs.set-environment.outputs.env-variable == 'production'
needs: [set-environment]
runs-on: ubuntu-latest
strategy:
matrix:
component:
- name: "api"
path: "."
dockerfile: "Dockerfile"
image_name: "dashboard-backend_api"
- name: "cron"
path: "./cronjobs"
dockerfile: "Dockerfile"
image_name: "dashboard-backend_cron"
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
# Login to GitHub Container Registry (GHCR)
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Re-tag image
run: |
COMMIT_HASH=$(git rev-parse --short "$GITHUB_REF_NAME")
docker pull ghcr.io/${{ github.repository }}/${{ matrix.component.name }}:${COMMIT_HASH}
docker tag ghcr.io/${{ github.repository }}/${{ matrix.component.name }}:${COMMIT_HASH} ghcr.io/${{ github.repository }}/${{ matrix.component.name }}:${GITHUB_REF_NAME}
docker push ghcr.io/${{ github.repository }}/${{ matrix.component.name }}:${GITHUB_REF_NAME}
echo "${{ matrix.component.name }} ghcr.io/${{ github.repository }}/${{ matrix.component.name }}:$GITHUB_REF_NAME" > metadata-${{ matrix.component.name }}.txt
- name: Upload image tags
uses: actions/upload-artifact@v2
with:
name: image-tag-${{ matrix.component.name }}
path: metadata-${{ matrix.component.name }}.txt
update-deployment-tags:
if: always() && needs.set-environment.outputs.env-variable != 'unknown'
needs: [set-environment,build,retag-and-push]
runs-on: ubuntu-latest
environment:
name: ${{ needs.set-environment.outputs.env-variable }}
steps:
- name: Use environment
run: |
echo "Deploying to environment ${{ needs.set-environment.outputs.env-variable }}"
echo "K8S_MANIFEST: ${{ vars.K8S_MANIFEST }}"
- name: Download all artifacts
uses: actions/download-artifact@v2
with:
path: build-artifacts
- name: Clone argo-apps repo
run: |
git clone https://x-access-token:${ARGOCD_APPS_TOKEN}@${ARGOCD_APPS_REPO} APPS
env:
ARGOCD_APPS_TOKEN: ${{ secrets.ARGOCD_APPS_TOKEN }}
ARGOCD_APPS_REPO: ${{ secrets.ARGOCD_APPS_REPO }}
- name: Update k8s deployment
run: |
find build-artifacts -name "*.txt" | while read filename; do
while read line; do
COMPONENT=$(echo $line | cut -d' ' -f1)
NEW_IMAGE=$(echo $line | cut -d' ' -f2-)
sed -i "s|ghcr.io/${{ github.repository }}/${COMPONENT}:[^ ]*|${NEW_IMAGE}|" APPS/${{ vars.K8S_MANIFEST }}
done < $filename
done
- name: Commit and push if it's necessary
run: |
cd APPS
git diff
git config --global user.email "[email protected]"
git config --global user.name "GitHub Action"
git commit -am "Update tags (pipeline #${{ github.run_number }})" || echo "No changes to commit"
git push