Skip to content

Commit c7a2eb6

Browse files
authored
Merge pull request #134 from maykinmedia/feature/115-add-trivy-scan
🎨 [#115] fixed the trivy image scan
2 parents b1699e2 + 0b88fa2 commit c7a2eb6

File tree

1 file changed

+58
-20
lines changed

1 file changed

+58
-20
lines changed

.github/workflows/ci.yml

+58-20
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,12 @@ jobs:
109109

110110
docker:
111111
needs: tests
112-
113-
name: Build (and push) Docker image
112+
name: Docker image build
114113
runs-on: ubuntu-latest
115114

116115
steps:
117-
- uses: actions/checkout@v2
118-
119-
- name: Set tag
116+
- uses: actions/checkout@v3
117+
- name: Determine tag/commit hash
120118
id: vars
121119
run: |
122120
# Strip git ref prefix from version
@@ -125,21 +123,22 @@ jobs:
125123
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
126124
# Use Docker `latest` tag convention
127125
[ "$VERSION" == "master" ] && VERSION=latest
128-
echo ::set-output name=tag::${VERSION}
126+
echo "tag=${VERSION}" >> $GITHUB_OUTPUT
127+
echo "git_hash=${GITHUB_SHA}" >> $GITHUB_OUTPUT
129128
- name: Build the Docker image
130-
env:
131-
RELEASE_VERSION: ${{ steps.vars.outputs.tag }}
132-
run: docker build . --tag $IMAGE_NAME:$RELEASE_VERSION
133-
134-
- name: Log into registry
135-
if: github.event_name == 'push' # exclude PRs
136-
run: echo "${{ secrets.DOCKER_TOKEN }}" | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
137-
138-
- name: Push the Docker image
139-
if: github.event_name == 'push' && github.repository_owner == 'maykinmedia' # exclude PRs/forks
140-
env:
141-
RELEASE_VERSION: ${{ steps.vars.outputs.tag }}
142-
run: docker push $IMAGE_NAME:$RELEASE_VERSION
129+
run: |
130+
docker build \
131+
--tag $IMAGE_NAME:${{ steps.vars.outputs.tag }} \
132+
--build-arg COMMIT_HASH=${{ steps.vars.outputs.git_hash }} \
133+
--build-arg RELEASE=${{ steps.vars.outputs.tag }} \
134+
.
135+
- run: docker image save -o image.tar $IMAGE_NAME:${{ steps.vars.outputs.tag }}
136+
- name: Store image artifact
137+
uses: actions/upload-artifact@v3
138+
with:
139+
name: docker-image
140+
path: image.tar
141+
retention-days: 1
143142

144143
image_scan:
145144
runs-on: ubuntu-latest
@@ -167,7 +166,7 @@ jobs:
167166
- name: Download built image
168167
uses: actions/download-artifact@v3
169168
with:
170-
name: docker-image-all-extensions-${{ steps.vars.outputs.tag }}
169+
name: docker-image
171170
- name: Scan image with Trivy
172171
uses: aquasecurity/trivy-action@master
173172
with:
@@ -180,3 +179,42 @@ jobs:
180179
with:
181180
sarif_file: 'trivy-results-docker.sarif'
182181

182+
publish:
183+
needs:
184+
- tests
185+
- docker
186+
187+
name: Push Docker image
188+
runs-on: ubuntu-latest
189+
if: github.event_name == 'push' && github.repository_owner == 'open-klant' # exclude PRs/forks
190+
191+
steps:
192+
- uses: actions/checkout@v3
193+
- name: Download built image
194+
uses: actions/download-artifact@v3
195+
with:
196+
name: docker-image
197+
198+
- name: Determine tag/commit hash
199+
id: vars
200+
run: |
201+
# Strip git ref prefix from version
202+
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
203+
204+
# Strip "v" prefix from tag name (if present at all)
205+
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
206+
207+
# Use Docker `latest` tag convention
208+
[ "$VERSION" == "main" ] && VERSION=latest
209+
210+
echo "tag=${VERSION}" >> $GITHUB_OUTPUT
211+
212+
- name: Load image
213+
run: |
214+
docker image load -i image.tar
215+
216+
- name: Log into registry
217+
run: echo "${{ secrets.DOCKER_TOKEN }}" | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
218+
219+
- name: Push the Docker image
220+
run: docker push $IMAGE_NAME:${{ steps.vars.outputs.tag }}

0 commit comments

Comments
 (0)