Skip to content

Commit b6cf782

Browse files
committed
feat: workflow to update sbom only if different than the latest one
1 parent 8b1bcaa commit b6cf782

File tree

1 file changed

+32
-55
lines changed

1 file changed

+32
-55
lines changed

.github/workflows/sbom.yml

Lines changed: 32 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ on:
44
workflow_dispatch:
55
push:
66
branches:
7-
- master
7+
- feature/*
8+
- fix/*
9+
- improvement/*
10+
- release/*
11+
- technical/*
12+
813

914
permissions:
1015
contents: write
1116

1217
jobs:
1318
sbom:
14-
# Skip if the job was triggered by the SBOM commit
19+
# Skip if the job was triggered by the SBOM commit in the latest push.
1520
if: "!contains(github.event.head_commit.message, 'SBOM updated')"
1621
runs-on: ubuntu-latest
1722

@@ -20,32 +25,10 @@ jobs:
2025
- name: Checkout repository
2126
uses: actions/checkout@v4
2227
with:
23-
ssh-key: ${{ secrets.DEPLOYMENT_SSH_KEY_SBOM }}
28+
# Parent commit to compare
29+
fetch-depth: 2
2430
persist-credentials: false
2531

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
4932

5033
# Cache Gradle dependencies to speed up future builds
5134
- name: Cache Gradle dependencies
@@ -78,17 +61,11 @@ jobs:
7861
- name: Install jq
7962
run: sudo apt-get update && sudo apt-get install -y jq
8063

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
64+
# Creates script to normalize SBOM files to compare
65+
- name: Normalization script
8766
run: |
88-
# Normalize SBOM JSON by removing non-essential fields and sorting arrays for consistent diff
8967
cat <<'EOF' > normalize-sbom.sh
9068
#!/bin/bash
91-
9269
jq -S '
9370
del(.serialNumber, .timestamp, .metadata.timestamp)
9471
| .components |= (if type=="array" then sort_by(.["bom-ref"] // "") else . end)
@@ -97,32 +74,32 @@ jobs:
9774
EOF
9875
chmod +x normalize-sbom.sh
9976
100-
# Extract & normalize both SBOMs
101-
- name: Extract and normalize both SBOMs
77+
# Compares with the HEAD to check if there are changes
78+
- name: Compare with previous SBOM
79+
id: compare
10280
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
81+
# Try HEAD first to compare with previous commit's sbom (HEAD~1)
82+
git show HEAD~1:sbom.json > sbom_prev.json 2>/dev/null || echo '{}' > sbom_prev.json
10683
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
84+
./normalize-sbom.sh sbom_prev.json sbom_prev_normalized.json
85+
./normalize-sbom.sh sbom.json sbom_current_normalized.json
86+
87+
if diff -q sbom_prev_normalized.json sbom_current_normalized.json; then
11288
echo "no_changes=true" >> $GITHUB_OUTPUT
113-
echo "NO Differences found in SBOM"
89+
echo "No changes in SBOM"
11490
else
11591
echo "no_changes=false" >> $GITHUB_OUTPUT
116-
echo "Differences found in SBOM:"
117-
cat sbom_diff.txt
92+
echo "Differences in SBOM"
93+
diff sbom_prev_normalized.json sbom_current_normalized.json || true
11894
fi
11995
12096
# 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
97+
- name: Commit and push updated SBOM
98+
if: steps.compare.outputs.no_changes == 'false'
99+
uses: GuillaumeFalourd/git-commit-push@v1.3
100+
with:
101+
commit_message: "docs: SBOM updated"
102+
files: sbom.json
103+
author_email: devops@owncloud.com
104+
author_name: ownClouders
105+
access_token: ${{ secrets.GH_PAT }}

0 commit comments

Comments
 (0)