@@ -12,22 +12,32 @@ jobs:
1212 - name : Checkout code
1313 uses : actions/checkout@v5
1414
15+ - name : Fetch all history for git diff
16+ run : |
17+ if [ "$(git rev-parse --is-shallow-repository)" = "true" ]; then
18+ git fetch --unshallow
19+ else
20+ git fetch --depth=2
21+ fi
22+
1523 - name : Extract changed images from PR diff
1624 id : images
1725 run : |
18- # Fetch the base branch with sufficient history
19- git fetch origin ${{ github.base_ref }}:refs/remotes/origin/${{ github.base_ref }} --depth=100
26+ echo "=== Analyzing PR diff for container images ==="
2027
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 "
28+ # Get all changed files in docker/ and kubernetes/
29+ changed_files =$(git diff --name-only HEAD~1 HEAD | grep -E '^(docker|kubernetes)/' || true )
30+ echo "Changed files : $changed_files "
2431
25- echo "=== Analyzing PR diff for container images ==="
26- git diff $MERGE_BASE...HEAD --name-only | head -10
27-
28- # Look for changes to k8s images
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)
32+ if [ -z "$changed_files" ]; then
33+ echo "No docker or kubernetes files changed"
34+ echo "should_scan=false" >> $GITHUB_OUTPUT
35+ exit 0
36+ fi
37+
38+ # Look for changes to k8s images in the actual diff
39+ repo=$(git diff HEAD~1 HEAD | grep -E '^\+.*repository:' | sed -E 's/.*repository:[[:space:]]*([^[:space:]]+).*/\1/' | head -n1)
40+ tag=$(git diff HEAD~1 HEAD | grep -E '^\+.*tag:' | sed -E 's/.*tag:[[:space:]]*([^[:space:]]+).*/\1/' | head -n1)
3141
3242 echo "Found k8s repo: '$repo', tag: '$tag'"
3343
3646 echo "Using k8s format: $image"
3747 else
3848 # Fall back to plain image: lines (docker-compose)
39- image=$(git diff $MERGE_BASE... HEAD | grep -E '^\+.*image:' | sed -E 's/.*image:[[:space:]]*([^[:space:]#]+).*/\1/' | head -n1)
49+ image=$(git diff HEAD~1 HEAD | grep -E '^\+.*image:' | sed -E 's/.*image:[[:space:]]*([^[:space:]#]+).*/\1/' | head -n1)
4050 echo "Raw image found: '$image'"
4151
4252 # Clean up common prefixes and quotes
5767 fi
5868
5969 - name : Scan changed image with Trivy
70+ if : steps.images.outputs.should_scan == 'true'
6071 uses : aquasecurity/trivy-action@0.33.1
6172 with :
6273 image-ref : ${{ steps.images.outputs.image }}
0 commit comments