Skip to content

Commit 418cd8c

Browse files
committed
Set of github actions to generate sbom on change
1 parent 06c0857 commit 418cd8c

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Update the SBOM
2+
description: Updates the SBOM for the project
3+
inputs:
4+
sbom_in_path:
5+
description: The path of the input sbom file.
6+
default: ./sbom.json
7+
sbom_file_name:
8+
description: The name of the output sbom file.
9+
default: ./sbom.json
10+
container_image:
11+
description: "The container image to use"
12+
default: "artifactory.corp.mongodb.com/release-tools-container-registry-public-local/silkbomb:1.0"
13+
14+
runs:
15+
using: composite
16+
steps:
17+
- name: Updates the SBOM file and writes it to the release assets and s3 assets folders
18+
shell: bash
19+
env:
20+
SBOM_IN_PATH: ${{ inputs.sbom_in_path }}
21+
CONTAINER_IMAGE: ${{ inputs.container_image }}
22+
SBOM_FILE_NAME: ${{ inputs.sbom_file_name }}
23+
PURLS_FILE: "${{ github.workspace }}/purls.txt"
24+
run: |
25+
set -eu
26+
27+
LIBMONGOC_VERSION=$(cat ${PWD}/src/LIBMONGOC_VERSION_CURRENT | tr -d '[:space:]')
28+
LIBMONGOCRYPT_VERSION=$(cat ${PWD}/src/LIBMONGOCRYPT_VERSION_CURRENT | tr -d '[:space:]')
29+
30+
# Generate purls file from stored versions
31+
echo "pkg:github/mongodb/mongo-c-driver@${LIBMONGOC_VERSION}" > $PURLS_FILE
32+
echo "pkg:github/mongodb/libmongocrypt@${LIBMONGOCRYPT_VERSION}" >> $PURLS_FILE
33+
34+
# Use silkbomb to update the sbom.json file
35+
docker run --platform="linux/amd64" -i --rm -v $PWD:/pwd ${CONTAINER_IMAGE} \
36+
update --sbom-in /pwd/${SBOM_IN_PATH} --purls /pwd/purls.txt --sbom-out /pwd/${SBOM_FILE_NAME}
37+
38+
echo "Generating SBOM file done."

.github/workflows/sbom.yml

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
name: Post-Merge SBOM Update
2+
3+
on:
4+
push:
5+
branches:
6+
- v1.21
7+
paths:
8+
- 'src/libmongoc/**'
9+
- 'src/libmongocrypt/**'
10+
11+
workflow_dispatch:
12+
13+
permissions:
14+
contents: write
15+
pull-requests: write
16+
17+
jobs:
18+
sbom:
19+
name: Generate SBOM and Create PR
20+
runs-on: ubuntu-latest
21+
22+
concurrency:
23+
group: sbom-${{ github.ref }}
24+
cancel-in-progress: false
25+
env:
26+
SBOM_FILE: sbom.json
27+
steps:
28+
- name: Checkout repository (Base Branch)
29+
uses: actions/checkout@v4
30+
with:
31+
ref: ${{ github.event.pull_request.base.ref }}
32+
token: ${{ secrets.GITHUB_TOKEN }}
33+
34+
- name: Run SBOM Updater
35+
uses: ./.github/actions/sbom-update
36+
37+
- name: Check for Changes in sbom.json
38+
id: git_status
39+
run: |
40+
set -e
41+
42+
# Validate current SBOM is valid JSON
43+
if ! jq empty "$SBOM_FILE" 2>/dev/null; then
44+
echo "Error: $SBOM_FILE is not valid JSON"
45+
cat "$SBOM_FILE"
46+
exit 1
47+
fi
48+
49+
# JQ filter to normalize timestamps and serialNumber
50+
JQ_NORMALIZER='del(.serialNumber, .metadata.timestamp) | walk(if type == "object" and has("timestamp") then .timestamp = "NORMALIZED" else . end)'
51+
52+
# Check if the file exists in Git
53+
if ! git show HEAD:"$SBOM_FILE" > /dev/null 2>&1; then
54+
echo "File is new - marking as changed"
55+
echo "HAS_CHANGES=true" >> "$GITHUB_OUTPUT"
56+
exit 0
57+
fi
58+
59+
# Normalize both versions and compare
60+
NORMALIZED_OLD=$(git show HEAD:"$SBOM_FILE" | jq --sort-keys "$JQ_NORMALIZER")
61+
NORMALIZED_NEW=$(jq --sort-keys "$JQ_NORMALIZER" "$SBOM_FILE")
62+
63+
if [ "$NORMALIZED_OLD" = "$NORMALIZED_NEW" ]; then
64+
echo "No changes detected in $SBOM_FILE"
65+
echo "HAS_CHANGES=false" >> "$GITHUB_OUTPUT"
66+
else
67+
echo "Changes detected in $SBOM_FILE"
68+
echo "HAS_CHANGES=true" >> "$GITHUB_OUTPUT"
69+
fi
70+
71+
- name: Create Pull Request
72+
if: steps.git_status.outputs.HAS_CHANGES == 'true'
73+
uses: peter-evans/create-pull-request@b4733b9419fd47bbfa1807b15627e17cd70b5b22
74+
with:
75+
token: ${{ secrets.GITHUB_TOKEN }}
76+
commit-message: 'chore: Update SBOM after dependency changes'
77+
branch: auto-update-sbom-${{ github.run_id }}
78+
delete-branch: true
79+
title: 'chore: Update SBOM'
80+
body: |
81+
## Automated SBOM Update
82+
83+
This PR was automatically generated because dependency manifest files changed.
84+
85+
### Changes
86+
- Updated `sbom.json` to reflect current dependencies
87+
88+
### Verification
89+
The SBOM was generated using SilkBomb v1.0.
90+
91+
### Triggered by
92+
- Commit: ${{ github.sha }}
93+
- Workflow run: ${{ github.run_id }}
94+
95+
---
96+
_This PR was created automatically by the [SBOM workflow](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})_
97+
labels: |
98+
sbom
99+
automated
100+
dependencies

0 commit comments

Comments
 (0)