Skip to content

Commit e667bf5

Browse files
committed
fix(ci): test trivy workflow
1 parent e8e9354 commit e667bf5

1 file changed

Lines changed: 31 additions & 5 deletions

File tree

.github/workflows/trivy.yml

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,57 @@ jobs:
1515
- name: Extract changed images from PR diff
1616
id: images
1717
run: |
18-
git fetch origin ${{ github.base_ref }}
18+
# Fetch the base branch with sufficient history
19+
git fetch origin ${{ github.base_ref }}:refs/remotes/origin/${{ github.base_ref }} --depth=100
20+
21+
# Find merge base and create diff
22+
MERGE_BASE=$(git merge-base HEAD origin/${{ github.base_ref }})
23+
echo "Using merge base: $MERGE_BASE"
24+
25+
echo "=== Analyzing PR diff for container images ==="
26+
git diff $MERGE_BASE...HEAD --name-only | head -10
1927
2028
# Look for changes to k8s images
21-
repo=$(git diff origin/${{ github.base_ref }}...HEAD | grep -E '^\+.*repository:' | sed -E 's/.*repository:[[:space:]]*//g' | head -n1)
22-
tag=$(git diff origin/${{ github.base_ref }}...HEAD | grep -E '^\+.*tag:' | sed -E 's/.*tag:[[:space:]]*//g' | head -n1)
29+
repo=$(git diff $MERGE_BASE...HEAD | grep -E '^\+.*repository:' | sed -E 's/.*repository:[[:space:]]*([^[:space:]]+).*/\1/' | head -n1)
30+
tag=$(git diff $MERGE_BASE...HEAD | grep -E '^\+.*tag:' | sed -E 's/.*tag:[[:space:]]*([^[:space:]]+).*/\1/' | head -n1)
31+
32+
echo "Found k8s repo: '$repo', tag: '$tag'"
2333
2434
if [ -n "$repo" ] && [ -n "$tag" ]; then
2535
image="$repo:$tag"
36+
echo "Using k8s format: $image"
2637
else
2738
# Fall back to plain image: lines (docker-compose)
28-
image=$(git diff origin/${{ github.base_ref }}...HEAD | grep -E '^\+.*image:' | sed -E 's/.*image:[[:space:]]*//g' | head -n1)
39+
image=$(git diff $MERGE_BASE...HEAD | grep -E '^\+.*image:' | sed -E 's/.*image:[[:space:]]*([^[:space:]#]+).*/\1/' | head -n1)
40+
echo "Raw image found: '$image'"
41+
42+
# Clean up common prefixes and quotes
43+
image=$(echo "$image" | sed -E 's/^["\x27]//; s/["\x27]$//; s/^[[:space:]]*//; s/[[:space:]]*$//')
44+
echo "Cleaned image: '$image'"
2945
fi
3046
3147
echo "Found image: $image"
3248
echo "image=$image" >> $GITHUB_OUTPUT
49+
50+
# Set a flag to indicate if we should run the scan
51+
if [ -n "$image" ] && [ "$image" != "" ]; then
52+
echo "should_scan=true" >> $GITHUB_OUTPUT
53+
echo "Will scan image: $image"
54+
else
55+
echo "should_scan=false" >> $GITHUB_OUTPUT
56+
echo "No container images found in the diff, skipping Trivy scan"
57+
fi
3358
3459
- name: Scan changed image with Trivy
35-
uses: aquasecurity/trivy-action@0.33.1
60+
uses: aquasecurity/trivy-action@0.28.0
3661
with:
3762
image-ref: ${{ steps.images.outputs.image }}
3863
format: 'sarif'
3964
output: 'trivy-results.sarif'
4065
severity: 'CRITICAL,HIGH'
4166

4267
- name: Upload Trivy scan results to GitHub Security tab
68+
if: steps.images.outputs.should_scan == 'true'
4369
uses: github/codeql-action/upload-sarif@v3
4470
with:
4571
sarif_file: 'trivy-results.sarif'

0 commit comments

Comments
 (0)