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"
# Check if develop is already up to date with main
if git merge-base --is-ancestor origin/main develop; then
echo "✓ Develop is already up to date with main"
exit 0
fi
# Create a branch for the merge (always use PR due to branch protection)
MERGE_BRANCH="auto-sync/main-to-develop-$(date +%Y%m%d-%H%M%S)"
git checkout -b "$MERGE_BRANCH"
# Merge main into the branch
echo "Merging main into ${MERGE_BRANCH}..."
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 the branch
git push -u origin "$MERGE_BRANCH"
# Create PR using GitHub CLI (labels are optional)
gh pr create \
--base develop \
--head "$MERGE_BRANCH" \
--title "chore: sync main to develop" \
--body "🔄 Automatic sync from main to develop.
**Latest commit:** ${LATEST_COMMIT}
**Triggered by:** ${{ github.event_name }}
This PR was automatically created to sync changes from main to develop.
All changes merged successfully without conflicts." || true
# Try to add labels (non-critical, continue on error)
gh pr edit --add-label "automated" 2>/dev/null || echo "Note: Could not add 'automated' label (label may not exist)"
gh pr edit --add-label "sync" 2>/dev/null || echo "Note: Could not add 'sync' label (label may not exist)"
# Enable auto-merge on the PR
PR_NUMBER=$(gh pr view "$MERGE_BRANCH" --json number -q .number)
gh pr merge "$PR_NUMBER" --auto --squash
echo "✓ Created PR #${PR_NUMBER} with auto-merge enabled"
else
echo "✗ Merge conflict detected"
echo "Creating PR for manual conflict resolution..."
# Abort the merge
git merge --abort
# Try merge again to capture conflicts
git merge origin/main --no-ff -m "chore: sync main to develop" || true
# Push the branch with conflicts
git push -u origin "$MERGE_BRANCH"
# Create PR using GitHub CLI (labels are optional)
gh pr create \
--base develop \
--head "$MERGE_BRANCH" \
--title "chore: sync main to develop (⚠️ conflicts)" \
--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." || true
# Try to add labels (non-critical, continue on error)
gh pr edit --add-label "automated" 2>/dev/null || echo "Note: Could not add 'automated' label (label may not exist)"
gh pr edit --add-label "sync" 2>/dev/null || echo "Note: Could not add 'sync' label (label may not exist)"
gh pr edit --add-label "conflicts" 2>/dev/null || echo "Note: Could not add 'conflicts' label (label may not exist)"
echo "✓ Created PR for manual conflict resolution"
fi
env:
GH_TOKEN: ${{ github.token }}
- name: Add sync confirmation comment
if: success()
uses: actions/github-script@v8
with:
script: |
console.log('✅ Sync PR created successfully');