|
| 1 | +name: Sync Template Updates |
| 2 | + |
| 3 | +on: |
| 4 | + # cronjob trigger |
| 5 | + # schedule: |
| 6 | + - cron: "30 04 * * 1" |
| 7 | + # manual trigger |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: write |
| 12 | + pull-requests: write |
| 13 | + |
| 14 | +jobs: |
| 15 | + sync-template: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + |
| 18 | + steps: |
| 19 | + - name: Checkout target repository |
| 20 | + uses: actions/checkout@v4 |
| 21 | + with: |
| 22 | + fetch-depth: 0 |
| 23 | + |
| 24 | + - name: Parse TEMPLATE_ORIGIN.txt |
| 25 | + id: origin |
| 26 | + run: | |
| 27 | + if [ ! -f TEMPLATE_ORIGIN.txt ]; then |
| 28 | + echo "❌ TEMPLATE_ORIGIN.txt not found. Cannot determine template origin." |
| 29 | + exit 1 |
| 30 | + fi |
| 31 | +
|
| 32 | + template_repo=$(grep '^Template:' TEMPLATE_ORIGIN.txt | awk '{print $2}') |
| 33 | + template_branch=$(grep '^Template Branch:' TEMPLATE_ORIGIN.txt | awk '{print $3}') |
| 34 | + template_commit=$(grep '^Template Commit:' TEMPLATE_ORIGIN.txt | awk '{print $3}') |
| 35 | +
|
| 36 | + echo "Template repo: $template_repo" |
| 37 | + echo "Template branch: $template_branch" |
| 38 | + echo "Template commit: $template_commit" |
| 39 | +
|
| 40 | + echo "template_repo=$template_repo" >> $GITHUB_OUTPUT |
| 41 | + echo "template_branch=$template_branch" >> $GITHUB_OUTPUT |
| 42 | + echo "template_commit=$template_commit" >> $GITHUB_OUTPUT |
| 43 | +
|
| 44 | + - name: Fetch template repository |
| 45 | + run: | |
| 46 | + git remote add template https://github.com/${{ steps.origin.outputs.template_repo }}.git |
| 47 | + git fetch template ${{ steps.origin.outputs.template_branch }} |
| 48 | +
|
| 49 | + - name: Determine new commits |
| 50 | + id: commits |
| 51 | + run: | |
| 52 | + set -e |
| 53 | + git log template/${{ steps.origin.outputs.template_branch }} --pretty=format:"%H" > all_commits.txt |
| 54 | + commits_to_apply=$(awk -v last="${{ steps.origin.outputs.template_commit }}" 'BEGIN{found=0}{if($1==last){found=1;next} if(found==1) print $1}' all_commits.txt | tac) |
| 55 | + if [ -z "$commits_to_apply" ]; then |
| 56 | + echo "No new commits to apply." |
| 57 | + echo "has_updates=false" >> $GITHUB_OUTPUT |
| 58 | + else |
| 59 | + echo "$commits_to_apply" > commits_to_apply.txt |
| 60 | + echo "has_updates=true" >> $GITHUB_OUTPUT |
| 61 | + fi |
| 62 | +
|
| 63 | + - name: Stop if no updates |
| 64 | + if: steps.commits.outputs.has_updates == 'false' |
| 65 | + run: echo "No new commits to cherry-pick." |
| 66 | + |
| 67 | + - name: Find existing template-sync PR |
| 68 | + if: steps.commits.outputs.has_updates == 'true' |
| 69 | + id: existing_pr |
| 70 | + env: |
| 71 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 72 | + REPO: ${{ github.repository }} |
| 73 | + run: | |
| 74 | + gh auth login --with-token <<< "$GITHUB_TOKEN" |
| 75 | + pr_number=$(gh pr list --repo "$REPO" --label "template-sync" --state open --json number --jq '.[0].number') |
| 76 | + if [ -n "$pr_number" ]; then |
| 77 | + echo "Found existing PR: #$pr_number" |
| 78 | + branch=$(gh pr view "$pr_number" --repo "$REPO" --json headRefName --jq '.headRefName') |
| 79 | + echo "branch_name=$branch" >> $GITHUB_OUTPUT |
| 80 | + echo "existing_pr_number=$pr_number" >> $GITHUB_OUTPUT |
| 81 | + echo "has_existing_pr=true" >> $GITHUB_OUTPUT |
| 82 | + else |
| 83 | + branch="sync-template-$(date +%Y%m%d-%H%M%S)" |
| 84 | + echo "branch_name=$branch" >> $GITHUB_OUTPUT |
| 85 | + echo "has_existing_pr=false" >> $GITHUB_OUTPUT |
| 86 | + fi |
| 87 | +
|
| 88 | + - name: Create or checkout sync branch |
| 89 | + if: steps.commits.outputs.has_updates == 'true' |
| 90 | + run: | |
| 91 | + branch="${{ steps.existing_pr.outputs.branch_name }}" |
| 92 | + git fetch origin "$branch" || true |
| 93 | + git checkout -B "$branch" origin/"$branch" || git checkout -b "$branch" |
| 94 | +
|
| 95 | + - name: Cherry-pick new commits |
| 96 | + if: steps.commits.outputs.has_updates == 'true' |
| 97 | + run: | |
| 98 | + set -e |
| 99 | + while read c; do |
| 100 | + echo "Cherry-picking $c" |
| 101 | + git cherry-pick $c || (git cherry-pick --abort && echo "Cherry-pick conflict. Please resolve manually." && exit 1) |
| 102 | + done < commits_to_apply.txt |
| 103 | +
|
| 104 | + - name: Update Template Commit in TEMPLATE_ORIGIN.txt |
| 105 | + if: steps.commits.outputs.has_updates == 'true' |
| 106 | + run: | |
| 107 | + latest=$(git rev-parse template/${{ steps.origin.outputs.template_branch }}) |
| 108 | + echo "Updating Template Commit to $latest" |
| 109 | + sed -i "s|^Template Commit: .*|Template Commit: $latest|" TEMPLATE_ORIGIN.txt |
| 110 | +
|
| 111 | + git config user.name "github-actions[bot]" |
| 112 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 113 | + git add TEMPLATE_ORIGIN.txt |
| 114 | + git commit -m "Update Template Commit to $latest" || echo "No changes to commit" |
| 115 | +
|
| 116 | + - name: Push updated branch |
| 117 | + if: steps.commits.outputs.has_updates == 'true' |
| 118 | + run: git push origin HEAD:${{ steps.existing_pr.outputs.branch_name }} |
| 119 | + |
| 120 | + - name: Ensure template-sync label exists |
| 121 | + if: steps.commits.outputs.has_updates == 'true' |
| 122 | + env: |
| 123 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 124 | + REPO: ${{ github.repository }} |
| 125 | + run: | |
| 126 | + gh auth login --with-token <<< "$GITHUB_TOKEN" |
| 127 | + if ! gh label list --repo "$REPO" | grep -q '^template-sync'; then |
| 128 | + echo "Creating 'template-sync' label..." |
| 129 | + gh label create template-sync \ |
| 130 | + --repo "$REPO" \ |
| 131 | + --description "Pull requests that synchronize changes from the template repository" \ |
| 132 | + --color "0E8A16" || true |
| 133 | + else |
| 134 | + echo "'template-sync' label already exists." |
| 135 | + fi |
| 136 | +
|
| 137 | + - name: Generate commit summary |
| 138 | + if: steps.commits.outputs.has_updates == 'true' |
| 139 | + id: summary |
| 140 | + run: | |
| 141 | + echo "### Template commits since last sync" > commit_summary.md |
| 142 | + echo "" >> commit_summary.md |
| 143 | + git log ${{ steps.origin.outputs.template_commit }}..template/${{ steps.origin.outputs.template_branch }} --pretty=format:"- %h %s (%an)" >> commit_summary.md |
| 144 | + echo "" >> commit_summary.md |
| 145 | + echo "commit_summary<<EOF" >> $GITHUB_OUTPUT |
| 146 | + cat commit_summary.md >> $GITHUB_OUTPUT |
| 147 | + echo "EOF" >> $GITHUB_OUTPUT |
| 148 | +
|
| 149 | + - name: Create new PR (if none exists) |
| 150 | + if: steps.commits.outputs.has_updates == 'true' && steps.existing_pr.outputs.has_existing_pr == 'false' |
| 151 | + uses: peter-evans/create-pull-request@v6 |
| 152 | + with: |
| 153 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 154 | + branch: ${{ steps.existing_pr.outputs.branch_name }} |
| 155 | + title: "Sync template updates" |
| 156 | + body: | |
| 157 | + This pull request applies new commits from the template repository. |
| 158 | +
|
| 159 | + - **Template:** `${{ steps.origin.outputs.template_repo }}` |
| 160 | + - **Branch:** `${{ steps.origin.outputs.template_branch }}` |
| 161 | + - **Base commit:** `${{ steps.origin.outputs.template_commit }}` |
| 162 | +
|
| 163 | + --- |
| 164 | + ${{ steps.summary.outputs.commit_summary }} |
| 165 | + --- |
| 166 | +
|
| 167 | + ⚠️ Resolve any conflicts manually before merging. |
| 168 | + commit-message: "Sync template updates" |
| 169 | + labels: template-sync |
| 170 | + |
| 171 | + - name: Update existing PR body (if already open) |
| 172 | + if: steps.commits.outputs.has_updates == 'true' && steps.existing_pr.outputs.has_existing_pr == 'true' |
| 173 | + env: |
| 174 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 175 | + REPO: ${{ github.repository }} |
| 176 | + run: | |
| 177 | + pr_number=${{ steps.existing_pr.outputs.existing_pr_number }} |
| 178 | + echo "Updating PR #$pr_number..." |
| 179 | + bodyfile="updated_body.md" |
| 180 | + cat <<EOF > "$bodyfile" |
| 181 | +This pull request applies new commits from the template repository. |
| 182 | + |
| 183 | +- **Template:** \`${{ steps.origin.outputs.template_repo }}\` |
| 184 | +- **Branch:** \`${{ steps.origin.outputs.template_branch }}\` |
| 185 | +- **Base commit:** \`${{ steps.origin.outputs.template_commit }}\` |
| 186 | + |
| 187 | +--- |
| 188 | +${{ steps.summary.outputs.commit_summary }} |
| 189 | +--- |
| 190 | + |
| 191 | +⚠️ Resolve any conflicts manually before merging. |
| 192 | +EOF |
| 193 | + gh pr edit "$pr_number" --repo "$REPO" --body-file "$bodyfile" |
0 commit comments