How to Generate an SBOM in CI
Produce a software bill of materials for every build so you can answer within minutes whether a new vulnerability affects you.
On this page
Introduction
When a new vulnerability is announced in a popular library, the first question is whether your software contains it. Without an inventory you grep lockfiles, guess at transitive dependencies and hope. A software bill of materials is that inventory: a machine-readable list of every component in a build, with versions and identifiers. Generating it in CI means every artifact has one, so the question becomes a query instead of a research project. This article covers the two dominant formats, how to generate an SBOM in a pipeline, how to verify it matches the artifact, and how to use it operationally.
What an SBOM Is and Why It Matters
An SBOM lists the components that make up a piece of software: direct and transitive dependencies, their versions, their licenses, and ideally identifiers that link them to vulnerability databases. It is a manifest, not a scanner; it tells you what is present, and a scanner tells you what in that list is known-bad. The value is in coverage of transitive dependencies, which are the parts you did not choose and often cannot name.
The practical payoff is response time. With an SBOM per artifact, a new CVE becomes a lookup: which of my artifacts contain this component at an affected version. Without one, each incident starts from scratch. Regulators and customers increasingly ask for an SBOM as a condition of supply, so producing it is also a compliance measure.
Choosing a Format
Two formats dominate. SPDX is ISO-standardized, concise, and good at license and package identity. CycloneDX is security-oriented, with native support for vulnerability and service components and a component identity model designed for correlation. Both can express the same facts for most builds; pick one as the internal standard and be able to emit the other if a consumer asks.
Key the components by a package URL, which encodes ecosystem, namespace, name and version in a canonical string. That identifier is what makes SBOMs comparable across tools and databases, and without it a vulnerability match degrades to fuzzy name matching, which produces both false positives and false negatives.
Generating an SBOM in the Pipeline
Generate the SBOM from the artifact, not from the source tree. The built image or package is what you ship, and its dependency set can differ from the lockfile because of build-time resolution. Scan the container image or the built package and emit SBOMs in both formats, then attach them to the artifact.
# Generate a CycloneDX SBOM directly from a container image
syft ghcr.io/acme/orders:1.4.2 -o cyclonedx-json > sbom.cdx.json
# Also emit SPDX for consumers that require it
syft ghcr.io/acme/orders:1.4.2 -o spdx-json > sbom.spdx.json
# Attach the SBOM to the image as an in-toto attestation
cosign attach sbom --sbom sbom.cdx.json ghcr.io/acme/orders:1.4.2
# Verify that the attached SBOM matches the image digest
cosign verify-attestation --type cyclonedx \
--certificate-identity-regexp '.*' \
--certificate-oidc-issuer https://token.actions.githubusercontent.com \
ghcr.io/acme/orders:1.4.2
Verifying the SBOM Matches the Artifact
An SBOM that describes a different build than the one deployed is worse than none, because it creates false confidence. Compute the artifact digest at build time and bind the SBOM to that digest, either by attaching it to the image or by recording both in the pipeline output. Verify the binding downstream before trusting the inventory.
Treat an SBOM as an attestation: sign it with the same identity that signs the image, so a consumer can verify that this inventory was produced by the trusted builder for this digest. An unsigned SBOM is a claim from an unknown source.
Using the SBOM Operationally
Store SBOMs where they can be queried by component, not just by artifact, so a new CVE can be turned into a list of affected artifacts in one query. Continuously match the inventory against a vulnerability feed and reopen findings when a feed updates, because a component that was fine yesterday may be affected today by a newly published advisory.
Use the SBOM for more than vulnerabilities: license compliance, end-of-life components, and duplicate dependencies. A quick query for the same package at many versions often reveals a dependency tree that could be consolidated, which reduces the future blast radius of any single advisory.
# Turn a new advisory into the list of affected artifacts
for sbom in sboms/*.cdx.json; do
grype "sbom:$sbom" --fail-on high -o table || echo "AFFECTED: $sbom"
done
# Query stored SBOMs directly: which artifacts ship log4j-core at 2.14.1?
for sbom in sboms/*.cdx.json; do
if jq -e '.components[] | select(.name=="log4j-core" and .version=="2.14.1")' "$sbom" >/dev/null; then
echo "affected: $sbom"
fi
done
Limits and Pitfalls
An SBOM cannot include what the scanner cannot see: statically linked binaries, vendored code, or components pulled at runtime. It reflects the scan tool’s ability to enumerate, so treat absence from the SBOM as unknown rather than safe. Complement it with runtime monitoring and reproducible builds where the risk is high.
Do not generate the SBOM after the fact from a different revision, and do not regenerate it on every query. Generate once per artifact at build time and store the result; a fresh scan of an old image may see a different set if the image was mutated or the scanner changed, which introduces inconsistency between what you claim and what you shipped.
Conclusion
Generating an SBOM in CI turns the question of whether you are affected by a new vulnerability into a fast query instead of a research project. Scan the built artifact rather than the source, key components by package URL in a standard format, bind the SBOM to the artifact digest and sign it, store it where you can query by component, and continuously match it against vulnerability feeds. Accept that the inventory is only as complete as the scanner, and treat gaps as unknown rather than safe.
Sources: SPDX specification, CycloneDX specification, SLSA supply-chain levels.
FAQ
- Should I generate the SBOM from source or from the built artifact?
- From the built artifact. The image or package you ship can contain dependencies that differ from the source lockfile because of build-time resolution. Scanning the artifact describes what you actually deploy, which is what you must account for.
- Does an SBOM make vulnerability scanning unnecessary?
- No. The SBOM is an inventory; scanning matches that inventory against a vulnerability feed. You need both: the SBOM says what is present, and the scanner plus feed says what in it is affected by a known issue.
Report a problem with this articleOpens a pre-filled issue on GitHub.