---
title: "Generating and Storing Image SBOMs Automatically"
date: "2026-11-18T18:30:00+07:00"
description: "Automating Software Bill of Materials (SBOM) generation using Syft, Trivy, and BuildKit, exporting in SPDX and CycloneDX formats, and storing in CI."
author: "AI Assistant"
image: "https://blog.redlinesoft.net/images/post/generating-storing-image-sboms-automatically.jpg"
alt: "Workflow diagram demonstrating automated CI step compiling container image and publishing SPDX/CycloneDX SBOM"
tags: ['docker', 'sbom', 'syft', 'spdx']
---

A **Software Bill of Materials (SBOM)** is an inventory of all open-source packages, system libraries, language dependencies, and licenses embedded inside a container image. 

With executive security mandates (like US Executive Order 14028) and open-source compliance frameworks requiring SBOM disclosures, generating SBOMs manually is no longer feasible.

In this tutorial, we will learn how to generate SBOMs automatically using **Syft**, **Trivy**, and **Docker BuildKit**, export them in standard **SPDX** and **CycloneDX** formats, and automate publishing in CI/CD pipelines.

## Standard SBOM Formats Compared

Two open-standard formats dominate container SBOM reporting:

```text
+-----------------------------------------------------------------------------------+
| 1. SPDX (Software Package Data Exchange - ISO/IEC 5962:2021)                     |
|    - Maintained by the Linux Foundation.                                          |
|    - Focuses heavily on licensing compliance, copyright claims, & system packages. |
+-----------------------------------------------------------------------------------+
| 2. CycloneDX                                                                      |
|    - Maintained by OWASP Foundation.                                              |
|    - Focuses heavily on application dependencies, vulnerability tracking, & VEX. |
+-----------------------------------------------------------------------------------+
```

Both formats express data in structured JSON or XML, allowing automated security tools to parse package names and exact version strings.

## Method 1: Generating SBOMs with Syft

**Syft** (developed by Anchore) is the industry standard CLI tool for generating container image SBOMs.

### Generating SPDX & CycloneDX JSON

```bash
# Generate SPDX JSON format
syft myregistry.com/myorg/app:v1.0.0 -o spdx-json=sbom.spdx.json

# Generate CycloneDX JSON format
syft myregistry.com/myorg/app:v1.0.0 -o cyclonedx-json=sbom.cdx.json

# Output clean human-readable text summary to terminal
syft myregistry.com/myorg/app:v1.0.0
```

Sample Syft Terminal Summary:
```text
NAME                    VERSION         TYPE
alpine-baselayout      3.4.3-r0        apk
busybox                 1.36.1-r2       apk
ca-certificates-bundle  20230506-r0     apk
express                 4.18.2          npm
lodash                  4.17.21         npm
```

## Method 2: Generating SBOMs with Trivy

**Trivy** (developed by Aqua Security) can generate SBOMs in addition to vulnerability scanning:

```bash
trivy image --format spdx-json --output sbom.spdx.json myregistry.com/myorg/app:v1.0.0
```

## Method 3: Automated BuildKit Native Generation

Docker Buildx can generate and attach an in-toto SBOM attestation directly during the build step:

```bash
docker buildx build \
  --sbom=true \
  --sbom-generator=image \
  -t myregistry.com/myorg/app:v1.0.0 \
  --push .
```

## Automating SBOM Generation in GitHub Actions

Below is a production GitHub Actions workflow that builds a container image, generates an SPDX SBOM via Syft, uploads the SBOM as a GitHub build artifact, and attaches it to the container registry:

```yaml
name: Automated Build & SBOM Generation

on:
  push:
    branches: [ "main" ]

jobs:
  build-and-sbom:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Log in to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and Push Docker Image
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: ghcr.io/${{ github.repository }}:${{ github.sha }}

      - name: Install Syft
        uses: anchore/scan-action/download-syft@v3

      - name: Generate SPDX SBOM
        run: |
          syft ghcr.io/${{ github.repository }}:${{ github.sha }} \
            -o spdx-json=sbom.spdx.json

      - name: Upload SBOM as CI Artifact
        uses: actions/upload-artifact@v3
        with:
          name: sbom-spdx-${{ github.sha }}
          path: sbom.spdx.json
```

## Consuming SBOMs in Vulnerability Scanners

Once an SBOM JSON is saved, you can run offline vulnerability scans against the SBOM file **without needing access to the original container image or registry!**

```bash
# Scan saved SBOM for CVE vulnerabilities using Grype
grype sbom:./sbom.spdx.json
```

## Summary Checklist

| Action | Recommended Tool |
| :--- | :--- |
| **CLI Generation** | `syft <image> -o spdx-json` |
| **Build Integration** | `docker buildx build --sbom=true` |
| **Offline CVE Scan** | `grype sbom:./sbom.spdx.json` |
| **Registry Storage** | `cosign attach sbom --sbom sbom.json <image>` |

Automating SBOM generation in CI pipelines guarantees transparency, speeds up CVE triage, and ensures compliance with enterprise software supply chain security standards.
