name: Sync Main to Develop
# This workflow runs after any push to main to keep develop in sync
# Includes releases, hotfixes, and dependency updates
on:
push:
branches: [main]
workflow_dispatch: # Allow manual trigger
permissions:
contents: write
pull-requests: write
jobs:
sync-develop:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Checkout main branch
uses: actions/checkout@v6
with:
fetch-depth: 0 # Need full history for merging
ref: main
- name: Configure Git identity
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Merge main into develop
run: |
# Checkout develop
git checkout develop
git pull origin develop
# Get the latest commit info
LATEST_COMMIT=$(git log -1 --pretty=format:"%h - %s" origin/main)
echo "Latest commit: $LATEST_COMMIT"
# Merge main into develop
echo "Merging main into develop..."
if git merge origin/main --no-ff -m "chore: sync main to develop
Automatic sync to keep develop up to date with main.
Latest commit: ${LATEST_COMMIT}
Triggered by: ${{ github.event_name }}"; then
echo "✓ Merge successful"
# Push to develop
git push origin develop
echo "✓ Pushed to develop"
else
echo "✗ Merge conflict detected"
echo "Creating PR instead of direct merge..."
# Abort the merge
git merge --abort
# Create a branch for the merge
MERGE_BRANCH="auto-merge/main-to-develop-$(date +%Y%m%d-%H%M%S)"
git checkout -b "$MERGE_BRANCH"
git merge origin/main --no-ff -m "chore: sync main to develop" || true
# Push the branch
git push -u origin "$MERGE_BRANCH"
# Create PR using GitHub CLI
gh pr create \
--base develop \
--head "$MERGE_BRANCH" \
--title "chore: sync main to develop ($(date +%Y-%m-%d))" \
--body "Automatic sync from main to develop.
**Latest commit:** ${LATEST_COMMIT}
**Triggered by:** ${{ github.event_name }}
⚠️ **Merge conflicts detected** - manual resolution required.
Please review and resolve conflicts before merging." \
--label "automated" \
--label "sync"
echo "✓ Created PR for manual conflict resolution"
exit 0
fi
env:
GH_TOKEN: ${{ github.token }}
- name: Add sync confirmation comment
if: success()
uses: actions/github-script@v8
with:
script: |
// Find the most recent commit on main
const { data: commit } = await github.rest.repos.getCommit({
owner: context.repo.owner,
repo: context.repo.repo,
ref: 'main'
});
// Add a comment to the PR/commit that triggered this
const message = '✅ Changes automatically synced to `develop` branch.';
console.log(message);