Repository navigation
Expand file tree
/
Copy pathaction.yml
More file actions
61 lines (58 loc) · 2.3 KB
/
Copy pathaction.yml
File metadata and controls
61 lines (58 loc) · 2.3 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
name: Commit changes
description: >-
Commits what changed under the given paths (screenshots, baselines, generated files) as
github-actions[bot] and pushes it to the branch the workflow runs on. Nothing changed: nothing
is committed. The job needs contents: write; a push with the GITHUB_TOKEN starts no other workflow.
It pushes with whatever actions/checkout set up, e.g. its ssh-key for a deploy key.
inputs:
paths:
description: What is committed, one path or glob per line
required: true
message:
description: Commit message
default: Update screenshots
outputs:
changed:
description: "true when something was committed"
value: ${{ steps.commit.outputs.changed }}
runs:
using: composite
steps:
- id: commit
shell: bash
env:
PATHS: ${{ inputs.paths }}
MESSAGE: ${{ inputs.message }}
run: |
set -euo pipefail
if [[ "$GITHUB_REF" != refs/heads/* ]]; then
echo "::error::Run this on a branch, not on $GITHUB_REF"
exit 1
fi
# In a container the workspace belongs to another user
git config --global --add safe.directory "$GITHUB_WORKSPACE"
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
mapfile -t pathspecs < <(printf '%s\n' "$PATHS" | sed '/^[[:space:]]*$/d')
git add -- "${pathspecs[@]}"
if git diff --cached --quiet; then
echo "Nothing changed." | tee -a "$GITHUB_STEP_SUMMARY"
echo "changed=false" >> "$GITHUB_OUTPUT"
exit 0
fi
{ echo "Committed:"; git diff --cached --name-only | sed 's/^/- /'; } >> "$GITHUB_STEP_SUMMARY"
git commit -m "$MESSAGE"
# The branch may have moved during the run (a push, another run): put the commit on top
# of it and try again; a conflict in the same files stops here
branch="${GITHUB_REF#refs/heads/}"
for attempt in 1 2 3 4 5; do
git push origin "HEAD:$GITHUB_REF" && break
if (( attempt == 5 )); then
echo "::error::Could not push to $branch"
exit 1
fi
sleep $((attempt * 2))
git fetch origin "$branch"
git rebase "origin/$branch"
done
echo "changed=true" >> "$GITHUB_OUTPUT"