forked from epicweb-dev/epic-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
166 lines (138 loc) · 5.77 KB
/
reset-upstream-main.yml
File metadata and controls
166 lines (138 loc) · 5.77 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
name: 🔄 Reset upstream-main Branch
on:
push:
branches:
- copilot/reset-upstream-main-branch
workflow_dispatch:
inputs:
target_commit:
description: 'Target commit SHA to reset to'
required: true
default: 'bac7bd445d5d4c7c602399a842518f40ec591f2d'
confirm:
description: 'Type "confirm" to proceed with the reset'
required: true
permissions:
contents: write
env:
DEFAULT_TARGET_COMMIT: 'bac7bd445d5d4c7c602399a842518f40ec591f2d'
jobs:
reset-branch:
name: Reset upstream-main to target commit
runs-on: ubuntu-22.04
# Only run on push events, or on workflow_dispatch with confirmation
if: |
github.event_name == 'push' ||
(github.event_name == 'workflow_dispatch' && github.event.inputs.confirm == 'confirm')
steps:
- name: ⬇️ Checkout repo
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for all branches
token: ${{ secrets.GITHUB_TOKEN }}
- name: 🎯 Set target commit
id: set-target
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
TARGET_COMMIT="${{ github.event.inputs.target_commit }}"
else
TARGET_COMMIT="${{ env.DEFAULT_TARGET_COMMIT }}"
fi
echo "target_commit=$TARGET_COMMIT" >> $GITHUB_OUTPUT
echo "Using target commit: $TARGET_COMMIT"
- name: 🔍 Verify target commit exists
run: |
TARGET_COMMIT="${{ steps.set-target.outputs.target_commit }}"
if ! git cat-file -e "$TARGET_COMMIT^{commit}"; then
echo "❌ Target commit $TARGET_COMMIT does not exist"
exit 1
fi
echo "✅ Target commit exists: $TARGET_COMMIT"
git log --oneline -1 "$TARGET_COMMIT"
- name: 📊 Show commits to be removed
run: |
TARGET_COMMIT="${{ steps.set-target.outputs.target_commit }}"
echo "The following commits will be REMOVED from upstream-main:"
git log --oneline "$TARGET_COMMIT..origin/upstream-main" || echo "No commits to remove (branch already at or before target)"
- name: 🔄 Reset upstream-main branch
run: |
TARGET_COMMIT="${{ steps.set-target.outputs.target_commit }}"
# Configure git
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# Fetch and checkout upstream-main
git fetch origin upstream-main
git checkout upstream-main
# Reset to target commit
git reset --hard "$TARGET_COMMIT"
echo "✅ Local branch reset complete"
echo "Current HEAD:"
git log --oneline -1
- name: 🚀 Force push to remote
run: |
git push origin upstream-main --force
echo "✅ Successfully force pushed upstream-main to origin"
- name: ✅ Verify reset
run: |
TARGET_COMMIT="${{ steps.set-target.outputs.target_commit }}"
# Verify the remote branch
REMOTE_SHA=$(git ls-remote origin upstream-main | awk '{print $1}')
if [ "$REMOTE_SHA" = "$TARGET_COMMIT" ]; then
echo "✅ SUCCESS: upstream-main branch has been reset to $TARGET_COMMIT"
else
echo "❌ ERROR: Remote branch is at $REMOTE_SHA, expected $TARGET_COMMIT"
exit 1
fi
echo ""
echo "Latest commits on upstream-main:"
git log --oneline upstream-main -5
verify-result:
name: Verify upstream-main reset result
runs-on: ubuntu-22.04
needs: reset-branch
if: always()
steps:
- name: ⬇️ Checkout repo
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: 🔍 Check reset result
run: |
TARGET_COMMIT="${{ env.DEFAULT_TARGET_COMMIT }}"
# Fetch the latest state
git fetch origin upstream-main
# Get the current commit on upstream-main
CURRENT_SHA=$(git ls-remote origin upstream-main | awk '{print $1}')
echo "Target commit: $TARGET_COMMIT"
echo "Current upstream-main: $CURRENT_SHA"
echo ""
if [ "$CURRENT_SHA" = "$TARGET_COMMIT" ]; then
echo "✅ VERIFICATION SUCCESS"
echo "The upstream-main branch is correctly reset to $TARGET_COMMIT"
echo ""
echo "Commits on upstream-main:"
git log --oneline "$CURRENT_SHA" -5
exit 0
else
echo "❌ VERIFICATION FAILED"
echo "The upstream-main branch is at $CURRENT_SHA but should be at $TARGET_COMMIT"
echo ""
echo "Difference:"
git log --oneline "$TARGET_COMMIT..$CURRENT_SHA" 2>/dev/null || echo "Target is ahead of current"
exit 1
fi
- name: 📋 Job summary
if: always()
run: |
TARGET_COMMIT="${{ env.DEFAULT_TARGET_COMMIT }}"
CURRENT_SHA=$(git ls-remote origin upstream-main | awk '{print $1}')
echo "## Reset Verification Results" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "- **Target commit**: \`$TARGET_COMMIT\`" >> $GITHUB_STEP_SUMMARY
echo "- **Current commit**: \`$CURRENT_SHA\`" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
if [ "$CURRENT_SHA" = "$TARGET_COMMIT" ]; then
echo "✅ **Status**: Reset successful" >> $GITHUB_STEP_SUMMARY
else
echo "❌ **Status**: Reset failed or incomplete" >> $GITHUB_STEP_SUMMARY
fi