Skip to content

Commit a0fd31e

Browse files
Merge pull request #607 from caponetto/RHOAIENG-9473
RHOAIENG-9473: Optionally enable Trivy for pull requests
2 parents 74e032e + 9b1e380 commit a0fd31e

File tree

2 files changed

+49
-20
lines changed

2 files changed

+49
-20
lines changed

.github/workflows/build-notebooks-TEMPLATE.yaml

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,42 @@ jobs:
120120
IMAGE_REGISTRY: "ghcr.io/${{ github.repository }}/workbench-images"
121121
CONTAINER_BUILD_CACHE_ARGS: "--cache-from ${{ env.CACHE }} --cache-to ${{ env.CACHE }}"
122122

123-
- name: "schedule: run Trivy vulnerability scanner"
124-
if: "${{ fromJson(inputs.github).event_name == 'schedule' }}"
123+
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
124+
- name: "pull_request: make ${{ inputs.target }}"
125+
run: |
126+
# start a black hole container registry as make target always does a push
127+
mkdir -p $HOME/.config/containers/registries.conf.d/
128+
cp ci/cached-builds/insecure_localhost_registry.conf $HOME/.config/containers/registries.conf.d/insecure_localhost_registry.conf
129+
go run ci/cached-builds/dev_null_container_registry.go &
130+
# build and push the image
131+
make ${{ inputs.target }}
132+
if: "${{ fromJson(inputs.github).event_name == 'pull_request' }}"
133+
env:
134+
IMAGE_TAG: "${{ github.sha }}"
135+
IMAGE_REGISTRY: "localhost:5000/workbench-images"
136+
CONTAINER_BUILD_CACHE_ARGS: "--cache-from ${{ env.CACHE }}"
137+
138+
- name: "pull_request|schedule: resolve image name if Trivy scan should run"
139+
id: resolve-image
140+
if: ${{ fromJson(inputs.github).event_name == 'pull_request' || fromJson(inputs.github).event_name == 'schedule' }}
141+
env:
142+
EVENT_NAME: ${{ fromJson(inputs.github).event_name }}
143+
HAS_TRIVY_LABEL: ${{ contains(fromJson(inputs.github).event.pull_request.labels.*.name, 'trivy-scan') }}
144+
run: |
145+
if [[ "$EVENT_NAME" == "pull_request" && "$HAS_TRIVY_LABEL" == "true" ]]; then
146+
IMAGE_NAME="localhost:5000/workbench-images:${{ inputs.target }}-${{ github.sha }}"
147+
echo "image=$IMAGE_NAME" >> $GITHUB_OUTPUT
148+
elif [[ "$EVENT_NAME" == "schedule" ]]; then
149+
IMAGE_NAME="ghcr.io/${{ github.repository }}/workbench-images:${{ inputs.target }}-${{ github.ref_name }}_${{ github.sha }}"
150+
echo "image=$IMAGE_NAME" >> $GITHUB_OUTPUT
151+
fi
152+
153+
if [[ -z "$IMAGE_NAME" ]]; then
154+
echo "Trivy scan won't run"
155+
fi
156+
157+
- name: Run Trivy vulnerability scanner
158+
if: ${{ steps.resolve-image.outputs.image }}
125159
run: |
126160
TRIVY_VERSION=0.53.0
127161
REPORT_FOLDER=${{ github.workspace }}/report
@@ -131,9 +165,15 @@ jobs:
131165
mkdir -p $REPORT_FOLDER
132166
cp ci/$REPORT_TEMPLATE $REPORT_FOLDER
133167
134-
IMAGE_NAME=ghcr.io/${{ github.repository }}/workbench-images:${{ inputs.target }}-${{ github.ref_name }}_${{ github.sha }}
168+
IMAGE_NAME=${{ steps.resolve-image.outputs.image }}
135169
echo "Scanning $IMAGE_NAME"
136170
171+
SEVERITY_OPTION=""
172+
# Report only higher vulnerabilities if not a pull request
173+
if [ "${{ fromJson(inputs.github).event_name }}" != "pull_request" ]; then
174+
SEVERITY_OPTION="--severity CRITICAL,HIGH"
175+
fi
176+
137177
# have trivy access podman socket,
138178
# https://github.com/aquasecurity/trivy/issues/580#issuecomment-666423279
139179
podman run --rm \
@@ -143,28 +183,13 @@ jobs:
143183
image \
144184
--image-src podman \
145185
--podman-host /var/run/podman/podman.sock \
146-
--scanners vuln,secret \
186+
--scanners vuln --ignore-unfixed \
147187
--exit-code 0 --timeout 30m \
148-
--severity CRITICAL,HIGH \
188+
$SEVERITY_OPTION \
149189
--format template --template "@/report/$REPORT_TEMPLATE" -o /report/$REPORT_FILE \
150190
$IMAGE_NAME
151191
152192
cat $REPORT_FOLDER/$REPORT_FILE >> $GITHUB_STEP_SUMMARY
153193
154-
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
155-
- name: "pull_request: make ${{ inputs.target }}"
156-
run: |
157-
# start a black hole container registry as make target always does a push
158-
mkdir -p $HOME/.config/containers/registries.conf.d/
159-
cp ci/cached-builds/insecure_localhost_registry.conf $HOME/.config/containers/registries.conf.d/insecure_localhost_registry.conf
160-
go run ci/cached-builds/dev_null_container_registry.go &
161-
# build and push the image
162-
make ${{ inputs.target }}
163-
if: "${{ fromJson(inputs.github).event_name == 'pull_request' }}"
164-
env:
165-
IMAGE_TAG: "${{ github.sha }}"
166-
IMAGE_REGISTRY: "localhost:5000/workbench-images"
167-
CONTAINER_BUILD_CACHE_ARGS: "--cache-from ${{ env.CACHE }}"
168-
169194
- run: df -h
170195
if: "${{ !cancelled() }}"

.github/workflows/build-notebooks-pr.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ permissions:
88
packages: read
99
pull-requests: read
1010

11+
concurrency:
12+
group: ${{ format('build-notebooks-pr-{0}', github.event.pull_request.number) }}
13+
cancel-in-progress: true
14+
1115
jobs:
1216
gen:
1317
name: Generate job matrix

0 commit comments

Comments
 (0)