Skip to content

Commit 67eb48a

Browse files
committed
tip
1 parent 10cb891 commit 67eb48a

1 file changed

Lines changed: 38 additions & 9 deletions

File tree

.github/workflows/release.yml

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ name: Automated Release
22

33
on:
44
release:
5-
types: [created] # Runs when a non-draft release is created in the UI
5+
types: [created] # Only fire when a release is created
66

77
permissions:
8-
contents: write # Required to push commits and update tags
8+
contents: write # Required to push commits, update tags and edit releases
99

1010
jobs:
1111
release:
12+
# Only run for draft releases; ignore non-draft created events
13+
if: ${{ github.event.release.draft == true }}
14+
1215
runs-on: ubuntu-latest
1316

1417
steps:
@@ -148,21 +151,47 @@ jobs:
148151
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
149152
MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
150153

151-
- name: Push changes to originating branch and tag
154+
- name: Push changes to originating branch and tag (with merge fallback)
152155
run: |
153156
TARGET_BRANCH="${{ steps.target_branch.outputs.target_branch }}"
154157
TAG_NAME="${{ github.event.release.tag_name }}"
155158
156159
echo "Pushing changes to branch: $TARGET_BRANCH"
157160
158-
if ! git push --force-with-lease origin "HEAD:${TARGET_BRANCH}"; then
159-
echo "::error::Failed to push release commits to ${TARGET_BRANCH} due to branch divergence."
160-
echo "The remote branch may have new commits. Please resolve the conflict manually:"
161-
echo " 1. Fetch the latest changes: git fetch origin"
162-
echo " 2. Rebase or merge as needed, then push again with --force-with-lease."
163-
exit 1
161+
# First, try a normal fast-forward push
162+
if git push origin "HEAD:${TARGET_BRANCH}"; then
163+
echo "Fast-forward push to ${TARGET_BRANCH} succeeded."
164+
else
165+
echo "::warning::Fast-forward push to ${TARGET_BRANCH} failed. Trying merge fallback."
166+
167+
# Fetch latest state of the branch
168+
git fetch origin "${TARGET_BRANCH}"
169+
170+
# Merge origin/TARGET_BRANCH into our release HEAD.
171+
# If this conflicts, we bail out rather than trying to auto-resolve.
172+
if ! git merge --no-edit "origin/${TARGET_BRANCH}"; then
173+
echo "::error::Automatic merge with origin/${TARGET_BRANCH} failed due to conflicts."
174+
echo "Please resolve manually by checking out the release branch locally and merging origin/${TARGET_BRANCH}."
175+
exit 1
176+
fi
177+
178+
# Now push the merge commit
179+
if git push origin "HEAD:${TARGET_BRANCH}"; then
180+
echo "Pushed merge commit to ${TARGET_BRANCH}."
181+
else
182+
echo "::error::Failed to push merge commit to ${TARGET_BRANCH}."
183+
exit 1
184+
fi
164185
fi
165186
166187
echo "Pushing tag $TAG_NAME"
167188
git push origin "$TAG_NAME"
168189
echo "Pushed release commits and tag to $TARGET_BRANCH"
190+
191+
- name: Publish GitHub release
192+
env:
193+
GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }}
194+
run: |
195+
TAG_NAME="${{ github.event.release.tag_name }}"
196+
echo "Publishing draft release for $TAG_NAME"
197+
gh release edit "$TAG_NAME" --draft=false

0 commit comments

Comments
 (0)