83a7cef9f1
CD / Release (push) Successful in 6s
The previous version didn't support Docker images with an alias e.g. "... AS base". Reviewed-on: t.behrendt/trivy-actions#46 Reviewed-by: branch-buddy <branch-buddy@t00n.de> Co-authored-by: Timo Behrendt <t.behrendt@t00n.de> Co-committed-by: Timo Behrendt <t.behrendt@t00n.de>
75 lines
2.9 KiB
YAML
75 lines
2.9 KiB
YAML
name: "Get Images From Files"
|
|
description: "Extract docker images from selected files (Dockerfiles, Kubernetes manifests, Docker Compose/compose files, Helmfile values, and files with recognizable image references)"
|
|
author: "Timo Behrendt <t.behrendt@t00n.de>"
|
|
branding:
|
|
icon: "package"
|
|
color: "blue"
|
|
|
|
inputs:
|
|
files:
|
|
description: "Files to extract images from. Multiple files can be specified (YAML list format)."
|
|
required: true
|
|
|
|
outputs:
|
|
images:
|
|
description: "JSON array of extracted image reference strings (e.g. [\"docker.io/library/alpine:latest\",\"gcr.io/distroless/static:nonroot@sha256:...\"])"
|
|
value: ${{ steps.format.outputs.images }}
|
|
|
|
runs:
|
|
using: "composite"
|
|
steps:
|
|
- id: extract
|
|
shell: bash
|
|
run: |
|
|
set -e
|
|
files="${{ inputs.files }}"
|
|
# Parse YAML list: lines like " - path/to/file" or "- file"
|
|
file_list=$(echo "$files" | sed -n 's/^[[:space:]]*-[[:space:]]*//p' | tr -d '"' | tr -d "'")
|
|
images=$(
|
|
for f in $file_list; do
|
|
[ -f "$f" ] || continue
|
|
# Dockerfile: FROM [--platform=...] <image> [AS <name>]
|
|
awk '/^[[:space:]]*FROM[[:space:]]/ {
|
|
for (i = 2; i <= NF; i++) {
|
|
if ($i !~ /^--/) { print $i; break }
|
|
}
|
|
}' "$f" 2>/dev/null || true
|
|
# Kubernetes / Docker Compose: image: <ref>
|
|
grep -E '[[:space:]]image:[[:space:]]' "$f" 2>/dev/null | sed 's/.*image:[[:space:]]*//' | awk '{ print $1 }' || true
|
|
# Helmfile values: repository + tag (both required, by indent); tag without repo is skipped
|
|
awk '
|
|
match($0, /^[[:space:]]*/) { indent = RLENGTH }
|
|
/^[[:space:]]*repository:[[:space:]]/ {
|
|
v = $0; sub(/^[^:]*:[[:space:]]*"?/, "", v); sub(/"?[[:space:]]*$/, "", v); repo[indent] = v; next
|
|
}
|
|
/^[[:space:]]*tag:[[:space:]]/ {
|
|
v = $0; sub(/^[^:]*:[[:space:]]*"?/, "", v); sub(/"?[[:space:]]*$/, "", v);
|
|
if (repo[indent] != "") { print repo[indent] ":" v; delete repo[indent] }; next
|
|
}
|
|
' "$f" 2>/dev/null || true
|
|
done | grep -E '/' | sort -u | paste -sd,
|
|
)
|
|
echo "images_list=${images:-}" >> "$GITHUB_OUTPUT"
|
|
- id: format
|
|
shell: bash
|
|
run: |
|
|
set -e
|
|
list="${{ steps.extract.outputs.images_list }}"
|
|
json_escape() { printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g'; }
|
|
images="["
|
|
first=1
|
|
if [ -n "$list" ]; then
|
|
IFS=,
|
|
for img in $list; do
|
|
[ -z "$img" ] && continue
|
|
[ "$first" -eq 1 ] && first=0 || images+=","
|
|
images+="\"$(json_escape "$img")\""
|
|
done
|
|
fi
|
|
images+="]"
|
|
echo "images=${images}" >> "$GITHUB_OUTPUT"
|
|
- id: list-images
|
|
shell: bash
|
|
run: |
|
|
echo '${{ steps.format.outputs.images }}'
|