Files
ci-actions/dtrack-sbom/action.yml
T
tthandClaude Opus 5.5 e82c65a3c4
tag / tag (push) Skipped
dtrack-sbom: never fail the job when Dependency-Track is unreachable (v8)
The runner starts composite bash steps with -e, while the script handles
every failure itself and only warns. With dtrack.grg down, curl wrote no
resp.json, the unguarded cat of it failed and -e ended the step with
exit 1 before warn() ran - a failed build (meshcore-open run 29) instead
of a warning. set +e restores the behaviour the script was written for.

Checked with the step run under bash -e -o pipefail against an
unreachable URL (was exit 1, now ::warning:: HTTP 000 and exit 0), a
server answering 401 (warning, exit 0) and 200 (uploaded, token shown).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 18:26:35 +01:00

88 lines
3.9 KiB
YAML

name: dtrack-sbom
description: >
Convert a trivy-gate report to CycloneDX and upload it to Dependency-Track.
Never fails the job: an SBOM outage must not stop a deploy, it only warns.
inputs:
report:
description: Trivy JSON report from trivy-gate (outputs.report).
required: true
url:
description: Dependency-Track base URL.
required: true
api-key:
description: API key with BOM_UPLOAD (+ PROJECT_CREATION_UPLOAD).
required: true
ca-pem:
description: PEM of the CA that signed the Dependency-Track certificate.
default: ""
project:
description: Project name in Dependency-Track.
required: true
version:
description: Project version (use the deployed tag, e.g. latest).
required: true
extra-components:
description: >
JSON file with a CycloneDX components array appended to the SBOM
(vendored libraries without package metadata).
default: ""
min-components:
description: Refuse to upload a suspiciously small SBOM.
default: "5"
trivy-image:
description: Trivy container image, pinned by digest.
default: aquasec/trivy:0.76.1@sha256:790fd66d1697395d8693d1acaa1ea702406a20f109b6fc7bac8db623eca6c06c
runs:
using: composite
steps:
- shell: bash
env:
REPORT: ${{ inputs.report }}
DT_URL: ${{ inputs.url }}
DT_KEY: ${{ inputs.api-key }}
CA_PEM: ${{ inputs.ca-pem }}
PROJECT: ${{ inputs.project }}
VERSION: ${{ inputs.version }}
EXTRA: ${{ inputs.extra-components }}
MIN: ${{ inputs.min-components }}
TRIVY: ${{ inputs.trivy-image }}
run: |
# The runner starts composite bash steps with -e; this script handles
# every failure itself (warn, exit 0), so -e only turns an unguarded
# command into a failed job - e.g. the cat of a resp.json curl never
# wrote when Dependency-Track was unreachable.
set +e -uo pipefail
warn() { echo "::warning::SBOM $PROJECT/$VERSION: $*"; exit 0; }
[ -n "$REPORT" ] && [ -f "$REPORT" ] || warn "no report (gate skipped?)"
[ -n "$DT_KEY" ] || warn "no API key"
work="${RUNNER_TEMP:-/tmp}/dtrack-sbom"; mkdir -p "$work"
# Same runtime as trivy-gate: its container when a Docker daemon is
# reachable, else the checksum-verified binary trivy-gate fetched.
gate_bin="${RUNNER_TEMP:-/tmp}/trivy-gate/bin/trivy"
if command -v docker >/dev/null && docker info >/dev/null 2>&1; then
docker run -i --rm --entrypoint sh "$TRIVY" -c \
'cat >/tmp/r.json && trivy convert --quiet --format cyclonedx /tmp/r.json' \
<"$REPORT" >"$work/bom.json" || warn "trivy convert failed"
elif [ -x "$gate_bin" ]; then
"$gate_bin" convert --quiet --format cyclonedx "$REPORT" >"$work/bom.json" || warn "trivy convert failed"
else
warn "neither Docker nor the trivy-gate binary available"
fi
if [ -n "$EXTRA" ]; then
jq --slurpfile extra "$EXTRA" '.components += $extra[0]' "$work/bom.json" >"$work/bom2.json" \
|| warn "cannot merge $EXTRA"
mv "$work/bom2.json" "$work/bom.json"
fi
n=$(jq '.components | length' "$work/bom.json") || warn "unreadable SBOM"
[ "$n" -ge "$MIN" ] || warn "only $n components, not uploading"
ca=()
if [ -n "$CA_PEM" ]; then printf '%s\n' "$CA_PEM" >"$work/ca.pem"; ca=(--cacert "$work/ca.pem"); fi
code=$(curl -sS "${ca[@]}" -o "$work/resp.json" -w '%{http_code}' \
-X POST "$DT_URL/api/v1/bom" -H "X-Api-Key: $DT_KEY" \
-F autoCreate=true -F "projectName=$PROJECT" -F "projectVersion=$VERSION" \
-F "bom=@$work/bom.json") || code=000
[ "$code" = 200 ] || { cat "$work/resp.json" 2>/dev/null; echo; warn "HTTP $code"; }
echo "SBOM $PROJECT/$VERSION: $n components uploaded (token $(jq -r .token "$work/resp.json"))"