-
Notifications
You must be signed in to change notification settings - Fork 237
329 lines (287 loc) · 11.5 KB
/
pull-request.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
name: Pull request
run-name: "${{ github.event.pull_request.title }} / [${{ github.actor }}] is testing${{ github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name && format(' from fork [{0}]', github.event.pull_request.head.repo.full_name) || format(' from current repo') }} 🚀"
on:
pull_request:
branches:
- main
- master
paths-ignore:
- ".github/**/*.yml"
- "**/*.md"
- "README.md"
permissions:
contents: write
packages: write
security-events: write
pull-requests: write
concurrency:
group: ${{ github.event.pull_request.head.repo.full_name }}-${{ github.head_ref }}/${{ github.ref }}
cancel-in-progress: true
env:
PULL_REQUEST_NUMBER: ${{ github.event.pull_request.number }}
PULL_REQUEST_BRANCH: ${{ github.head_ref }}
PULL_REQUEST_HEAD: ${{ github.event.pull_request.head.sha }}
CONCURRENCY_GROUP: ${{ github.event.pull_request.head.repo.full_name }}-${{ github.head_ref }}/${{ github.ref }}
BRANCH: ${{ github.event.pull_request.head.ref }}
REPOSITORY: ${{ github.event.pull_request.head.repo.full_name }}
REGISTRY: ghcr.io
IMAGE_NAME: "ghcr.io/${{ github.repository }}"
RELEASE: false
FORKED: ${{ github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name }}
jobs:
config:
if: github.triggering_actor != 'dependabot[bot]'
runs-on: ubuntu-latest
outputs:
go-version: ${{ fromJson(steps.config.outputs.config).go-version }}
deploy-pull-request: ${{ fromJson(steps.config.outputs.config).deploy.pull-request }}
deploy-sign-docker-image: ${{ fromJson(steps.config.outputs.config).deploy.sign-docker-image }}
deploy-pre-release-matrix: ${{ steps.pre-release-matrix.outputs.matrix }}
deploy-release-matrix: ${{ steps.release-matrix.outputs.matrix }}
is-forked: ${{ github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name }}
steps:
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ env.PULL_REQUEST_HEAD }}
- name: Read config
id: config
run: echo "config=$(jq -M -c '.' ./.github/workflow-config.json)" >> $GITHUB_OUTPUT
- name: List Pre-release profiles
id: deploy-pr-images-profiles
run: echo "profiles=$(jq -c -M '.deploy."pull-request-images"' .github/workflow-config.json)" >> $GITHUB_OUTPUT
- name: Deploy pre-release matrix
id: pre-release-matrix
run: echo 'matrix={"include":${{ steps.deploy-pr-images-profiles.outputs.profiles }} }' >> $GITHUB_OUTPUT
- name: List release profiles
id: deploy-images-profiles
run: echo "profiles=$(jq -c -M '.deploy."release-images"' .github/workflow-config.json)" >> $GITHUB_OUTPUT
- name: Deploy release matrix
id: release-matrix
run: echo 'matrix={"include":${{ steps.deploy-images-profiles.outputs.profiles }} }' >> $GITHUB_OUTPUT
build:
runs-on: ubuntu-latest
needs:
- config
outputs:
version: ${{ steps.version.outputs.version }}
steps:
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ env.PULL_REQUEST_HEAD }}
- name: Setup Golang
uses: actions/setup-go@v3
with:
go-version: ${{ needs.config.outputs.go-version }}
- name: Get makefile versions
id: version
run: |
version=$(make version)
echo "version=$version" >> $GITHUB_OUTPUT
- name: Setup Golang cache
uses: actions/cache@v3
with:
path: |
~/go/pkg/mod
~/.cache/go-build
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Build artifact
run: make go-build
- name: Run test
run: make go-test
- name: Go lint
continue-on-error: true
uses: golangci/golangci-lint-action@v3
with:
args: --issues-exit-code=0
skip-pkg-cache: true
skip-build-cache: true
- name: Package artifact
run: |
mv dist/*.tar.gz application.tar.gz
- name: Upload build output
uses: actions/upload-artifact@v3
with:
name: application
path: "./application.tar.gz"
- name: Upload test coverage
uses: actions/upload-artifact@v3
with:
name: test-coverage
path: "./test-coverage.out"
release:
runs-on: ubuntu-latest
needs:
- config
- build
outputs:
version: ${{ steps.version.outputs.version }}
steps:
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ env.PULL_REQUEST_HEAD }}
- name: Set alpha version
id: version
run: echo "version=${{ needs.build.outputs.version }}-rc.pr-${{ env.PULL_REQUEST_NUMBER }}-${{ github.run_number }}" >> $GITHUB_OUTPUT
- name: Download artifact
uses: actions/download-artifact@v3
with:
name: application
path: "."
- name: Rename artifact
run: mv application.tar.gz ${{ github.event.repository.name }}.tar.gz
- name: Create release
if: ${{ env.FORKED == 'false' }}
uses: softprops/action-gh-release@v1
id: create-release
with:
name: Pre-release ${{ steps.version.outputs.version }}
tag_name: ${{ steps.version.outputs.version }}
token: ${{ secrets.GITHUB_TOKEN }}
body: ${{ steps.changelog.outputs.changelog }}
files: |
./${{ github.event.repository.name }}.tar.gz
draft: false
prerelease: true
generate_release_notes: true
deploy:
name: deploy-[${{ matrix.name }}]
runs-on: ubuntu-latest
strategy:
max-parallel: 1
matrix: ${{ fromJson(needs.config.outputs.deploy-pre-release-matrix) }}
if: ${{ !failure() && needs.release.result == 'success' && needs.config.outputs.deploy-pull-request == 'true' }}
needs:
- config
- build
- release
steps:
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ env.PULL_REQUEST_HEAD }}
- name: Build image
id: docker-meta
env:
VERSION: "${{ needs.release.outputs.version }}"
run: |
make ${{ matrix.name }}-image
TAG_SUFFIX=$(echo "-${{ matrix.name }}" | sed s/-ubuntu//)
echo "image-id=$IMAGE_NAME" >> $GITHUB_OUTPUT
echo "image-version=${VERSION}${TAG_SUFFIX}" >> $GITHUB_OUTPUT
- name: Log in to registry
if: ${{ env.FORKED == 'false' }}
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ${{ env.REGISTRY }} -u $ --password-stdin
- name: Push image
if: ${{ env.FORKED == 'false' }}
env:
VERSION: "${{ needs.release.outputs.version }}"
run: |
make push-${{ matrix.name }}-image
- name: Format current time
if: ${{ env.FORKED == 'false' }}
id: time_now
run: echo "time_now_formatted=$(date +'%Y-%m-%d %H:%M:%S')" >> "$GITHUB_OUTPUT"
- name: Find releases comment
if: ${{ env.FORKED == 'false' }}
uses: peter-evans/find-comment@v2
id: find_comment
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: "github-actions[bot]"
body-includes: "Following docker image(s) have been created for this PR."
- name: Create releases comment
uses: peter-evans/create-or-update-comment@v2
if: ${{ steps.find_comment.outputs.comment-id == '' && env.FORKED == 'false' }}
with:
comment-id: ${{ steps.find_comment.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
edit-mode: replace
body: |
## Pull Request information
Following docker image(s) have been created for this PR.
| Time | Tag |
| --- | --- |
| ${{ steps.time_now.outputs.time_now_formatted }} | **${{ steps.docker-meta.outputs.image-version }}** |
- name: Append releases comment
uses: peter-evans/create-or-update-comment@v2
if: ${{ steps.find_comment.outputs.comment-id != '' && env.FORKED == 'false' }}
with:
comment-id: ${{ steps.find_comment.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
edit-mode: append
body: |
| ${{ steps.time_now.outputs.time_now_formatted }} | **${{ steps.docker-meta.outputs.image-version }}** |
- name: Setup cosign
if: ${{ needs.config.outputs.deploy-sign-docker-image == 'true' && env.FORKED == 'false' }}
uses: sigstore/cosign-installer@main
- name: Write signing key to disk (only needed for `cosign sign --key`)
if: ${{ needs.config.outputs.deploy-sign-docker-image == 'true' && env.FORKED == 'false' }}
continue-on-error: true
run: echo "${{ secrets.SIGNING_SECRET }}" > cosign.key
- name: Sign the published Docker image
if: ${{ needs.config.outputs.deploy-sign-docker-image == 'true' && env.FORKED == 'false' }}
continue-on-error: true
env:
COSIGN_PASSWORD: ""
VERSION: "${{ needs.release.outputs.version }}"
run: make sign-${{ matrix.name }}-image
- name: Container scan
uses: aquasecurity/trivy-action@0.8.0
env:
image-ref: "${{ steps.docker-meta.outputs.image-id }}:${{ steps.docker-meta.outputs.image-version }}"
with:
image-ref: "${{ env.image-ref }}"
ignore-unfixed: true
vuln-type: "os,library"
severity: "CRITICAL,HIGH"
format: "sarif"
output: "trivy-results.sarif"
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: "trivy-results.sarif"
category: trivy-${{ matrix.name }}
comments:
runs-on: ubuntu-latest
if: ${{ needs.config.outputs.is-forked == 'false' }}
needs:
- config
- release
steps:
- name: Format current time
id: time_now
run: echo "time_now_formatted=$(date +'%Y-%m-%d %H:%M:%S')" >> "$GITHUB_OUTPUT"
- name: Find releases comment
uses: peter-evans/find-comment@v2
id: find_comment
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: "github-actions[bot]"
body-includes: "Following release(s) have been created for this PR."
- name: Create releases comment
uses: peter-evans/create-or-update-comment@v2
if: ${{ steps.find_comment.outputs.comment-id == '' }}
with:
comment-id: ${{ steps.find_comment.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
edit-mode: replace
body: |
## Pull Request information
Following release(s) have been created for this PR.
| Time | Release |
| --- | --- |
| ${{ steps.time_now.outputs.time_now_formatted }} | **${{ needs.release.outputs.version }}** |
- name: Append releases comment
uses: peter-evans/create-or-update-comment@v2
if: ${{ steps.find_comment.outputs.comment-id != '' }}
with:
comment-id: ${{ steps.find_comment.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
edit-mode: append
body: |
| ${{ steps.time_now.outputs.time_now_formatted }} | **${{ needs.release.outputs.version }}** |