Sync branch to template #4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Sync branch to template | |
on: | |
workflow_dispatch: | |
schedule: | |
- cron: "14 0 1 * *" | |
jobs: | |
sync: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
pull-requests: write | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Get GitHub App token | |
uses: tibdex/[email protected] | |
id: get_installation_token | |
with: | |
app_id: ${{ secrets.APP_ID }} | |
private_key: ${{ secrets.APP_PRIVATE_KEY }} | |
- name: Sync branch to template | |
env: | |
GH_TOKEN: ${{ steps.get_installation_token.outputs.token }} | |
run: | | |
branch_name=$(git rev-parse --abbrev-ref HEAD) | |
original_remote=$(git remote get-url origin) | |
pr_branch="sync-template/${branch_name}" | |
git config --global user.email "ubiquity-os[bot]@users.noreply.github.com" | |
git config --global user.name "ubiquity-os[bot]" | |
git remote add template https://github.com/ubiquity/ts-template.git | |
git fetch template | |
git checkout -b "$pr_branch" | |
last_sync=$(git log --grep="Sync template" --format="%H" -n 1) | |
if [ -n "$last_sync" ]; then | |
# If there's a previous sync, try to cherry-pick new changes | |
if ! git cherry-pick $last_sync..template/main; then | |
# If cherry-pick fails, abort and try a merge instead | |
git cherry-pick --abort | |
git merge template/main --no-commit | |
fi | |
else | |
# If it's the first sync, merge the template | |
git merge template/main --allow-unrelated-histories --no-commit | |
fi | |
# Check if there are any changes | |
if git diff --staged --quiet && git diff --quiet; then | |
echo "No changes to sync from template." | |
exit 0 | |
fi | |
# Commit changes, even if there are conflicts | |
git commit -am "chore: sync template (with potential conflicts)" || true | |
# Push changes and create PR | |
git push -f "$original_remote" "$pr_branch" | |
gh pr create --title "Sync branch to template (manual resolution required)" \ | |
--body "This pull request merges changes from the template repository. There may be conflicts that require manual resolution." \ | |
--head "$pr_branch" \ | |
--base "$branch_name" || true |