Skip to content

fix: 🐛 fix copier update #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions template/.github/workflows/update-from-template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Update from template

on:
workflow_dispatch:
schedule:
# Every day at 3:30 at night.
- cron: '30 3 * * *'

# Limit token permissions for security
permissions: read-all

jobs:
update-from-template:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
with:
egress-policy: audit

- name: Check out repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

- name: Install Python
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
with:
python-version: "3.13"

- name: Install dependencies
run: |
sudo apt install pipx
pipx ensurepath
pipx install uv rust-just copier

- name: Set User
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"

- name: Pull request with updates from template
run: |
just update-from-template
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this to use the just recipe

any_changes=$(git status --porcelain=v1 2>/dev/null | wc -l)
if [ "$any_changes" -eq 0 ]; then
echo "No updates from the template detected, and no changes found. Stopping and exiting."
exit 0
fi
git checkout -b chore/update-from-template
git add .
git commit -m "chore(sync): :hammer: update changes from template"
gh pr create \
--title "chore(sync): :hammer: update changes from template" \
--body "This PR is automatically generated by the 'update-from-template' workflow. It syncs the latest changes from the template repository with this repository."
3 changes: 2 additions & 1 deletion template/justfile.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ build-contributors:

# Check for and apply updates from the template
update-from-template:
uvx copier update --trust --defaults
# Do not update existing source files
uvx copier update --trust --defaults $(find src/{{ github_repo_snake_case }} -type f -printf "--exclude %p ")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem was that running copier update ... deleted some files in the src/{{ github_repo_snake_case }} folder. Maybe because the folder name is templated?

In any case, there is a _skip_if_exists property in copier.yml, where you can set which files not to touch if they exist. Setting this worked for some test files I put in the folder, but interestingly not for __init__.py and py.typed. Even skipping all files in the folder didn't catch these two 🤔 .

The _exclude property is similar, but it stays in force regardless of the file existing, so a deleted file will not be recreated.

This version with the --exclude flag is a workaround: it excludes all files in the folder that exist, recreating the behaviour of _skip_if_exists in copier.yml. We could narrow it to only the two problematic files if this is too broad.

Any other ideas welcome!!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Yea, I had been wondering about this.. We'll have to see how we use all of this stuff in practice, will be interesting!


# Reset repo changes to match the template
reset-from-template:
Expand Down
2 changes: 1 addition & 1 deletion template/{{_copier_conf.answers_file}}.jinja
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Changes here will be overwritten by Copier; NEVER EDIT MANUALLY
{{ dict(_copier_answers, copyright_year=copyright_year) | to_nice_yaml -}}
{{ dict(_copier_answers, github_repo=github_repo, copyright_year=copyright_year) | to_nice_yaml -}}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise copier will use a temp folder name it uses under the hood

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this change and how it relates to this PR. What is this doing?

Loading