Skip to content

Commit 43beb11

Browse files
committed
feat: workflow to update sbom only if different than the latest one
1 parent f084a3a commit 43beb11

File tree

1 file changed

+30
-57
lines changed

1 file changed

+30
-57
lines changed

.github/workflows/sbom.yml

Lines changed: 30 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: SBOM
22

33
on:
44
workflow_dispatch:
5-
push:
5+
pull_request:
66
branches:
77
- master
88

@@ -11,41 +11,20 @@ permissions:
1111

1212
jobs:
1313
sbom:
14-
# Skip if the job was triggered by the SBOM commit
15-
if: "!contains(github.event.head_commit.message, 'SBOM updated')"
14+
# Skip if the job was triggered by the SBOM commit in the latest push. If PR, go ahead.
15+
if: github.event_name == 'push' && !contains(github.event.head_commit.message, 'SBOM updated') || github.event_name == 'pull_request'
1616
runs-on: ubuntu-latest
1717

1818
steps:
1919
# Checkout the full repository history (required to access origin/master)
2020
- name: Checkout repository
2121
uses: actions/checkout@v4
2222
with:
23-
ssh-key: ${{ secrets.DEPLOYMENT_SSH_KEY_SBOM }}
23+
# Parent commit to compare
24+
fetch-depth: 2
2425
persist-credentials: false
25-
26-
# Start SSH agent and add the SSH key to authenticate Git operations
27-
- name: Start SSH agent and add key
28-
run: |
29-
mkdir -p ~/.ssh
30-
echo "${{ secrets.DEPLOYMENT_SSH_KEY_SBOM }}" > ~/.ssh/id_rsa
31-
chmod 600 ~/.ssh/id_rsa
32-
# Start the SSH agent
33-
eval "$(ssh-agent -s)"
34-
35-
# Add the private key to the SSH agent
36-
ssh-add ~/.ssh/id_rsa
37-
38-
# Add GitHub to known hosts to prevent authenticity prompts
39-
ssh-keyscan github.com >> ~/.ssh/known_hosts
40-
41-
# Check the SSH connection to GitHub (ignore failure)
42-
ssh -o StrictHostKeyChecking=no -T git@github.com || true
43-
44-
# Dry-run push to confirm SSH authentication is working
45-
- name: Check SSH push permissions (dry-run)
46-
run: |
47-
git remote set-url origin git@github.com:${{ github.repository }}.git
48-
git push --dry-run origin HEAD
26+
# To checkout the latest commit of the PR or the latest event's commit
27+
ref: ${{ github.event.pull_request.head.sha || github.sha }}
4928

5029
# Cache Gradle dependencies to speed up future builds
5130
- name: Cache Gradle dependencies
@@ -78,17 +57,11 @@ jobs:
7857
- name: Install jq
7958
run: sudo apt-get update && sudo apt-get install -y jq
8059

81-
# Fetch the master branch to compare with current SBOM
82-
- name: Fetch origin/master
83-
run: git fetch origin master
84-
85-
# Prepare common JQ filter in a script
86-
- name: Prepare normalize script
60+
# Creates script to normalize SBOM files to compare
61+
- name: Normalization script
8762
run: |
88-
# Normalize SBOM JSON by removing non-essential fields and sorting arrays for consistent diff
8963
cat <<'EOF' > normalize-sbom.sh
9064
#!/bin/bash
91-
9265
jq -S '
9366
del(.serialNumber, .timestamp, .metadata.timestamp)
9467
| .components |= (if type=="array" then sort_by(.["bom-ref"] // "") else . end)
@@ -97,32 +70,32 @@ jobs:
9770
EOF
9871
chmod +x normalize-sbom.sh
9972
100-
# Extract & normalize both SBOMs
101-
- name: Extract and normalize both SBOMs
73+
# Compares with the HEAD to check if there are changes
74+
- name: Compare with previous SBOM
75+
id: compare
10276
run: |
103-
git show origin/master:sbom.json > sbom_master.json || echo '{}' > sbom_master.json
104-
./normalize-sbom.sh sbom_master.json sbom_master_normalized.json
105-
./normalize-sbom.sh sbom.json sbom_normalized.json
77+
# Try HEAD first to compare with previous commit's sbom (HEAD~1)
78+
git show HEAD~1:sbom.json > sbom_prev.json 2>/dev/null || echo '{}' > sbom_prev.json
10679
107-
# Compare normalized SBOMs
108-
- name: Compare SBOMs and show diff
109-
id: diff_sbom
110-
run: |
111-
if diff -u sbom_master_normalized.json sbom_normalized.json > sbom_diff.txt; then
80+
./normalize-sbom.sh sbom_prev.json sbom_prev_normalized.json
81+
./normalize-sbom.sh sbom.json sbom_current_normalized.json
82+
83+
if diff -q sbom_prev_normalized.json sbom_current_normalized.json; then
11284
echo "no_changes=true" >> $GITHUB_OUTPUT
113-
echo "NO Differences found in SBOM"
85+
echo "No changes in SBOM"
11486
else
11587
echo "no_changes=false" >> $GITHUB_OUTPUT
116-
echo "Differences found in SBOM:"
117-
cat sbom_diff.txt
88+
echo "Differences in SBOM"
89+
diff sbom_prev_normalized.json sbom_current_normalized.json || true
11890
fi
11991
12092
# Commit the SBOM file only if it differs from master to avoid unnecessary commits
121-
- name: Commit and push SBOM over SSH
122-
if: steps.diff_sbom.outputs.no_changes == 'false'
123-
run: |
124-
git config user.email "devops@owncloud.com"
125-
git config user.name "ownClouders"
126-
git add sbom.json
127-
git commit -m "docs: SBOM updated"
128-
git push origin master
93+
- name: Commit and push updated SBOM
94+
if: steps.compare.outputs.no_changes == 'false'
95+
uses: GuillaumeFalourd/git-commit-push@v1.3
96+
with:
97+
commit_message: "docs: SBOM updated"
98+
files: sbom.json
99+
author_email: devops@owncloud.com
100+
author_name: ownClouders
101+
access_token: ${{ secrets.GH_PAT }}

0 commit comments

Comments
 (0)