9
9
sync-demo-branch :
10
10
runs-on : ubuntu-latest
11
11
steps :
12
- # Use v4, as v3 didn't help either
12
+ # Use v4
13
13
- name : Checkout demo branch
14
14
uses : actions/checkout@v4
15
15
with :
16
16
ref : ' demo'
17
17
token : ${{ secrets.DEMO_UPDATE_PAT }}
18
18
fetch-depth : 0
19
19
20
- # Verify .gitattributes (still good practice)
20
+ # Verify .gitattributes
21
21
- name : Verify .gitattributes
22
22
run : |
23
23
echo "Checking .gitattributes content:"
24
24
cat .gitattributes || echo ".gitattributes not found!"
25
25
26
- # *** ADDED: Check Git Version ***
26
+ # Check Git Version
27
27
- name : Check Git Version
28
28
run : git --version
29
29
@@ -41,68 +41,61 @@ jobs:
41
41
- name : Fetch origin
42
42
run : git fetch origin
43
43
44
- # Perform the merge using -X ours and add verbose output
45
- # Use continue-on-error to allow the next step to run
46
- - name : Merge main into demo using -X ours (VERBOSE)
44
+ # Attempt the merge using -X ours, allow failure
45
+ - name : Attempt Merge main into demo using -X ours
46
+ id : merge_attempt # Give the step an ID
47
47
run : |
48
- echo "Attempting merge with: git merge --no-ff -X ours --verbose origin/main"
49
- git merge --no-ff -X ours --verbose origin/main -m "Auto-merge main into demo (using -X ours, verbose)"
50
- continue-on-error : true # IMPORTANT: Allow workflow to continue to next step on failure
48
+ echo "Attempting merge with: git merge --no-ff -X ours origin/main"
49
+ # Use a standard merge message here, we might amend it later if needed
50
+ git merge --no-ff -X ours origin/main -m "Auto-merge main into demo"
51
+ continue-on-error : true
51
52
52
- # *** ADDED: Show Git Status After Merge Attempt ***
53
- # This step will run regardless of merge success/failure IF the previous step used continue-on-error
54
- - name : Show Git Status After Merge Attempt
55
- if : always() # Ensure this runs even if merge succeeded or failed
53
+ # *** NEW: Resolve conflicts by keeping 'ours' if merge failed ***
54
+ - name : Resolve Conflicts Favoring 'ours'
55
+ # Run only if the previous merge step failed
56
+ if : steps.merge_attempt.outcome == 'failure'
56
57
run : |
57
- echo "--- Git Status after merge attempt ---"
58
- git status --short # Use --short for concise output
59
- echo "--- Files with conflicts (if any) ---"
60
- # List files specifically marked as Unmerged (U)
61
- git diff --name-only --diff-filter=U || echo "No files found with Unmerged status."
58
+ echo "Merge failed, attempting to resolve conflicts favouring 'ours'..."
59
+ # Get list of unmerged files (conflicts)
60
+ git diff --name-only --diff-filter=U > conflicted_files.txt
62
61
63
- # *** IMPORTANT: Check if merge failed before trying to push ***
64
- - name : Check merge result before push
65
- id : merge_check
66
- # Check the exit code of the merge step
67
- run : |
68
- # Note: This relies on the step ID of the merge step if you named it.
69
- # Adjust 'steps.merge_step_id.outcome' if your merge step has an id:
70
- # For now, we check if status shows unmerged files as a proxy
71
- if git status --short | grep -q '^UU '; then
72
- echo "Merge failed with conflicts. Aborting push."
73
- echo "outcome=failure" >> $GITHUB_OUTPUT
74
- exit 1 # Fail the workflow explicitly
75
- elif git status --short | grep -q '^AA '; then # Added by us, deleted by them
76
- echo "Merge failed with conflicts (AA). Aborting push."
77
- echo "outcome=failure" >> $GITHUB_OUTPUT
78
- exit 1 # Fail the workflow explicitly
79
- elif git status --short | grep -q '^DD '; then # Deleted by both
80
- echo "Merge failed with conflicts (DD). Aborting push."
81
- echo "outcome=failure" >> $GITHUB_OUTPUT
82
- exit 1 # Fail the workflow explicitly
83
- elif git status --short | grep -q '^AU '; then # Added by us, unmerged by them
84
- echo "Merge failed with conflicts (AU). Aborting push."
85
- echo "outcome=failure" >> $GITHUB_OUTPUT
86
- exit 1 # Fail the workflow explicitly
87
- elif git status --short | grep -q '^UA '; then # Unmerged by us, added by them
88
- echo "Merge failed with conflicts (UA). Aborting push."
89
- echo "outcome=failure" >> $GITHUB_OUTPUT
90
- exit 1 # Fail the workflow explicitly
91
- elif git status --short | grep -q '^UD '; then # Unmerged by us, deleted by them
92
- echo "Merge failed with conflicts (UD). Aborting push."
93
- echo "outcome=failure" >> $GITHUB_OUTPUT
94
- exit 1 # Fail the workflow explicitly
95
- elif git status --short | grep -q '^DU '; then # Deleted by us, unmerged by them
96
- echo "Merge failed with conflicts (DU). Aborting push."
97
- echo "outcome=failure" >> $GITHUB_OUTPUT
98
- exit 1 # Fail the workflow explicitly
62
+ if [ -s conflicted_files.txt ]; then
63
+ echo "Conflicting files found:"
64
+ cat conflicted_files.txt
65
+ # For each conflicting file, checkout the version from 'ours' (demo branch)
66
+ # Use xargs to handle the list of files efficiently
67
+ echo "Checking out 'ours' version for conflicting files..."
68
+ xargs -r -a conflicted_files.txt git checkout --ours --
69
+ # Add the now-resolved files to the index
70
+ echo "Adding resolved files..."
71
+ xargs -r -a conflicted_files.txt git add --
72
+ # Commit the resolved merge
73
+ echo "Committing resolved merge..."
74
+ # You can use a specific message or potentially amend the failed merge commit
75
+ # Using a new message for clarity:
76
+ git commit -m "Auto-merge main into demo (Conflicts resolved favoring demo)"
77
+ echo "Conflicts resolved and committed."
99
78
else
100
- echo "Merge appears successful or conflicts resolved automatically."
101
- echo "outcome=success" >> $GITHUB_OUTPUT
79
+ echo "Merge failed, but no conflicting files found by 'git diff --diff-filter=U'. This is unexpected."
80
+ # Explicitly fail the job if merge failed but we couldn't find/resolve conflicts
81
+ exit 1
82
+ fi
83
+
84
+ # Check final status ONLY IF the resolution step ran (i.e., merge initially failed)
85
+ # This is mostly for logging/debugging if the resolution itself failed.
86
+ - name : Check Status After Resolution Attempt
87
+ if : steps.merge_attempt.outcome == 'failure'
88
+ run : |
89
+ echo "--- Git Status after conflict resolution attempt ---"
90
+ git status --short
91
+ # Check again for unmerged files. If any exist here, the resolution failed.
92
+ if git diff --name-only --diff-filter=U | read; then
93
+ echo "ERROR: Unmerged files still exist after resolution attempt!"
94
+ exit 1
102
95
fi
103
96
104
- # Push only if the merge succeeded (checked in previous step )
97
+ # Push the changes (either the successful initial merge or the resolved merge )
105
98
- name : Push changes to demo
106
- if : steps.merge_check.outputs.outcome == 'success'
107
99
run : |
100
+ echo "Pushing changes to demo..."
108
101
git push origin demo
0 commit comments