@@ -17,11 +17,19 @@ jobs:
1717 name : Merge main into preview
1818 runs-on : ubuntu-latest
1919 steps :
20+ - name : Generate App token
21+ id : app-token
22+ uses : actions/create-github-app-token@v1
23+ with :
24+ app-id : ${{ secrets.APP_ID }}
25+ private-key : ${{ secrets.APP_PRIVATE_KEY }}
26+
2027 - name : Checkout preview
2128 uses : actions/checkout@v6
2229 with :
2330 ref : preview
2431 fetch-depth : 0
32+ token : ${{ steps.app-token.outputs.token }}
2533
2634 - name : Configure git
2735 run : |
@@ -46,26 +54,10 @@ jobs:
4654 if : steps.check.outputs.needed == 'false'
4755 run : echo "Nothing to sync."
4856
49- - name : Check for existing PR
57+ - name : Merge main into preview
5058 if : steps.check.outputs.needed == 'true'
51- id : existing
52- env :
53- GH_TOKEN : ${{ github.token }}
54- run : |
55- COUNT=$(gh pr list --base preview --search "sync-preview:" --state open --json number --jq 'length')
56- echo "count=$COUNT" >> $GITHUB_OUTPUT
57-
58- - name : Skip if PR already open
59- if : steps.check.outputs.needed == 'true' && steps.existing.outputs.count != '0'
60- run : echo "ℹ️ Sync PR already open — skipping duplicate."
61-
62- - name : Create sync branch and merge
63- if : steps.check.outputs.needed == 'true' && steps.existing.outputs.count == '0'
6459 id : merge
6560 run : |
66- BRANCH="sync-preview/merge-main-$(date +%Y%m%d-%H%M%S)"
67- git checkout -b "$BRANCH"
68-
6961 # Save preview's version before merge so we can restore it after
7062 PREVIEW_VERSION=$(node -p "require('./package.json').version")
7163 echo "preview_version=$PREVIEW_VERSION" >> $GITHUB_OUTPUT
@@ -88,14 +80,16 @@ jobs:
8880 git commit --no-edit -m "chore: merge main into preview"
8981 echo "status=clean" >> $GITHUB_OUTPUT
9082 else
91- git add -A
92- git commit --no-edit -m "chore: merge main into preview (conflicts need resolution)" || true
9383 echo "status=conflict" >> $GITHUB_OUTPUT
9484 fi
9585 fi
9686
97- # Restore preview's version in package.json and package-lock.json
87+ - name : Restore preview version and push
88+ if : steps.merge.outputs.status == 'clean'
89+ run : |
90+ PREVIEW_VERSION="${{ steps.merge.outputs.preview_version }}"
9891 CURRENT_VERSION=$(node -p "require('./package.json').version")
92+
9993 if [[ "$CURRENT_VERSION" != "$PREVIEW_VERSION" ]]; then
10094 node -e "
10195 const fs = require('fs');
@@ -118,55 +112,35 @@ jobs:
118112 git commit -m "chore: restore preview version ($PREVIEW_VERSION)"
119113 fi
120114
121- git push origin "$BRANCH"
122- echo "branch=$BRANCH" >> $GITHUB_OUTPUT
123-
124- - name : Get original commit author
125- if : steps.merge.outputs.branch != ''
126- id : author
127- env :
128- GH_TOKEN : ${{ github.token }}
129- run : |
130- GH_USER=$(gh api "/repos/${{ github.repository }}/commits/$(git rev-parse origin/main)" --jq '.author.login // empty' 2>/dev/null || echo "")
131- echo "gh_user=$GH_USER" >> $GITHUB_OUTPUT
115+ git push origin HEAD:preview
116+ echo "✅ main merged into preview and pushed"
132117
133- - name : Create PR (clean merge)
134- if : steps.merge.outputs.status == 'clean '
118+ - name : Create PR for conflict resolution
119+ if : steps.merge.outputs.status == 'conflict '
135120 env :
136- GH_TOKEN : ${{ github.token }}
137- BRANCH : ${{ steps.merge.outputs.branch }}
138- AUTHOR_GH : ${{ steps.author.outputs.gh_user }}
121+ GH_TOKEN : ${{ steps.app-token.outputs.token }}
139122 run : |
140- MENTION=""
141- if [[ -n "$AUTHOR_GH" ]]; then
142- MENTION="cc @${AUTHOR_GH}"
123+ # Check if there's already an open sync PR
124+ COUNT=$(gh pr list --base preview --search "sync-preview:" --state open --json number --jq 'length')
125+ if [[ "$COUNT" != "0" ]]; then
126+ echo "ℹ️ Sync PR already open — skipping duplicate."
127+ exit 0
143128 fi
144129
145- gh pr create \
146- --base preview \
147- --head "$BRANCH" \
148- --title "sync-preview: merge main into preview" \
149- --body "$(cat <<BODY
150- Automated sync of \`main\` into \`preview\`. Clean merge — no conflicts.
151-
152- Review the changes and merge when ready.
130+ # Abort the failed merge and redo on a branch for the PR
131+ git merge --abort
153132
154- ${MENTION}
155-
156- _Opened automatically by the sync-preview workflow._
157- BODY
158- )"
133+ BRANCH="sync-preview/merge-main-$(date +%Y%m%d-%H%M%S)"
134+ git checkout -b "$BRANCH"
135+ git merge origin/main --no-edit -m "chore: merge main into preview (conflicts need resolution)" || true
136+ git add -A
137+ git commit --no-edit -m "chore: merge main into preview (conflicts need resolution)" || true
138+ git push origin "$BRANCH"
159139
160- - name : Create PR (conflict)
161- if : steps.merge.outputs.status == 'conflict'
162- env :
163- GH_TOKEN : ${{ github.token }}
164- BRANCH : ${{ steps.merge.outputs.branch }}
165- AUTHOR_GH : ${{ steps.author.outputs.gh_user }}
166- run : |
140+ GH_USER=$(gh api "/repos/${{ github.repository }}/commits/$(git rev-parse origin/main)" --jq '.author.login // empty' 2>/dev/null || echo "")
167141 MENTION=""
168- if [[ -n "$AUTHOR_GH " ]]; then
169- MENTION="cc @${AUTHOR_GH }"
142+ if [[ -n "$GH_USER " ]]; then
143+ MENTION="cc @${GH_USER }"
170144 fi
171145
172146 gh pr create \
0 commit comments