@@ -2,14 +2,14 @@ name: SBOM
22
33on :
44 workflow_dispatch :
5+ inputs :
6+ target_branch :
7+ type : string
8+ required : false
9+ default : master
510 push :
611 branches :
7- - feature/*
8- - fix/*
9- - improvement/*
10- - release/*
11- - technical/*
12- - ' dependabot/**'
12+ - master
1313
1414# Cancels other executions in the same branch
1515concurrency :
@@ -21,20 +21,19 @@ permissions:
2121
2222jobs :
2323 sbom :
24- # Skip if the job was triggered by the SBOM commit or a merge commit in the latest push.
25- if : " !contains(github.event.head_commit.message, 'Merge pull request') && !contains(github.event.head_commit.message, 'SBOM updated')"
2624 runs-on : ubuntu-latest
25+ env :
26+ SOURCE_BRANCH : chore/sbom-update
27+ TARGET_BRANCH : ${{ github.event.inputs.target_branch || 'master' }}
2728
2829 steps :
2930 # Checkout the repository
3031 - name : Checkout repository
3132 uses : actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
3233 with :
33- # Parent commit to compare
3434 fetch-depth : 2
3535 persist-credentials : false
3636
37-
3837 # Cache Gradle dependencies to speed up future builds
3938 - name : Cache Gradle dependencies
4039 uses : actions/cache@668228422ae6a00e4ad889ee87cd7109ec5666a7 # v5.0.4
@@ -79,12 +78,28 @@ jobs:
7978 EOF
8079 chmod +x normalize-sbom.sh
8180
82- # Compares with the HEAD to check if there are changes
83- - name : Compare with previous SBOM
81+ # Compare with the SBOM update branch, or master as fallback
82+ - name : Compare with previous SBOM in branch or master as fallback
8483 id : compare
8584 run : |
86- # Try HEAD first to compare with previous commit's sbom (HEAD~1)
87- git show HEAD~1:sbom.json > sbom_prev.json 2>/dev/null || echo '{}' > sbom_prev.json
85+ FALLBACK_BRANCH="master"
86+
87+ echo "Checking whether branch $SOURCE_BRANCH exists in origin..."
88+
89+ # If source branch exists, fetch it and set as previous sbom
90+ if git ls-remote --exit-code --heads origin "$SOURCE_BRANCH"; then
91+ echo "Remote branch found: $SOURCE_BRANCH"
92+ git fetch origin "refs/heads/$SOURCE_BRANCH:refs/remotes/origin/$SOURCE_BRANCH" --depth=1
93+ PREVIOUS_SBOM_REF="origin/$SOURCE_BRANCH"
94+ echo "Using sbom.json from $PREVIOUS_SBOM_REF"
95+ # Use the fallback branch
96+ else
97+ echo "Remote branch not found: $SOURCE_BRANCH"
98+ PREVIOUS_SBOM_REF="origin/$FALLBACK_BRANCH"
99+ echo "Using sbom.json from fallback branch: $PREVIOUS_SBOM_REF"
100+ fi
101+
102+ git show "$PREVIOUS_SBOM_REF:sbom.json" > sbom_prev.json
88103
89104 ./normalize-sbom.sh sbom_prev.json sbom_prev_normalized.json
90105 ./normalize-sbom.sh sbom.json sbom_current_normalized.json
@@ -100,19 +115,24 @@ jobs:
100115
101116 # Generate a token to perform the commit in the next step
102117 - name : Generate GitHub App token
118+ if : steps.compare.outputs.no_changes == 'false'
103119 id : app-token
104120 uses : actions/create-github-app-token@1b10c78c7865c340bc4f6099eb2f838309f1e8c3 # v3.1.1
105121 with :
106122 app-id : ${{ secrets.SBOM_APP_ID }}
107123 private-key : ${{ secrets.SBOM_APP_PRIVATE_KEY }}
108124
109- # Commit the SBOM file only if it differs from master to avoid unnecessary commits
110- - name : Commit and push updated SBOM
125+ # Create a branch with latest SBOM changes only if there are changes
126+ - name : Create or update SBOM PR
111127 if : steps.compare.outputs.no_changes == 'false'
112- uses : GuillaumeFalourd/git-commit-push@205c043bca2f932f7a48a28a8d619ba30eb84baf # v1.3
128+ uses : peter-evans/create-pull-request@c0f553fe549906ede9cf27b5156039d195d2ece0 # v8.1.0
113129 with :
114- commit_message : " docs: SBOM updated"
115- files : sbom.json
116- email : devops@owncloud.com
117- name : ownClouders
118- access_token : ${{ steps.app-token.outputs.token }}
130+ add-paths : sbom.json
131+ token : ${{ steps.app-token.outputs.token }}
132+ branch : ${{ env.SOURCE_BRANCH }}
133+ base : ${{ env.TARGET_BRANCH }}
134+ commit-message : " chore: update SBOM"
135+ title : " chore: update sbom.json"
136+ body : " Automated SBOM update. This pull request is updated on each push to `master` or manual dispatch — merging it will close it and a fresh one will be opened on the next change."
137+ delete-branch : true
138+ sign-commits : true
0 commit comments