Skip to content

Commit 81dba72

Browse files
authored
fix(gh-cache): Get remote changes before pushing, to avoid concurrent write failures (#339)
If two concurrent jobs attempt to push to the `gh-cache` branch at the same time, `git` fails with an error like this: ``` [gh-cache] Save artifact: cached_findings-main.json 1 file changed, 1 insertion(+), 1 deletion(-) Committed 'cached_findings-main.json' to 'gh-cache' branch To https://github.com/github-community-projects/continuous-ai-for-accessibility-scanner ! [rejected] gh-cache -> gh-cache (fetch first) error: failed to push some refs to 'https://github.com/github-community-projects/continuous-ai-for-accessibility-scanner' hint: Updates were rejected because the remote contains work that you do not hint: have locally. This is usually caused by another repository pushing to hint: the same ref. If you want to integrate the remote changes, use hint: 'git pull' before pushing again. ``` This happens even in cases where the remote’s changes and the local changes don’t conflict, e.g. if two different files were updated. This PR fixes the issue by fetching and rebasing[^1] before pushing. [^1]: Unless the `gh-cache` branch doesn’t existing on the remote. If this is the case, the commands are skipped, because a. they are unnecessary and b. they would fail.
2 parents 3188221 + af696f7 commit 81dba72

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

.github/actions/gh-cache/save/action.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ runs:
4545
token: ${{ inputs.token }}
4646
fetch-depth: 0
4747
path: .gh-cache-${{ github.run_id }}
48-
show-progress: 'false'
48+
show-progress: "false"
4949

5050
- name: Switch to gh-cache branch (or create orphan)
5151
shell: bash
@@ -79,6 +79,10 @@ runs:
7979
git commit -m "$(printf 'Save artifact: %s' "${{ inputs.path }}")" \
8080
&& echo "Committed '${{ inputs.path }}' to 'gh-cache' branch" \
8181
|| echo "No changes to commit"
82+
if git ls-remote --exit-code --heads origin gh-cache >/dev/null; then
83+
git fetch origin gh-cache
84+
git rebase origin/gh-cache
85+
fi
8286
git push origin gh-cache
8387
else
8488
echo "'${{ inputs.path }}' does not exist"

0 commit comments

Comments
 (0)