-
Notifications
You must be signed in to change notification settings - Fork 0
184 lines (164 loc) · 6.18 KB
/
build-image.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
name: 'Build and Scan Container'
on:
workflow_dispatch:
pull_request:
branches:
- stable
- 'release-*'
push:
branches:
- "*"
- "**/*"
# You can kill currently running builds on the branch this way
#concurrency:
# group: ${{ github.ref }}-ci
# cancel-in-progress: true
jobs:
build:
name: Build
runs-on: ubuntu-20.04
permissions:
contents: read
id-token: write
pull-requests: write
outputs:
digest: ${{ steps.push.outputs.digest }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup buildx
uses: docker/setup-buildx-action@v2
- name: Configure AWS credentials
if: github.event_name != 'pull_request'
uses: aws-actions/configure-aws-credentials@v3
with:
aws-region: ${{ secrets.AWS_REGION }}
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/${{ secrets.AWS_ROLE }}
role-session-name: github-action
- name: Export environment variables
id: env
env:
SHA: ${{ github.sha }}
run: |
export NODE_VERSION=`cat .nvmrc`
export GITHASH="${SHA::9}"
export IMAGE_TAG="githash-${GITHASH}"
echo "## Git Hash" >> $GITHUB_STEP_SUMMARY
echo $IMAGE_TAG >> $GITHUB_STEP_SUMMARY
echo "node_version=$NODE_VERSION" >> $GITHUB_OUTPUT
echo "git_hash=$GITHASH" >> $GITHUB_OUTPUT
echo "image_tag=$IMAGE_TAG" >> $GITHUB_OUTPUT
# Build and scan on PRs
- name: Build container image for PR
if: github.event_name == 'pull_request'
id: vuln_build
uses: docker/build-push-action@v3
with:
push: false
file: docker/Dockerfile
tags: ${{ secrets.IMAGE_NAME }}:${{ steps.env.outputs.image_tag }}
outputs: type=docker
build-args: |
NODE_VERSION=${{ steps.env.outputs.node_version }}
GITHASH=${{ steps.env.outputs.git_hash }}
FULL_GITHASH=${{ github.event.pull_request.head.sha }}
SENTRY_AUTH_TOKEN=${{ secrets.SENTRY_AUTH_TOKEN }}
- name: Run vulnerability scan for PR
if: github.event_name == 'pull_request'
id: vuln_scan
uses: aquasecurity/[email protected]
env:
TRIVY_DB_REPOSITORY: "public.ecr.aws/aquasecurity/trivy-db:2"
TRIVY_JAVA_DB_REPOSITORY: "public.ecr.aws/aquasecurity/trivy-java-db:1"
with:
image-ref: ${{ secrets.IMAGE_NAME }}:${{ steps.env.outputs.image_tag }}
format: 'table'
exit-code: '1'
ignore-unfixed: true
severity: 'CRITICAL,HIGH'
# Build scan and push to ECR
- name: Login to Amazon ECR
if: github.event_name != 'pull_request'
uses: aws-actions/amazon-ecr-login@v1
with:
mask-password: true
- name: Build container image
if: github.event_name != 'pull_request'
id: build
uses: docker/build-push-action@v3
with:
push: false
file: docker/Dockerfile
tags: ${{ secrets.IMAGE_NAME }}:${{ steps.env.outputs.image_tag }}
outputs: type=docker
build-args: |
NODE_VERSION=${{ steps.env.outputs.node_version }}
GITHASH=${{ steps.env.outputs.git_hash }}
FULL_GITHASH=${{ github.sha }}
SENTRY_AUTH_TOKEN=${{ secrets.SENTRY_AUTH_TOKEN }}
# log medium vulnerabilities
- name: Run scan for medium vulnerabilities
if: github.event_name != 'pull_request'
id: scan_medium
uses: aquasecurity/[email protected]
env:
TRIVY_DB_REPOSITORY: "public.ecr.aws/aquasecurity/trivy-db:2"
TRIVY_JAVA_DB_REPOSITORY: "public.ecr.aws/aquasecurity/trivy-java-db:1"
continue-on-error: true
with:
image-ref: ${{ secrets.IMAGE_NAME }}:${{ steps.env.outputs.image_tag }}
format: 'table'
exit-code: '0'
ignore-unfixed: true
severity: 'MEDIUM'
# fail on critical and high vulnerabilities
- name: Run scan for critical and high vulnerabilities
if: github.event_name != 'pull_request'
id: scan_crit
uses: aquasecurity/[email protected]
env:
TRIVY_DB_REPOSITORY: "public.ecr.aws/aquasecurity/trivy-db:2"
TRIVY_JAVA_DB_REPOSITORY: "public.ecr.aws/aquasecurity/trivy-java-db:1"
with:
image-ref: ${{ secrets.IMAGE_NAME }}:${{ steps.env.outputs.image_tag }}
format: 'table'
exit-code: '1'
ignore-unfixed: true
severity: 'CRITICAL,HIGH'
- name: Test container
if: github.event_name != 'pull_request'
id: test
env:
image: ${{ secrets.IMAGE_NAME }}:${{ steps.env.outputs.image_tag }}
shell: bash
run: |
./bin/test-container $image
- name: Set Docker remote
if: ${{ github.event_name != 'pull_request' }}
id: metadata
uses: docker/metadata-action@v4
with:
images: ${{ secrets.AWS_ECR_REGISTRY }}
tags: |
${{ steps.env.outputs.image_tag }}
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'stable') }}
type=raw,value=stable,enable=${{ github.ref == format('refs/heads/{0}', 'stable') }}
type=ref,event=branch
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
- name: Build and push container image
if: ${{ github.event_name != 'pull_request' }}
id: push
uses: docker/build-push-action@v3
with:
push: ${{ github.event_name != 'pull_request' }}
file: docker/Dockerfile
labels: ${{ steps.metadata.outputs.labels }}
tags: ${{ steps.metadata.outputs.tags }}
#tags: ${{ secrets.AWS_ECR_REGISTRY }}:${{ steps.env.outputs.image_tag }}
build-args: |
NODE_VERSION=${{ steps.env.outputs.node_version }}
GITHASH=${{ steps.env.outputs.git_hash }}
FULL_GITHASH=${{ github.sha }}
SENTRY_AUTH_TOKEN=${{ secrets.SENTRY_AUTH_TOKEN }}