-
Notifications
You must be signed in to change notification settings - Fork 0
296 lines (289 loc) · 10.7 KB
/
golang.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
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
# Copyright 2024 Zaphiro Technologies
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: Golang Lint & Test
concurrency:
group: ${{github.repository}}-${{ github.ref_name }}-go
cancel-in-progress: true
# Permissions needed
# permissions:
# # Give the default GITHUB_TOKEN write permission to commit and push the
# # updated CHANGELOG back to the repository.
# # https://github.blog/changelog/2023-02-02-github-actions-updating-the-default-github_token-permissions-to-read-only/
# contents: write
on:
workflow_call:
inputs:
alert-threshold-benchmark:
required: false
default: 150%
type: string
description: Percent that will trigger benchmark alert
skip-benchmark:
required: false
default: false
type: boolean
description: True to skip benchmark exec
code-scan:
required: false
default: true
type: boolean
description: False to skip code-scan exec
vulterability-scan:
required: false
default: true
type: boolean
description: False to skip vulterability-scan exec
vulterability-scan-skip-dirs:
required: false
default: 'vendor,.github'
type: string
description: Comma separated list of directories where vulterability check is skipped
jobs:
changes:
runs-on: ubuntu-latest
outputs:
files_changed: ${{ steps.filter.outputs.go == 'true' || steps.filter.outputs.scripts == 'true' }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2 # To retrieve the preceding commit.
- uses: dorny/paths-filter@v3
id: filter
with:
list-files: shell
filters: |
scripts:
- '.golangci.yaml'
- 'Makefile'
- '.github/workflows/golang.*'
go:
- '**/*.go'
- 'go.mod'
- 'go.sum'
lint:
needs: changes
runs-on: ubuntu-latest
permissions:
contents: write
if: ${{ needs.changes.outputs.files_changed == 'true' && github.event.pull_request }}
env:
GOPRIVATE: github.com/zaphiro-technologies/*
GH_ACCESS_TOKEN: ${{ secrets.REPO_PRIVATE_READ_PAT }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{github.event.pull_request.head.ref}} # Can't commit on detached PR merge commit, so this checkouts the branch
token: ${{ secrets.REPO_PAT }}
- run: git config --global url.https://[email protected]/.insteadOf https://github.com/
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Golines
run: |
go install github.com/segmentio/golines@latest
golines . -w
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
args: --fix
version: latest
- name: Commit golines & golangci-lint changes
run: |
git config --global user.name 'Bot'
git config --global user.email '[email protected]'
git commit -am "Automated lint fixes [dependabot skip]" || echo "No changes to commit"
git push
test:
needs: changes
runs-on: ubuntu-latest
env:
GOPRIVATE: github.com/zaphiro-technologies/*
GH_ACCESS_TOKEN: ${{ secrets.REPO_PRIVATE_READ_PAT }}
if: ${{ needs.changes.outputs.files_changed == 'true' }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- run: git config --global url.https://[email protected]/.insteadOf https://github.com/
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Install gcov2lcov
run: |
go install github.com/jandelgado/gcov2lcov@latest
# We Login in GHCR to retrieve owned private images used in docker-compose
# This requires to set in the package settings read permission for the
# repository where the golang ci is running
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Test
run: |
make ci-test
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
if: ${{ inputs.code-scan }}
# we log in the registry to code check also images
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: SonarCloud Code Scan
if: ${{ inputs.code-scan }}
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
- name: Download code coverage report from base branch
uses: dawidd6/action-download-artifact@v6
if: github.actor != 'dependabot[bot]'
with:
name: cov-result
github_token: ${{ secrets.REPO_PRIVATE_READ_PAT }}
branch: ${{github.base_ref}}
if_no_artifact_found: warn
- name: Generate Code Coverage report
id: code-coverage
uses: barecheck/code-coverage-action@v1
if: github.actor != 'dependabot[bot]'
with:
barecheck-github-app-token: ${{ secrets.BARECHECK_GITHUB_APP_TOKEN }}
lcov-file: "./coverage/lcov.info"
base-lcov-file: ${{ hashFiles('./lcov.info') != '' && './lcov.info' || null }}
minimum-ratio: 70
send-summary-comment: true
show-annotations: ""
- name: Upload code coverage for ref branch
if: ${{ ! github.event.pull_request }}
uses: actions/upload-artifact@v4
with:
name: cov-result
path: ./coverage/lcov.info
retention-days: 400
benchmark:
needs: changes
runs-on: ubuntu-latest
if: ${{ needs.changes.outputs.files_changed == 'true' && ! inputs.skip-benchmark}}
env:
GOPRIVATE: github.com/zaphiro-technologies/*
GH_ACCESS_TOKEN: ${{ secrets.REPO_PRIVATE_READ_PAT }}
steps:
- uses: actions/checkout@v4
- run: git config --global url.https://[email protected]/.insteadOf https://github.com/
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
# We Login in GHCR to retrieve owned private images used in docker-compose
# This requires to set in the package settings read permission for the
# repository where the golang ci is running
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Run benchmark
shell: bash
run: make ci-bench | tee output.txt
# Download previous benchmark result from cache (if exists)
- name: Download benchmark report from base branch
uses: dawidd6/action-download-artifact@v6
with:
name: benchmark-result
github_token: ${{ secrets.REPO_PRIVATE_READ_PAT }}
branch: ${{ github.base_ref || github.ref_name }}
if_no_artifact_found: warn
# Run `github-action-benchmark` action
- name: Store benchmark result
uses: benchmark-action/github-action-benchmark@v1
with:
# What benchmark tool the output.txt came from
tool: "go"
# Where the output from the benchmark tool is stored
output-file-path: output.txt
# Where the previous data file is stored
external-data-json-path: benchmark-data.json
# Workflow will fail when an alert happens
fail-on-alert: true
# Enable alert commit comment
comment-on-alert: true
github-token: ${{ secrets.GITHUB_TOKEN }}
alert-threshold: ${{ inputs.alert-threshold-benchmark }}
summary-always: true
- name: Upload code coverage for ref branch
if: ${{ ! github.event.pull_request }}
uses: actions/upload-artifact@v4
with:
name: benchmark-result
path: benchmark-data.json
retention-days: 400
vulnerability:
needs: changes
if: ${{ inputs.vulterability-scan}}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
## To avoid the trivy-db becoming outdated, we save the cache for one day
- name: Get data
id: date
run: echo "date=$(date +%Y-%m-%d)" >> $GITHUB_OUTPUT
- name: Restore trivy cache
uses: actions/cache@v4
with:
path: cache/db
key: trivy-cache-${{ steps.date.outputs.date }}
restore-keys:
trivy-cache-
- name: Run Trivy vulnerability scanner (for summary)
uses: aquasecurity/trivy-action@master
with:
scan-type: fs
scan-ref: .
skip-dirs: ${{ inputs.vulterability-scan-skip-dirs }}
scanners: "vuln,secret,misconfig"
hide-progress: true
output: trivy.txt
cache-dir: "./cache"
- name: Publish Trivy Output to Summary
run: |
if [[ -s trivy.txt ]]; then
{
echo "### Trivy Security Output"
echo ""
echo '```text'
cat trivy.txt
echo '```'
} >> $GITHUB_STEP_SUMMARY
fi
# this comes as last or scan results won't be uploaded
- name: Run Trivy vulnerability scanner (security treshold)
uses: aquasecurity/trivy-action@master
with:
scan-type: fs
scan-ref: .
skip-dirs: ${{ inputs.vulterability-scan-skip-dirs }}
scanners: "vuln,secret,misconfig"
exit-code: '1'
severity: MEDIUM,HIGH,CRITICAL
cache-dir: "./cache"
## Trivy-db uses `0600` permissions.
## But `action/cache` use `runner` user by default
## So we need to change the permissions before caching the database.
- name: change permissions for trivy.db
run: sudo chmod 0644 ./cache/db/trivy.db