-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
246 lines (211 loc) · 8.73 KB
/
action.yml
File metadata and controls
246 lines (211 loc) · 8.73 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
name: 'Skyhook Git Sync and Commit'
description: 'Safely sync, commit, and push changes with automatic rebase and retry logic'
author: 'Skyhook'
branding:
icon: 'git-commit'
color: 'purple'
inputs:
path:
description: 'Working directory for git operations'
required: false
default: '.'
commit_message:
description: 'Commit message'
required: true
commit_user_name:
description: 'Git committer name'
required: false
default: 'github-actions[bot]'
commit_user_email:
description: 'Git committer email'
required: false
default: '41898282+github-actions[bot]@users.noreply.github.com'
file_pattern:
description: 'Files to stage for commit (e.g., ".", "*.yml", "path/to/files/*")'
required: false
default: '.'
max_retries:
description: 'Maximum number of push retry attempts (handles concurrent pushes)'
required: false
default: '12'
outputs:
committed:
description: 'Whether a commit was created (true/false)'
value: ${{ steps.commit.outputs.committed }}
commit_sha:
description: 'SHA of the created commit (empty if no commit)'
value: ${{ steps.commit.outputs.commit_sha }}
runs:
using: 'composite'
steps:
- name: Validate inputs
shell: bash
run: |
set -euo pipefail
if [[ ! -d "${{ inputs.path }}" ]]; then
echo "::error::Working directory not found: ${{ inputs.path }}"
exit 1
fi
if [[ -z "${{ inputs.commit_message }}" ]]; then
echo "::error::commit_message is required"
exit 1
fi
# Validate max_retries is a number
if ! [[ "${{ inputs.max_retries }}" =~ ^[0-9]+$ ]]; then
echo "::error::max_retries must be a positive integer"
exit 1
fi
- name: Sync and commit changes
id: commit
shell: bash
working-directory: ${{ inputs.path }}
env:
COMMIT_MESSAGE: ${{ inputs.commit_message }}
COMMIT_USER_NAME: ${{ inputs.commit_user_name }}
COMMIT_USER_EMAIL: ${{ inputs.commit_user_email }}
FILE_PATTERN: ${{ inputs.file_pattern }}
MAX_RETRIES: ${{ inputs.max_retries }}
run: |
set -euo pipefail
echo "🔄 Starting git sync and commit process..."
# Configure git user for this operation only
git config user.name "$COMMIT_USER_NAME"
git config user.email "$COMMIT_USER_EMAIL"
# Stash any uncommitted changes (including untracked files)
echo "📦 Stashing current changes..."
STASH_RESULT=0
git stash push -u -m "temp-stashed-changes" || STASH_RESULT=$?
if [ $STASH_RESULT -ne 0 ]; then
# Check if it's because there's nothing to stash (expected) or a real error
if git diff --quiet && git diff --cached --quiet && [ -z "$(git ls-files --others --exclude-standard)" ]; then
echo "ℹ️ Nothing to stash"
STASH_RESULT=0
else
echo "::error::Failed to stash changes"
exit 1
fi
fi
# Pull latest changes with rebase
echo "⬇️ Pulling latest changes with rebase..."
git pull --rebase origin "$(git branch --show-current)"
# Pop the stash to restore changes (if we stashed anything)
echo "📂 Restoring stashed changes..."
if [ "$(git stash list | grep -c 'temp-stashed-changes')" -gt 0 ]; then
if ! git stash pop; then
echo "⚠️ Merge conflicts detected during stash pop"
# Get list of conflicted files
CONFLICTED_FILES=$(git diff --name-only --diff-filter=U)
if [ -n "$CONFLICTED_FILES" ]; then
echo "🔧 Resolving conflicts by accepting stashed changes..."
# For each conflicted file, accept the stashed version
# In stash pop context: --theirs = stashed changes (what we want)
RESOLUTION_FAILED=0
while IFS= read -r file; do
if [ -n "$file" ]; then
echo " - Resolving: $file"
if ! git checkout --theirs "$file"; then
echo "::error::Failed to resolve conflict in $file"
RESOLUTION_FAILED=1
break
fi
fi
done <<< "$CONFLICTED_FILES"
if [ $RESOLUTION_FAILED -ne 0 ]; then
# Drop the stash to clean up
git stash drop
exit 1
fi
# Drop the stash since we've successfully applied it
git stash drop
echo "✅ Conflicts resolved"
else
echo "ℹ️ No actual conflicts found, continuing..."
# Still need to drop the stash
git stash drop
fi
fi
else
echo "ℹ️ No stash to restore"
fi
# Stage files matching the pattern
echo "➕ Staging files matching pattern: $FILE_PATTERN"
git add $FILE_PATTERN
# Check if there are changes to commit
if git diff --cached --quiet; then
echo "ℹ️ No changes to commit"
echo "committed=false" >> $GITHUB_OUTPUT
echo "commit_sha=" >> $GITHUB_OUTPUT
exit 0
fi
# Safety check: ensure no conflict markers are being committed
echo "🔍 Checking for conflict markers in staged files..."
if git diff --cached | grep -qE "^\+.*<<<<<<<|^\+.*=======|^\+.*>>>>>>>"; then
echo "::error::Conflict markers detected in staged files! Aborting commit."
echo "The following files contain unresolved conflicts:"
STAGED_FILES=$(git diff --cached --name-only)
while IFS= read -r file; do
if [ -n "$file" ] && grep -qE "<<<<<<<|=======|>>>>>>>" "$file" 2>/dev/null; then
echo " - $file"
fi
done <<< "$STAGED_FILES"
exit 1
fi
# Create commit
echo "💾 Creating commit..."
git commit -m "$COMMIT_MESSAGE"
COMMIT_SHA=$(git rev-parse HEAD)
echo "committed=true" >> $GITHUB_OUTPUT
echo "commit_sha=$COMMIT_SHA" >> $GITHUB_OUTPUT
# Push with retry logic
echo "⬆️ Pushing changes..."
ATTEMPT=1
while [ $ATTEMPT -le $MAX_RETRIES ]; do
echo "Push attempt $ATTEMPT of $MAX_RETRIES..."
if git push origin "$(git branch --show-current)"; then
echo "✅ Successfully pushed changes!"
exit 0
fi
if [ $ATTEMPT -lt $MAX_RETRIES ]; then
echo "⚠️ Push failed, rebasing and retrying..."
# Use theirs strategy to prefer our committed changes over remote changes during rebase
# In rebase context: theirs = our local commit (what we want to keep)
if ! git pull --rebase -X theirs origin "$(git branch --show-current)"; then
echo "::error::Rebase failed during retry despite conflict resolution strategy."
echo "::error::This indicates a complex conflict that cannot be auto-resolved."
# Check if we're in a rebase state
if [ -d .git/rebase-merge ] || [ -d .git/rebase-apply ]; then
echo "Aborting rebase..."
git rebase --abort
fi
exit 1
fi
fi
ATTEMPT=$((ATTEMPT + 1))
done
echo "::error::Failed to push after $MAX_RETRIES attempts"
exit 1
- name: Generate summary
if: always()
shell: bash
working-directory: ${{ inputs.path }}
run: |
set -euo pipefail
echo "## 🔄 Git Sync and Commit Summary" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
if [[ "${{ steps.commit.outcome }}" == "success" ]]; then
if [[ "${{ steps.commit.outputs.committed }}" == "true" ]]; then
echo "✅ **Status:** Successfully committed and pushed" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### 📝 Commit Details" >> $GITHUB_STEP_SUMMARY
echo "- **SHA:** \`${{ steps.commit.outputs.commit_sha }}\`" >> $GITHUB_STEP_SUMMARY
echo "- **Message:** ${{ inputs.commit_message }}" >> $GITHUB_STEP_SUMMARY
echo "- **Author:** ${{ inputs.commit_user_name }} <${{ inputs.commit_user_email }}>" >> $GITHUB_STEP_SUMMARY
echo "- **Files:** \`${{ inputs.file_pattern }}\`" >> $GITHUB_STEP_SUMMARY
else
echo "ℹ️ **Status:** No changes to commit" >> $GITHUB_STEP_SUMMARY
fi
else
echo "❌ **Status:** Failed" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "Check the logs above for error details." >> $GITHUB_STEP_SUMMARY
fi