Skip to content

Commit 32fe085

Browse files
committed
from f13
1 parent db3d78c commit 32fe085

File tree

4 files changed

+219
-4
lines changed

4 files changed

+219
-4
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
name: squash-merge-into-dev
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
pr_number:
7+
description: "Pull Request Number (e.g. 123)"
8+
required: true
9+
10+
jobs:
11+
squash-merge:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
# Checkout repository
16+
- name: Checkout repository
17+
uses: actions/checkout@v4
18+
with:
19+
fetch-depth: 0
20+
token: ${{ secrets.PAT }}
21+
22+
# Configure Git
23+
- name: Configure Git
24+
run: |
25+
git config --global user.name "GitHub Actions"
26+
git config --global user.email "actions@github.com"
27+
28+
# Fetch PR branch info
29+
- name: Get PR branch name
30+
id: pr_info
31+
run: |
32+
BRANCH_NAME=$(gh pr view ${{ github.event.inputs.pr_number }} --json headRefName -q ".headRefName")
33+
echo "branch_name=$BRANCH_NAME" >> $GITHUB_ENV
34+
env:
35+
GH_TOKEN: ${{ secrets.PAT }}
36+
37+
# Check for duplicate commit in dev
38+
- name: Check if commit message already exists in dev
39+
id: check_commit
40+
run: |
41+
git fetch origin dev
42+
git checkout dev
43+
COMMIT_MSG="from ${{ env.branch_name }}"
44+
if git log --pretty=format:"%s" origin/dev | grep -q "^$COMMIT_MSG$"; then
45+
echo "duplicate=true" >> $GITHUB_OUTPUT
46+
else
47+
echo "duplicate=false" >> $GITHUB_OUTPUT
48+
fi
49+
50+
# Trigger revert branch workflow if duplicate found
51+
- name: Trigger revert workflow
52+
if: steps.check_commit.outputs.duplicate == 'true'
53+
uses: peter-evans/workflow-dispatch@v2
54+
with:
55+
token: ${{ secrets.PAT }}
56+
repository: ${{ github.repository }}
57+
workflow: manual-revert-workflow.yml
58+
ref: dev
59+
inputs:
60+
branch_name: ${{ env.branch_name }}
61+
62+
# Wait for revert workflow to finish
63+
- name: Wait for revert to complete
64+
if: steps.check_commit.outputs.duplicate == 'true'
65+
run: |
66+
echo "Waiting for revert workflow to finish..."
67+
sleep 30
68+
69+
# Retry squash merge after revert OR first attempt
70+
- name: Squash merge PR into dev
71+
id: merge
72+
run: |
73+
git fetch origin
74+
git checkout dev
75+
git pull origin dev
76+
77+
# Fetch PR branch
78+
git fetch origin ${{ env.branch_name }}
79+
80+
set +e
81+
git merge origin/${{ env.branch_name }} --squash
82+
STATUS=$?
83+
set -e
84+
85+
if [ $STATUS -ne 0 ]; then
86+
echo "Merge conflict detected!"
87+
echo "conflict=true" >> $GITHUB_OUTPUT
88+
exit 0
89+
fi
90+
91+
git commit -m "from ${{ env.branch_name }}"
92+
git push origin dev
93+
echo "conflict=false" >> $GITHUB_OUTPUT
94+
95+
# Fail workflow if merge conflict detected
96+
- name: Fail on conflict
97+
if: steps.merge.outputs.conflict == 'true'
98+
run: |
99+
echo "❌ Merge conflict detected while merging ${{ env.branch_name }} into dev."
100+
echo "Please create a PR and resolve conflicts manually."
101+
exit 1
102+
103+
# Comment on PR after successful merge
104+
- name: Comment on PR
105+
if: steps.merge.outputs.conflict == 'false'
106+
run: |
107+
gh pr comment ${{ github.event.inputs.pr_number }} --body "✅ Successfully squash merged into dev with commit: from ${{ env.branch_name }}"
108+
env:
109+
GH_TOKEN: ${{ secrets.PAT }}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
name: push-test-to-prod
2+
3+
on:
4+
workflow_run:
5+
workflows: ["manual-revert-workflow"]
6+
types:
7+
- completed
8+
workflow_dispatch:
9+
inputs:
10+
force_push:
11+
description: "Force push test to prod (overwrites prod branch)"
12+
required: false
13+
default: "false"
14+
15+
jobs:
16+
push-to-prod:
17+
runs-on: ubuntu-latest
18+
if: github.event.workflow_run.conclusion == 'success'
19+
20+
steps:
21+
# Checkout repository
22+
- name: Checkout repository
23+
uses: actions/checkout@v4
24+
with:
25+
fetch-depth: 0
26+
token: ${{ secrets.PAT }}
27+
28+
# Configure Git
29+
- name: Configure Git
30+
run: |
31+
git config --global user.name "GitHub Actions"
32+
git config --global user.email "actions@github.com"
33+
34+
# Validate branches
35+
- name: Validate branches
36+
run: |
37+
git fetch origin
38+
if ! git ls-remote --heads origin test | grep -q test; then
39+
echo "Error: test branch does not exist"
40+
exit 1
41+
fi
42+
if ! git ls-remote --heads origin prod | grep -q prod; then
43+
echo "Creating prod branch from test"
44+
git checkout test
45+
git push origin test:prod
46+
exit 0
47+
fi
48+
49+
# Push test to prod
50+
- name: Push test to prod
51+
id: push
52+
run: |
53+
git checkout prod
54+
git fetch origin test
55+
set +e
56+
git merge origin/test --no-ff --no-edit
57+
STATUS=$?
58+
set -e
59+
if [ $STATUS -ne 0 ]; then
60+
echo "Merge conflict detected"
61+
TIMESTAMP=$(date +%s)
62+
CONFLICT_BRANCH="conflict/test-to-prod-$TIMESTAMP"
63+
git checkout -b "$CONFLICT_BRANCH"
64+
git push origin "$CONFLICT_BRANCH"
65+
gh pr create \
66+
--base prod \
67+
--head "$CONFLICT_BRANCH" \
68+
--title "Merge conflict: test to prod" \
69+
--body "Automatic PR: Conflicts occurred while merging test into prod. Please resolve manually."
70+
echo "conflict=true" >> $GITHUB_OUTPUT
71+
exit 0
72+
fi
73+
git push origin prod
74+
echo "conflict=false" >> $GITHUB_OUTPUT
75+
env:
76+
GH_TOKEN: ${{ secrets.PAT }}
77+
78+
# Force push if requested
79+
- name: Force push test to prod
80+
if: github.event.inputs.force_push == 'true' && steps.push.outputs.conflict == 'false'
81+
run: |
82+
git checkout test
83+
git push origin test:prod --force
84+
echo "Force pushed test to prod"
85+
env:
86+
GH_TOKEN: ${{ secrets.PAT }}
87+
88+
# Validate prod branch
89+
- name: Validate prod branch
90+
if: steps.push.outputs.conflict == 'false'
91+
run: |
92+
git fetch origin prod
93+
git checkout prod
94+
MERGE_COMMIT=$(git log --merges --pretty=%H -n 1)
95+
if [ -z "$MERGE_COMMIT" ]; then
96+
echo "No merge commit found in prod branch"
97+
exit 1
98+
fi
99+
echo "Merge commit validated: $MERGE_COMMIT"
100+
101+
# Notify on merge conflict
102+
- name: Notify on merge conflict
103+
if: steps.push.outputs.conflict == 'true'
104+
run: |
105+
gh issue create \
106+
--title "Merge Conflict: Test to Prod" \
107+
--body "Merge conflict occurred while merging test into prod. Conflict branch created. Check PR for details." \
108+
--repo ${{ github.repository }}
109+
env:
110+
GH_TOKEN: ${{ secrets.PAT }}

f11.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
test test normal commit

f13.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
2-
# test commit 19
3-
# test commit 15

0 commit comments

Comments
 (0)