Skip to content

Commit e11d989

Browse files
committed
Add Chat-GPT created template-sync files
deleted: .github/workflows/init.yaml new file: .github/workflows/record_template_origin.yaml new file: .github/workflows/sync-template-updates.yaml modified: .github/workflows/template-sync.yml
1 parent 55f1589 commit e11d989

4 files changed

Lines changed: 178 additions & 29 deletions

File tree

.github/workflows/init.yaml

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
name: Record Template Origin
2+
3+
on:
4+
push:
5+
# Run only once, right after the repo is created from the template
6+
branches:
7+
- '**'
8+
9+
permissions:
10+
contents: write
11+
12+
jobs:
13+
record-origin:
14+
runs-on: ubuntu-latest
15+
if: github.run_number == 1 # Only run on first push
16+
17+
steps:
18+
- name: Checkout repository
19+
uses: actions/checkout@v5
20+
with:
21+
fetch-depth: 0
22+
23+
- name: Install GitHub CLI and jq
24+
run: sudo apt-get install -y gh jq
25+
26+
- name: Detect this repository's default branch
27+
id: branch
28+
env:
29+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
30+
run: |
31+
default_branch=$(gh repo view "${{ github.repository }}" --json defaultBranchRef --jq '.defaultBranchRef.name')
32+
echo "Default branch detected: $default_branch"
33+
echo "default_branch=$default_branch" >> $GITHUB_OUTPUT
34+
35+
- name: Get template repository info (via API)
36+
id: template
37+
env:
38+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
39+
REPO: ${{ github.repository }}
40+
run: |
41+
echo "🔍 Querying repository metadata for template source..."
42+
data=$(gh api "repos/$REPO" --jq '.template_repository | {full_name: .full_name, default_branch: .default_branch}')
43+
44+
if [ "$data" = "null" ] || [ -z "$data" ]; then
45+
echo "⚠️ No template repository detected for this repo."
46+
template_repo="none"
47+
template_branch="none"
48+
template_commit="none"
49+
else
50+
template_repo=$(echo "$data" | jq -r '.full_name')
51+
template_branch=$(echo "$data" | jq -r '.default_branch')
52+
53+
# Get latest commit from template repo
54+
template_commit=$(gh api "repos/$template_repo/commits/$template_branch" --jq '.sha')
55+
fi
56+
57+
echo "Template repository: $template_repo"
58+
echo "Template branch: $template_branch"
59+
echo "Template commit: $template_commit"
60+
61+
echo "template_repo=$template_repo" >> $GITHUB_OUTPUT
62+
echo "template_branch=$template_branch" >> $GITHUB_OUTPUT
63+
echo "template_commit=$template_commit" >> $GITHUB_OUTPUT
64+
65+
- name: Record template origin file
66+
run: |
67+
echo "Template: ${{ steps.template.outputs.template_repo }}" > TEMPLATE_ORIGIN.txt
68+
echo "Template Branch: ${{ steps.template.outputs.template_branch }}" >> TEMPLATE_ORIGIN.txt
69+
echo "Template Commit: ${{ steps.template.outputs.template_commit }}" >> TEMPLATE_ORIGIN.txt
70+
echo "Recorded template origin:"
71+
cat TEMPLATE_ORIGIN.txt
72+
73+
- name: Commit and push template origin record
74+
env:
75+
BRANCH_NAME: ${{ steps.branch.outputs.default_branch }}
76+
run: |
77+
git config user.name "github-actions[bot]"
78+
git config user.email "github-actions[bot]@users.noreply.github.com"
79+
git add TEMPLATE_ORIGIN.txt
80+
git commit -m "Record template origin commit" || echo "No changes to commit"
81+
git push origin "HEAD:$BRANCH_NAME"
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
name: Sync Template Updates
2+
3+
on:
4+
schedule:
5+
- cron: '0 0 * * *' # Runs daily at midnight UTC
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: write
10+
pull-requests: write
11+
12+
jobs:
13+
sync-template:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout repository
18+
uses: actions/checkout@v5
19+
with:
20+
fetch-depth: 0
21+
22+
- name: Read template origin
23+
id: template
24+
run: |
25+
if [ ! -f TEMPLATE_ORIGIN.txt ]; then
26+
echo "TEMPLATE_ORIGIN.txt not found. Skipping sync."
27+
echo "skip_sync=true" >> $GITHUB_OUTPUT
28+
exit 0
29+
fi
30+
31+
template_repo=$(sed -n 's/^Template: //p' TEMPLATE_ORIGIN.txt)
32+
template_branch=$(sed -n 's/^Template Branch: //p' TEMPLATE_ORIGIN.txt)
33+
template_commit=$(sed -n 's/^Template Commit: //p' TEMPLATE_ORIGIN.txt)
34+
35+
echo "Template repo: $template_repo"
36+
echo "Template branch: $template_branch"
37+
echo "Template commit: $template_commit"
38+
39+
# Skip if placeholder values
40+
if [ "$template_repo" = "none" ] || [ "$template_branch" = "none" ] || [ "$template_commit" = "none" ]; then
41+
echo "Repository was not created from a template. Skipping sync."
42+
echo "skip_sync=true" >> $GITHUB_OUTPUT
43+
exit 0
44+
fi
45+
46+
echo "skip_sync=false" >> $GITHUB_OUTPUT
47+
echo "template_repo=$template_repo" >> $GITHUB_OUTPUT
48+
echo "template_branch=$template_branch" >> $GITHUB_OUTPUT
49+
echo "template_commit=$template_commit" >> $GITHUB_OUTPUT
50+
51+
- name: Install GitHub CLI
52+
if: steps.template.outputs.skip_sync == 'false'
53+
run: sudo apt-get install -y gh jq
54+
55+
- name: Fetch commits from template since last sync
56+
if: steps.template.outputs.skip_sync == 'false'
57+
id: commits
58+
run: |
59+
# Get the list of commits from template repo that are not yet in this repo
60+
commits=$(gh api repos/${{ steps.template.outputs.template_repo }}/commits --jq '.[] | .sha' | tac)
61+
echo "$commits" > template_commits.txt
62+
echo "Commits to consider:"
63+
cat template_commits.txt
64+
echo "commits=$(cat template_commits.txt)" >> $GITHUB_OUTPUT
65+
66+
- name: Cherry-pick commits and create PR
67+
if: steps.template.outputs.skip_sync == 'false'
68+
env:
69+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
70+
run: |
71+
git config user.name "github-actions[bot]"
72+
git config user.email "github-actions[bot]@users.noreply.github.com"
73+
74+
branch_name="template-sync-$(date +%Y%m%d%H%M%S)"
75+
git checkout -b "$branch_name"
76+
77+
pr_message="Template Sync Commit Summary:\n\n"
78+
79+
while read commit_sha; do
80+
# Skip empty lines
81+
[ -z "$commit_sha" ] && continue
82+
echo "Cherry-picking $commit_sha"
83+
git cherry-pick "$commit_sha" || git cherry-pick --abort
84+
pr_message="$pr_message- $commit_sha\n"
85+
done < template_commits.txt
86+
87+
git push origin "$branch_name"
88+
89+
# Create PR
90+
gh pr create \
91+
--title "Sync Template Updates" \
92+
--body "$pr_message" \
93+
--base main \
94+
--head "$branch_name" \
95+
--label "template-sync"

.github/workflows/template-sync.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ name: "Sync with Az-RBSI Template"
33

44
on:
55
# cronjob trigger
6-
schedule:
7-
- cron: "30 04 * * 1"
6+
# schedule:
7+
# - cron: "30 04 * * 1"
88
# manual trigger
99
workflow_dispatch:
1010
jobs:

0 commit comments

Comments
 (0)