-
Notifications
You must be signed in to change notification settings - Fork 17
250 lines (221 loc) · 9.74 KB
/
scan_vulns_deps.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
name: scan-vulns-deps
on:
workflow_dispatch:
push:
branches:
- '**'
jobs:
build-and-deploy:
runs-on: ubuntu-latest
outputs:
BRANCH_NAME: ${{ steps.myvars.outputs.BRANCH_NAME }}
GIT_HASH_SHORT: ${{ steps.myvars.outputs.GIT_HASH_SHORT }}
DATE_IN_SECS: ${{ steps.myvars.outputs.DATE_IN_SECS }}
CONTAINER_TAG: ${{ steps.myvars.outputs.CONTAINER_TAG }}
SHORT_ENV_OUT: ${{ steps.myvars.outputs.SHORT_ENV_OUT }}
CONTAINER_NAME: ${{ steps.myvars.outputs.CONTAINER_NAME }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set myvars
id: myvars
run: |
branchname=$(echo ${GITHUB_REF#refs/heads/} | tr '/' '-' )
dateinsecs=$(date +%s)
githashshort=$(git rev-parse --short HEAD)
echo "BRANCH_NAME=$branchname" >> $GITHUB_OUTPUT
echo "GIT_HASH_SHORT=$githashshort" >> $GITHUB_OUTPUT
echo "DATE_IN_SECS=$dateinsecs" >> $GITHUB_OUTPUT
if [ "$branchname" = "develop" ]; then
echo "CURRENT_ENVIRONMENT=development" >> $GITHUB_OUTPUT
echo "SHORT_ENV_OUT=DEV" >> $GITHUB_OUTPUT
containertag="commit-race-$githashshort"
elif [ "$branchname" = "main" ]; then
echo "CURRENT_ENVIRONMENT=production" >> $GITHUB_OUTPUT
echo "SHORT_ENV_OUT=PROD" >> $GITHUB_OUTPUT
containertag="commit-race-$githashshort"
else
echo "BRANCH_NAME=test" >> $GITHUB_OUTPUT
echo "CURRENT_ENVIRONMENT=testing" >> $GITHUB_OUTPUT
echo "SHORT_ENV_OUT=TEST" >> $GITHUB_OUTPUT
containertag="commit-$githashshort"
fi
echo "CONTAINER_NAME=vocdoni-node" >> $GITHUB_OUTPUT
echo "CONTAINER_TAG=$containertag" >> $GITHUB_OUTPUT
- name: Set up Go environment
uses: actions/setup-go@v5
with:
go-version: '1.22'
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Push Docker image to ghcr.io
uses: docker/build-push-action@v5
with:
context: .
#push: true
push: false
tags: |
ghcr.io/vocdoni/go-dvote:latest,
ghcr.io/vocdoni/go-dvote:${{ steps.myvars.outputs.BRANCH_NAME }},
ghcr.io/vocdoni/go-dvote:commit-${{ steps.myvars.outputs.GIT_HASH_SHORT }},
ghcr.io/vocdoni/go-dvote:${{ steps.myvars.outputs.BRANCH_NAME }}-${{ steps.myvars.outputs.DATE_IN_SECS }}
cache-from: type=gha
cache-to: type=gha,mode=max
#outputs: type=tar,dest=${{ steps.myvars.outputs.CONTAINER_NAME }}-${{ steps.myvars.outputs.CONTAINER_TAG }}-tar
outputs: type=docker,dest=${{ steps.myvars.outputs.CONTAINER_NAME }}-${{ steps.myvars.outputs.CONTAINER_TAG }}-oci-tar
- name: Push Docker image to ghcr.io (race enabled)
uses: docker/build-push-action@v5
if: github.ref == 'refs/heads/dev' || github.ref == 'refs/heads/main'
with:
context: .
#push: true
push: false
build-args: |
BUILDARGS=-race
tags: |
ghcr.io/vocdoni/go-dvote:latest-race,
ghcr.io/vocdoni/go-dvote:${{ steps.myvars.outputs.BRANCH_NAME }}-race,
ghcr.io/vocdoni/go-dvote:commit-${{ steps.myvars.outputs.GIT_HASH_SHORT }},
ghcr.io/vocdoni/go-dvote:${{ steps.myvars.outputs.BRANCH_NAME }}-race-${{ steps.myvars.outputs.DATE_IN_SECS }}
cache-from: type=gha
cache-to: type=gha,mode=max
#outputs: type=tar,dest=${{ steps.myvars.outputs.CONTAINER_NAME }}-${{ steps.myvars.outputs.CONTAINER_TAG }}-tar
outputs: type=docker,dest=${{ steps.myvars.outputs.CONTAINER_NAME }}-${{ steps.myvars.outputs.CONTAINER_TAG }}-oci-tar
- name: Upload Container Img Tarball as Artifact
uses: actions/upload-artifact@v4
if: success() || failure()
with:
name: ${{ steps.myvars.outputs.CONTAINER_NAME }}-${{ steps.myvars.outputs.CONTAINER_TAG }}-docker-img
path: ${{ steps.myvars.outputs.CONTAINER_NAME }}-${{ steps.myvars.outputs.CONTAINER_TAG }}-oci-tar
scan-vulns-repo:
name: Scan Vulns in Repo
runs-on: ubuntu-latest
needs: [build-and-deploy]
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go environment
uses: actions/setup-go@v5
with:
go-version: '1.22'
- name: Scan in Repo (html)
uses: aquasecurity/trivy-action@master
if: success() || failure()
with:
scan-type: fs
scanners: vuln,secret,config
scan-ref: .
format: template
template: '@/contrib/html.tpl'
output: trivy-results-repo-${{ needs.build-and-deploy.outputs.GIT_HASH_SHORT }}.html
env:
TRIVY_USERNAME: ${{ github.repository_owner }}
TRIVY_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
- name: Scan in Repo (sarif)
uses: aquasecurity/trivy-action@master
if: success() || failure()
with:
scan-type: fs
scanners: vuln,secret,config
scan-ref: .
format: sarif
output: trivy-results-repo-${{ needs.build-and-deploy.outputs.GIT_HASH_SHORT }}.sarif
env:
TRIVY_USERNAME: ${{ github.repository_owner }}
TRIVY_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
- name: Publish Repo Scan Results as Artifact
uses: actions/upload-artifact@v4
if: success() || failure()
with:
name: trivy-results-repo-${{ needs.build-and-deploy.outputs.DATE_IN_SECS }}
path: trivy-results-repo-${{ needs.build-and-deploy.outputs.GIT_HASH_SHORT }}.*
- name: Load Repo Scan Results (sarif) to Github
uses: github/codeql-action/upload-sarif@v2
if: always()
#if: false ## false = bypass
with:
sarif_file: trivy-results-repo-${{ needs.build-and-deploy.outputs.GIT_HASH_SHORT }}.sarif
category: vulns-in-repo
scan-vulns-docker:
name: Scan Vulns in Docker
runs-on: ubuntu-latest
needs: [build-and-deploy]
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Download Container Img Tarball as Artifact
uses: actions/download-artifact@v4
id: container_img_tar
with:
name: ${{ needs.build-and-deploy.outputs.CONTAINER_NAME }}-${{ needs.build-and-deploy.outputs.CONTAINER_TAG }}-docker-img
path: _tmp/
- name: Check Container Image Tarball
run: |
cd _tmp/
mkdir _tar/
ls -la
file ${{ needs.build-and-deploy.outputs.CONTAINER_NAME }}-${{ needs.build-and-deploy.outputs.CONTAINER_TAG }}-oci-tar
## we remove 'z' flag because file is not compressed (gz), only archived (tar)
tar -xvf ${{ needs.build-and-deploy.outputs.CONTAINER_NAME }}-${{ needs.build-and-deploy.outputs.CONTAINER_TAG }}-oci-tar -C _tar/
ls -la _tar/
- name: Vuln scan in Docker (table)
uses: aquasecurity/trivy-action@master
if: always()
with:
scan-type: image
scanners: vuln,secret,config
## it can be the dir with content of untar file or it can be the tar file
input: _tmp/_tar/
##input: _tmp/${{ needs.build-and-deploy.outputs.CONTAINER_NAME }}-${{ needs.build-and-deploy.outputs.CONTAINER_TAG }}-oci-tar
format: table
env:
TRIVY_USERNAME: ${{ github.repository_owner }}
TRIVY_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
- name: Vuln scan in Docker (html)
uses: aquasecurity/trivy-action@master
if: always()
with:
scan-type: image
scanners: vuln,secret,config
## it can be the dir with content of untar file or it can be the tar file
## input: _tmp/_tar/
input: _tmp/${{ needs.build-and-deploy.outputs.CONTAINER_NAME }}-${{ needs.build-and-deploy.outputs.CONTAINER_TAG }}-oci-tar
format: template
template: '@/contrib/html.tpl'
output: trivy-results-docker-${{ needs.build-and-deploy.outputs.GIT_HASH_SHORT }}.html
env:
TRIVY_USERNAME: ${{ github.repository_owner }}
TRIVY_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
- name: Vuln scan in Docker (sarif)
uses: aquasecurity/trivy-action@master
if: always()
with:
scan-type: image
scanners: vuln,secret,config
## it can be the dir with content of untar file or it can be the tar file
## input: _tmp/_tar/
input: _tmp/${{ needs.build-and-deploy.outputs.CONTAINER_NAME }}-${{ needs.build-and-deploy.outputs.CONTAINER_TAG }}-oci-tar
format: sarif
output: trivy-results-docker-${{ needs.build-and-deploy.outputs.GIT_HASH_SHORT }}.sarif
env:
TRIVY_USERNAME: ${{ github.repository_owner }}
TRIVY_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
- name: Publish Docker Scan Results as Artifact
uses: actions/upload-artifact@v4
if: success() || failure()
with:
name: trivy-results-docker-${{ needs.build-and-deploy.outputs.DATE_IN_SECS }}
path: trivy-results-docker-${{ needs.build-and-deploy.outputs.GIT_HASH_SHORT }}.*
- name: Load Docker Scan Results (sarif) to Github
uses: github/codeql-action/upload-sarif@v2
if: always()
#if: false ## false = bypass
with:
sarif_file: trivy-results-docker-${{ needs.build-and-deploy.outputs.GIT_HASH_SHORT }}.sarif
category: vulns-in-docker