-
Notifications
You must be signed in to change notification settings - Fork 11
96 lines (82 loc) · 4.08 KB
/
Copy pathsbom.yml
File metadata and controls
96 lines (82 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
name: Generate SBOM
# Generates and attaches an SPDX SBOM to the GitHub release for the pushed
# version tag. Runs only on vX.Y.Z tag pushes; the SBOM asset is uploaded
# (or replaced) via the GitHub Releases API. Idempotent: if the asset
# already exists it is deleted and re-uploaded with fresh scan output.
on:
push:
tags:
- "v*"
permissions: {}
concurrency:
group: sbom-${{ github.ref }}
cancel-in-progress: false
jobs:
generate-sbom:
name: Generate and Attach SBOM
runs-on: ubuntu-latest
timeout-minutes: 30
permissions:
contents: write # upload SBOM asset to the GitHub release
steps:
- name: Checkout code
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
with:
persist-credentials: false
- name: Install Syft (pinned version with checksum verification)
env:
SYFT_VERSION: "1.41.0"
run: |
curl -sSfL -o syft.tar.gz \
"https://github.com/anchore/syft/releases/download/v${SYFT_VERSION}/syft_${SYFT_VERSION}_linux_amd64.tar.gz"
curl -sSfL -o syft_checksums.txt \
"https://github.com/anchore/syft/releases/download/v${SYFT_VERSION}/syft_${SYFT_VERSION}_checksums.txt"
# Verify the downloaded archive against the published checksum for this exact asset.
EXPECTED=$(awk '$2 == "syft_'${SYFT_VERSION}'_linux_amd64.tar.gz" {print $1}' syft_checksums.txt)
echo "${EXPECTED} syft.tar.gz" | sha256sum -c -
tar -xzf syft.tar.gz syft
sudo mv syft /usr/local/bin/syft
rm -f syft.tar.gz syft_checksums.txt
- name: Generate SBOM (SPDX format)
run: |
syft . -o spdx-json=sbom.spdx.json
- name: Find release ID for the pushed tag
id: release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
TAG_NAME="${GITHUB_REF#refs/tags/}"
RELEASE_ID=$(curl -s -H "Authorization: token ${GITHUB_TOKEN}" \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/releases/tags/${TAG_NAME}" \
| jq -r '.id')
if [ -z "${RELEASE_ID}" ] || [ "${RELEASE_ID}" = "null" ]; then
echo "release_id=" >> "${GITHUB_OUTPUT}"
else
echo "release_id=${RELEASE_ID}" >> "${GITHUB_OUTPUT}"
fi
- name: Check for existing SBOM asset and upload
if: steps.release.outputs.release_id != '' && steps.release.outputs.release_id != 'null'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RELEASE_ID: ${{ steps.release.outputs.release_id }}
run: |
# Check if an SBOM asset with this name already exists in the release
ASSET_ID=$(curl -s -H "Authorization: token ${GITHUB_TOKEN}" \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/releases/${RELEASE_ID}/assets" \
| jq -r ".[] | select(.name == \"sbom.spdx.json\") | .id")
if [ -n "${ASSET_ID}" ] && [ "${ASSET_ID}" != "null" ]; then
# Delete existing SBOM asset so we can re-upload the fresh scan
curl -s -X DELETE -H "Authorization: token ${GITHUB_TOKEN}" \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/releases/assets/${ASSET_ID}" > /dev/null
echo "Deleted existing sbom.spdx.json asset"
fi
# Upload the SBOM as a release asset
curl -s -X POST -H "Authorization: token ${GITHUB_TOKEN}" \
-H "Content-Type: application/spdx+json" \
--data-binary @sbom.spdx.json \
"https://uploads.github.com/repos/${GITHUB_REPOSITORY}/releases/${RELEASE_ID}/assets" > /dev/null
echo "Uploaded sbom.spdx.json to release ${RELEASE_ID}"
- name: Notify if no release found for tag
if: steps.release.outputs.release_id == '' || steps.release.outputs.release_id == 'null'
run: |
echo "::notice::No GitHub Release found for tag ${GITHUB_REF#refs/tags/}. The SBOM was generated at ./sbom.spdx.json but was not attached to a release. Ensure the tag was created by the release-please process or manually create a release draft."