-
Notifications
You must be signed in to change notification settings - Fork 2
171 lines (164 loc) · 6.64 KB
/
build-container.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
name: Build Image
on:
push:
tags:
- '*-v*.*.*'
pull_request:
branches: [ main ]
merge_group:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: 'read'
id-token: 'write'
packages: 'write'
security-events: write
actions: read
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0 # No shallow clone, we need all history
- name: Set tag output
id: tag
run: |
if [ ${{ github.event_name }} == 'pull_request' ];
then
echo "running because of PR"
CHANGED_DIRS=$(git diff --name-only ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }} | xargs -I {} dirname {})
CONTAINER_FOLDERS=$(for CHANGED_DIR in ${CHANGED_DIRS}; do echo ${CHANGED_DIR} | awk -F "/" '{print $1}'; done | sort -u)
# there should only every be 1 container folder changed in a PR
for CONTAINER_FOLDER in ${CONTAINER_FOLDERS}; do
if [[ "${CONTAINER_FOLDER}" != "."* ]]; then
APP="${CONTAINER_FOLDER}"
VERSION="dev"
fi
done
elif [ ${{ github.event_name }} == 'merge_group' ];
then
echo "running because of Merge Group"
CHANGED_DIRS=$(git diff --name-only ${{ github.event.before }}..${{ github.event.after }} | xargs -I {} dirname {})
CONTAINER_FOLDERS=$(for CHANGED_DIR in ${CHANGED_DIRS}; do echo ${CHANGED_DIR} | awk -F "/" '{print $1}'; done | sort -u)
# there should only every be 1 container folder changed in a PR
for CONTAINER_FOLDER in ${CONTAINER_FOLDERS}; do
if [[ "${CONTAINER_FOLDER}" != "."* ]]; then
APP="${CONTAINER_FOLDER}"
VERSION="dev"
fi
done
else
TAG=${GITHUB_REF#refs/*/}
APP=$(echo $TAG | cut -d'-' -f1)
VERSION=$(echo $TAG | cut -d'-' -f2)
fi
APP_VERSION=$(grep -Eo "FROM[^:]+:[^[:space:]]+" ${APP}/Dockerfile | cut -d ":" -f 2-)
APP_IMAGE=$(grep -Eo "FROM[^:]+:[^[:space:]]+" ${APP}/Dockerfile | cut -d ":" -f -1 | cut -d/ -f 2- | cut -d " " -f 2-)
echo "app=${APP}" >> $GITHUB_OUTPUT
echo "version=${VERSION}" >> $GITHUB_OUTPUT
echo "app_version=${APP_VERSION}" >> $GITHUB_OUTPUT
echo "app_image=${APP_IMAGE}" >> $GITHUB_OUTPUT
- name: Generate tag value
id: tag_value
run: |
if [ ${{ github.event_name }} == 'pull_request' ] || [ ${{ github.event_name }} == 'merge_group' ];
then
TAG_VALUE="type=raw,value=${{ steps.tag.outputs.app_version }}-plural-${{ steps.tag.outputs.version }}"
else
TAG_VALUE="type=match,prefix=${{ steps.tag.outputs.app_version }}-plural,pattern=.*-v(.*),group=1"
fi
echo "tag_value=${TAG_VALUE}" >> $GITHUB_OUTPUT
- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
flavor: |
latest=false
# list of Docker images to use as base name for tags
images: |
ghcr.io/pluralsh/containers/${{ steps.tag.outputs.app_image }}
dkr.plural.sh/${{ steps.tag.outputs.app }}/${{ steps.tag.outputs.app_image }}
# generate Docker tags based on the following events/attributes
tags: |
${{ steps.tag_value.outputs.tag_value }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to GHCR
if: ${{ github.event_name != 'pull_request' }}
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Login to plural registry
uses: docker/login-action@v2
if: ${{ github.event_name != 'pull_request' }}
with:
registry: dkr.plural.sh
username: [email protected]
password: ${{ secrets.PLURAL_ACCESS_TOKEN }}
- name: Build ${{ steps.tag.outputs.app }} image for security scanning
uses: docker/build-push-action@v4
with:
context: ${{ steps.tag.outputs.app }}
file: "${{ steps.tag.outputs.app }}/Dockerfile"
load: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
scan-type: 'image'
image-ref: ${{ fromJSON(steps.meta.outputs.json).tags[0] }}
hide-progress: false
format: 'sarif'
output: 'trivy-results.sarif'
scanners: 'vuln'
ignore-unfixed: true
#severity: 'CRITICAL,HIGH'
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: 'trivy-results.sarif'
- name: Build ${{ steps.tag.outputs.app }} image
uses: docker/build-push-action@v4
with:
context: ${{ steps.tag.outputs.app }}
file: "${{ steps.tag.outputs.app }}/Dockerfile"
push: ${{ github.event_name != 'pull_request' && github.event_name != 'merge_group' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm64
# cache-from: type=gha
# cache-to: type=gha,mode=max
trivy-scan:
name: Trivy IaC scan
runs-on: ubuntu-latest
permissions:
contents: read # for actions/checkout to fetch code
security-events: write # for github/codeql-action/upload-sarif to upload SARIF results
actions: read # only required for a private repository by github/codeql-action/upload-sarif to get the Action run status
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Run Trivy vulnerability scanner in IaC mode
uses: aquasecurity/trivy-action@master
with:
scan-type: 'fs'
hide-progress: false
scan-ref: '.'
format: 'sarif'
output: 'trivy-results.sarif'
scanners: 'vuln,secret,config'
ignore-unfixed: true
#severity: 'CRITICAL,HIGH'
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: 'trivy-results.sarif'