|
| 1 | +# Inspired by serpro69/claude-starter-kit template-sync approach |
| 2 | +name: Template Sync |
| 3 | + |
| 4 | +on: |
| 5 | + workflow_dispatch: |
| 6 | + inputs: |
| 7 | + dry_run: |
| 8 | + description: "Show changes without creating a PR" |
| 9 | + type: boolean |
| 10 | + default: false |
| 11 | + template_repo: |
| 12 | + description: "Upstream template repository (owner/repo)" |
| 13 | + type: string |
| 14 | + default: "stranma/claude-code-python-template" |
| 15 | + template_branch: |
| 16 | + description: "Upstream template branch" |
| 17 | + type: string |
| 18 | + default: "master" |
| 19 | + schedule: |
| 20 | + - cron: "0 9 * * 1" # Weekly on Monday at 09:00 UTC |
| 21 | + |
| 22 | +permissions: |
| 23 | + contents: write |
| 24 | + pull-requests: write |
| 25 | + |
| 26 | +jobs: |
| 27 | + sync: |
| 28 | + name: Sync from upstream template |
| 29 | + runs-on: ubuntu-latest |
| 30 | + steps: |
| 31 | + - name: Checkout |
| 32 | + uses: actions/checkout@v4 |
| 33 | + with: |
| 34 | + fetch-depth: 0 |
| 35 | + |
| 36 | + - name: Configure git |
| 37 | + run: | |
| 38 | + git config user.name "github-actions[bot]" |
| 39 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 40 | +
|
| 41 | + - name: Determine template repo |
| 42 | + id: config |
| 43 | + run: | |
| 44 | + REPO="${{ inputs.template_repo || 'stranma/claude-code-python-template' }}" |
| 45 | + BRANCH="${{ inputs.template_branch || 'master' }}" |
| 46 | + echo "repo=${REPO}" >> "$GITHUB_OUTPUT" |
| 47 | + echo "branch=${BRANCH}" >> "$GITHUB_OUTPUT" |
| 48 | + echo "Syncing from ${REPO}@${BRANCH}" |
| 49 | +
|
| 50 | + - name: Add upstream remote and fetch |
| 51 | + run: | |
| 52 | + git remote add upstream "https://github.com/${{ steps.config.outputs.repo }}.git" || true |
| 53 | + git fetch upstream "${{ steps.config.outputs.branch }}" |
| 54 | +
|
| 55 | + - name: Compute template diff |
| 56 | + id: diff |
| 57 | + env: |
| 58 | + UPSTREAM_BRANCH: ${{ steps.config.outputs.branch }} |
| 59 | + run: | |
| 60 | + # Paths managed by the template (synced from upstream) |
| 61 | + # Defined once here; reused in the apply step via GITHUB_OUTPUT |
| 62 | + TEMPLATE_PATHS=".claude/agents/ .claude/commands/ .claude/hooks/ .claude/rules/ .claude/skills/ .devcontainer/ .github/workflows/ docs/DEVELOPMENT_PROCESS.md" |
| 63 | + echo "template_paths=${TEMPLATE_PATHS}" >> "$GITHUB_OUTPUT" |
| 64 | +
|
| 65 | + # Get changed files between local and upstream |
| 66 | + CHANGED=$(git diff --name-only HEAD "upstream/${UPSTREAM_BRANCH}" -- ${TEMPLATE_PATHS} 2>/dev/null || true) |
| 67 | +
|
| 68 | + if [ -z "$CHANGED" ]; then |
| 69 | + echo "No template changes found" |
| 70 | + echo "has_changes=false" >> "$GITHUB_OUTPUT" |
| 71 | + else |
| 72 | + echo "Template changes detected:" |
| 73 | + echo "$CHANGED" |
| 74 | + echo "has_changes=true" >> "$GITHUB_OUTPUT" |
| 75 | + # Store diff summary for PR body |
| 76 | + DIFF_STAT=$(git diff --stat HEAD "upstream/${UPSTREAM_BRANCH}" -- ${TEMPLATE_PATHS} 2>/dev/null || true) |
| 77 | + { |
| 78 | + echo "diff_stat<<EOF" |
| 79 | + echo "$DIFF_STAT" |
| 80 | + echo "EOF" |
| 81 | + } >> "$GITHUB_OUTPUT" |
| 82 | + fi |
| 83 | +
|
| 84 | + - name: Show diff (dry run) |
| 85 | + if: steps.diff.outputs.has_changes == 'true' && (inputs.dry_run == true || inputs.dry_run == 'true') |
| 86 | + run: | |
| 87 | + echo "=== DRY RUN: Changes that would be synced ===" |
| 88 | + echo "${{ steps.diff.outputs.diff_stat }}" |
| 89 | +
|
| 90 | + - name: Apply template changes |
| 91 | + id: apply |
| 92 | + if: steps.diff.outputs.has_changes == 'true' && inputs.dry_run != true && inputs.dry_run != 'true' |
| 93 | + env: |
| 94 | + UPSTREAM_BRANCH: ${{ steps.config.outputs.branch }} |
| 95 | + TEMPLATE_PATHS: ${{ steps.diff.outputs.template_paths }} |
| 96 | + run: | |
| 97 | + SYNC_BRANCH="template-sync/$(date +%Y%m%d)" |
| 98 | +
|
| 99 | + # Check if branch already exists |
| 100 | + if git rev-parse --verify "refs/heads/${SYNC_BRANCH}" > /dev/null 2>&1; then |
| 101 | + echo "Sync branch ${SYNC_BRANCH} already exists, updating" |
| 102 | + git checkout "${SYNC_BRANCH}" |
| 103 | + else |
| 104 | + git checkout -b "${SYNC_BRANCH}" |
| 105 | + fi |
| 106 | +
|
| 107 | + # Checkout template-managed files from upstream |
| 108 | + for path in ${TEMPLATE_PATHS}; do |
| 109 | + git checkout "upstream/${UPSTREAM_BRANCH}" -- "${path}" 2>/dev/null || true |
| 110 | + done |
| 111 | +
|
| 112 | + # Stage and commit |
| 113 | + git add -A |
| 114 | + if git diff --cached --quiet; then |
| 115 | + echo "No changes to commit after checkout" |
| 116 | + echo "changes_applied=false" >> "$GITHUB_OUTPUT" |
| 117 | + exit 0 |
| 118 | + fi |
| 119 | +
|
| 120 | + echo "changes_applied=true" >> "$GITHUB_OUTPUT" |
| 121 | +
|
| 122 | + git commit -m "chore: sync template from upstream |
| 123 | +
|
| 124 | + Source: ${{ steps.config.outputs.repo }}@${{ steps.config.outputs.branch }}" |
| 125 | +
|
| 126 | + git push -u origin "${SYNC_BRANCH}" |
| 127 | +
|
| 128 | + echo "sync_branch=${SYNC_BRANCH}" >> "$GITHUB_ENV" |
| 129 | +
|
| 130 | + - name: Create pull request |
| 131 | + if: steps.apply.outputs.changes_applied == 'true' && inputs.dry_run != true && inputs.dry_run != 'true' |
| 132 | + env: |
| 133 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 134 | + run: | |
| 135 | + # Check for existing PR from this branch |
| 136 | + EXISTING_PR=$(gh pr list --head "${{ env.sync_branch }}" --json number --jq '.[0].number' 2>/dev/null || true) |
| 137 | +
|
| 138 | + if [ -n "$EXISTING_PR" ]; then |
| 139 | + echo "PR #${EXISTING_PR} already exists for this sync branch" |
| 140 | + exit 0 |
| 141 | + fi |
| 142 | +
|
| 143 | + gh pr create \ |
| 144 | + --title "chore: sync upstream template changes" \ |
| 145 | + --body "$(cat <<'EOF' |
| 146 | + ## Template Sync |
| 147 | +
|
| 148 | + Automated sync of template-managed files from upstream. |
| 149 | +
|
| 150 | + **Source:** ${{ steps.config.outputs.repo }}@${{ steps.config.outputs.branch }} |
| 151 | +
|
| 152 | + ### Changed files |
| 153 | + ``` |
| 154 | + ${{ steps.diff.outputs.diff_stat }} |
| 155 | + ``` |
| 156 | +
|
| 157 | + ### What to review |
| 158 | + - Check if any synced files conflict with project-specific customizations |
| 159 | + - Template-managed paths: `.claude/`, `.devcontainer/`, `.github/workflows/`, `docs/DEVELOPMENT_PROCESS.md` |
| 160 | + - Project-specific files (`apps/`, `libs/`, `tests/`, `pyproject.toml`, `README.md`) are NOT touched |
| 161 | +
|
| 162 | + ### How to resolve conflicts |
| 163 | + If a synced file conflicts with local changes, edit the file on this branch to keep your customizations, then merge. |
| 164 | + EOF |
| 165 | + )" |
| 166 | +
|
| 167 | + - name: Summary |
| 168 | + if: steps.diff.outputs.has_changes == 'false' |
| 169 | + run: echo "Already up to date with upstream template. No sync needed." |
0 commit comments