Skip to content

Commit 8751635

Browse files
Update template_update.yml
1 parent 020418e commit 8751635

1 file changed

Lines changed: 37 additions & 42 deletions

File tree

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# .github/workflows/Template_Update.yml
12
name: Template_Update
23

34
permissions:
@@ -21,6 +22,7 @@ jobs:
2122
runs-on: ubuntu-latest
2223

2324
steps:
25+
# ─────────────────────────── Setup ───────────────────────────
2426
- name: Checkout project repo
2527
uses: actions/checkout@v4
2628
with:
@@ -36,111 +38,104 @@ jobs:
3638
- name: Install dependencies
3739
run: pip install cookiecutter jq
3840

41+
# ───────────── Read old SHA & compare with template HEAD ─────────────
3942
- name: Read old template SHA
40-
id: get_old_sha
43+
id: old_sha
4144
run: |
42-
OLD_SHA=$(jq -r '.cookiecutter.template_sha // empty' .cookiecutter.json)
43-
if [ -z "$OLD_SHA" ]; then
45+
SHA=$(jq -r '.cookiecutter.template_sha // empty' .cookiecutter.json)
46+
if [ -z "$SHA" ]; then
4447
echo "::error ::.cookiecutter.json is missing cookiecutter.template_sha"
4548
exit 1
4649
fi
47-
echo "old_sha=$OLD_SHA" >> "$GITHUB_OUTPUT"
50+
echo "sha=$SHA" >> "$GITHUB_OUTPUT"
4851
4952
- name: Fetch new template SHA
50-
id: get_new_sha
53+
id: new_sha
5154
run: |
52-
NEW_SHA=$(git ls-remote "${{ inputs.template_repo }}" HEAD | cut -f1)
53-
echo "new_sha=$NEW_SHA" >> "$GITHUB_OUTPUT"
55+
echo "sha=$(git ls-remote '${{ inputs.template_repo }}' HEAD | cut -f1)" >> "$GITHUB_OUTPUT"
5456
55-
- name: Determine if SHA changed
57+
- name: Decide whether to continue
5658
run: |
57-
if [ "${{ steps.get_old_sha.outputs.old_sha }}" != "${{ steps.get_new_sha.outputs.new_sha }}" ]; then
58-
echo "SHA_CHANGED=true" >> "$GITHUB_ENV"
59-
else
59+
if [ "${{ steps.old_sha.outputs.sha }}" = "${{ steps.new_sha.outputs.sha }}" ]; then
6060
echo "SHA_CHANGED=false" >> "$GITHUB_ENV"
61+
else
62+
echo "SHA_CHANGED=true" >> "$GITHUB_ENV"
6163
fi
6264
63-
- name: Exit early if no change
65+
- name: Exit if SHA unchanged
6466
if: env.SHA_CHANGED == 'false'
6567
run: echo "✂️ SHA unchanged; nothing to update."
6668

67-
# ────────── Clone template repo (old & new) ──────────
69+
# ───────────────── Clone template repo at both SHAs ─────────────────
6870
- name: Clone template at OLD SHA
6971
if: env.SHA_CHANGED == 'true'
7072
run: |
71-
git clone "${{ inputs.template_repo }}" base-template
72-
git -C base-template checkout "${{ steps.get_old_sha.outputs.old_sha }}"
73+
git clone '${{ inputs.template_repo }}' base-template
74+
git -C base-template checkout '${{ steps.old_sha.outputs.sha }}'
7375
74-
- name: Clone template at NEW SHA (HEAD)
76+
- name: Clone template at NEW SHA
7577
if: env.SHA_CHANGED == 'true'
76-
run: git clone "${{ inputs.template_repo }}" template-source
78+
run: git clone '${{ inputs.template_repo }}' template-source
7779

78-
- name: Generate BASE template output (OLD SHA)
80+
# ────────────────────── Render OLD template ──────────────────────
81+
- name: Render OLD template (base-template → template-base)
7982
if: env.SHA_CHANGED == 'true'
8083
run: |
8184
rm -rf ~/.cookiecutters/*
8285
mkdir -p template-base
83-
84-
# Uncomment the next two lines only if you still need to inject
85-
# a missing variable into the replay file:
86-
# jq '.cookiecutter.templates = "main"' .cookiecutter.json > tmp && mv tmp .cookiecutter.json
87-
86+
# jq '.cookiecutter.templates = "main"' .cookiecutter.json > tmp && mv tmp .cookiecutter.json # <-- uncomment if keeping the variable
8887
cookiecutter base-template \
8988
--replay-file .cookiecutter.json \
9089
--overwrite-if-exists \
9190
--output-dir template-base
9291
93-
- name: Generate NEW template output (HEAD)
92+
# ────────────────────── Render NEW template ──────────────────────
93+
- name: Render NEW template (template-source → template-new)
9494
if: env.SHA_CHANGED == 'true'
9595
run: |
9696
rm -rf ~/.cookiecutters/*
9797
mkdir -p template-new
98-
98+
# jq '.cookiecutter.templates = "main"' .cookiecutter.json > tmp && mv tmp .cookiecutter.json # <-- same note
9999
cookiecutter template-source \
100100
--replay-file .cookiecutter.json \
101101
--overwrite-if-exists \
102102
--output-dir template-new
103103
104-
- name: Apply patch if changes & create PR
104+
# ───────────── diff, merge, bump SHA, push branch, open PR ─────────────
105+
- name: Apply patch & raise PR
105106
if: env.SHA_CHANGED == 'true'
106107
shell: bash
107108
run: |
108-
# Create unified diff between old and new renders
109109
diff -ruN template-base template-new > update.patch || true
110110
111111
if [ ! -s update.patch ]; then
112112
echo "ℹ️ No template diffs; only SHA bump will occur."
113113
else
114-
# Try a three-way merge; abort on conflicts
115114
if ! git apply --index --3way update.patch; then
116115
echo "::error ::Merge conflicts detected; aborting."
117116
exit 1
118117
fi
119118
echo "✅ Applied template changes"
120119
fi
121120
122-
# Bump the recorded SHA in .cookiecutter.json
123-
jq ".cookiecutter.template_sha = \"${{ steps.get_new_sha.outputs.new_sha }}\"" \
121+
jq ".cookiecutter.template_sha = \"${{ steps.new_sha.outputs.sha }}\"" \
124122
.cookiecutter.json > tmp && mv tmp .cookiecutter.json
125123
git add .cookiecutter.json
126124
127-
UPDATE_BRANCH="template-update-${{ steps.get_new_sha.outputs.new_sha }}"
128-
echo "UPDATE_BRANCH=$UPDATE_BRANCH" >> "$GITHUB_ENV"
129-
130-
# Commit and push
131-
git checkout -b "$UPDATE_BRANCH"
125+
BRANCH="template-update-${{ steps.new_sha.outputs.sha }}"
126+
git checkout -b "$BRANCH"
132127
git config user.name "GitHub Actions Bot"
133128
git config user.email "actions@github.com"
134-
git commit -am "chore: merge template updates ${{ steps.get_old_sha.outputs.old_sha }} → ${{ steps.get_new_sha.outputs.new_sha }}"
135-
git push origin "$UPDATE_BRANCH"
129+
git commit -am "chore: merge template updates ${{ steps.old_sha.outputs.sha }} → ${{ steps.new_sha.outputs.sha }}"
130+
git push origin "$BRANCH"
136131
137-
- name: Create Pull Request
132+
- name: Create pull request
138133
if: env.SHA_CHANGED == 'true'
139134
env:
140135
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
141136
run: |
142137
gh pr create \
143-
--title "chore: merge template updates ${{ steps.get_old_sha.outputs.old_sha }} → ${{ steps.get_new_sha.outputs.new_sha }}" \
144-
--body "Updates cookiecutter.template_sha from ${{ steps.get_old_sha.outputs.old_sha }} to ${{ steps.get_new_sha.outputs.new_sha }}" \
145-
--base "${{ inputs.repo_branch }}" \
146-
--head "$UPDATE_BRANCH"
138+
--title "chore: merge template updates ${{ steps.old_sha.outputs.sha }} → ${{ steps.new_sha.outputs.sha }}" \
139+
--body "Updates cookiecutter.template_sha from ${{ steps.old_sha.outputs.sha }} to ${{ steps.new_sha.outputs.sha }}" \
140+
--base '${{ inputs.repo_branch }}' \
141+
--head "$BRANCH"

0 commit comments

Comments
 (0)