Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/actions/sbom-update/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Generate SBOM
description: Generates CycloneDX SBOM using cdxgen
inputs:
output-file:
description: "Output filename for the SBOM"
required: false
default: "sbom.json"

runs:
using: composite
steps:
- name: Generate SBOM
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move the contents of this action into a standalone script? That's easier to maintain and run locally than a Github action.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd keep it as a composite actions, since it is native to github - clearer error logs and we can use success state triggers (like - if: steps.generate-sbom.outcome == 'success'), and unified across repos - close to the other repo approaches compared to script.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can keep the composite action, but instead of having multiple steps in the action, we could just call a single bash script.

Would you mind making this change?

shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
echo "Generating SBOM for 'node' project..."
npx @cyclonedx/cyclonedx-npm --package-lock-only --omit dev --output-file sbom.json --output-format json --spec-version 1.5

- name: Validate SBOM
shell: bash
run: |
if [ ! -f "${{ inputs.output-file }}" ]; then
echo "Error: SBOM file not found"
exit 1
fi

echo "SBOM file validated: ${{ inputs.output-file }}"
19 changes: 19 additions & 0 deletions .github/actions/setup-sbom/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Setup PHP SBOM
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it was a copy-paste from the PHP implementation?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was replicating structure, but missed to update the text in all places. Removed since no setup needed.

description: Sets up environment for generating SBOM in PHP projects
inputs:
working-directory:
description: "The directory where composer.json is located"
required: false
default: "."

runs:
using: composite
steps:
- name: Setup Node.js (for cdxgen)
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install dependencies
shell: bash
run: npm ci
19 changes: 19 additions & 0 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Setup Node SBOM
description: Sets up environment for generating SBOM in Node.js projects
inputs:
working-directory:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This action shouldn't be necessary because:

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed as we use npx and direct download

description: "The directory where package.json is located"
required: false
default: "."

runs:
using: composite
steps:
- name: Setup Node.js (for cdxgen)
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install cdxgen
shell: bash
run: npm install -g @cyclonedx/cdxgen
115 changes: 115 additions & 0 deletions .github/workflows/sbom.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
name: Post-Merge SBOM Update

on:
push:
branches:
- main
paths:
- 'package.json'
- 'package-lock.json'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To explicitly avoid workflow run on sbom changes.

Suggested change
- 'package-lock.json'
- 'package-lock.json'
paths-ignore:
- 'sbom.json'

workflow_dispatch:
env:
SBOM_FILE: "sbom.json"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
env:
SBOM_FILE: "sbom.json"

This won't ever change, I think, so we should be able to remove it.

permissions:
contents: write
pull-requests: write
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pull-requests: write

I think we can remove this permission now that we're committing to main, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed


jobs:
sbom:
name: Generate SBOM and Create PR
runs-on: ubuntu-latest

concurrency:
group: sbom-${{ github.ref }}
cancel-in-progress: false

steps:
- name: Checkout repository (Base Branch)
uses: actions/checkout@v5
with:
ref: ${{ github.event.pull_request.base.ref || github.ref }}
token: ${{ secrets.GITHUB_TOKEN }}

- name: Install Node and dependencies
uses: mongodb-labs/drivers-github-tools/node/setup@v3
with:
ignore_install_scripts: false

- name: Load version and package info
uses: mongodb-labs/drivers-github-tools/node/get_version_info@v3
with:
npm_package_name: mongodb
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- name: Load version and package info
uses: mongodb-labs/drivers-github-tools/node/get_version_info@v3
with:
npm_package_name: mongodb

This shouldn't be necessary

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't remember why I've added it, but I'll trust you and remove)


- name: Generate/Update package-lock.json
run: |
echo "Resolving dependencies and generating package-lock.json..."
npm install --package-lock-only
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've already run npm install above in mongodb-labs/drivers-github-tools/node/setup@v3. Is it necessary to only install from package-lock? If so, we might want to remove the call to the above action.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

echo "package-lock.json generated with resolved versions"

- name: Setup SBOM environment
uses: ./.github/actions/setup-sbom

- name: Generate SBOM
uses: ./.github/actions/sbom-update
with:
output-file: ${SBOM_FILE}

- name: Check for Changes in sbom.json
id: git_status
run: |
# Filter to remove/normalize serialNumber and timestamp fields
JQ_NORMALIZER='del(.serialNumber) | del(.metadata.timestamp) | walk(if type == "object" and .timestamp then .timestamp = "TIMESTAMP_NORMALIZED" else . end)'

# Check if the base file exists in Git (to prevent errors on first commit)
if ! git show HEAD:$SBOM_FILE > /dev/null 2>&1; then
echo "HAS_CHANGES=true" >> $GITHUB_OUTPUT
exit 0
fi

# Compare the normalized committed version vs. the normalized current version
if diff -q \
<(git show HEAD:$SBOM_FILE | jq -r "$JQ_NORMALIZER") \
<(cat $SBOM_FILE | jq -r "$JQ_NORMALIZER"); then

echo "HAS_CHANGES=false" >> $GITHUB_OUTPUT
echo "No changes detected in sbom.json"
else
echo "HAS_CHANGES=true" >> $GITHUB_OUTPUT
echo "Changes detected in sbom.json"
fi

- name: Create Pull Request
if: steps.git_status.outputs.HAS_CHANGES == 'true'
uses: peter-evans/create-pull-request@v6
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: 'chore: update SBOM after dependency changes'
add-paths: |
sbom.json
branch: auto-update-sbom-${{ github.run_id }}
delete-branch: true
title: 'chore: Update SBOM'
body: |
## Automated SBOM Update

This PR was automatically generated because package files changed.

### Environment
- Node.js version: ${{ steps.versions.outputs.node-version }}

### Changes
- Updated `sbom.json` to reflect current dependencies

### Verification
The SBOM was generated using CycloneDX NPM.

### Triggered by
- Commit: ${{ github.sha }}
- Workflow run: ${{ github.run_id }}

---
_This PR was created automatically by the [SBOM workflow](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})_
labels: |
sbom
automated
dependencies