Skip to content
Merged
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
120 changes: 120 additions & 0 deletions .github/workflows/update-example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
name: Update Example Repo

on:
pull_request:
types: [opened, synchronize, reopened]
push:
branches: [main]

permissions:
pull-requests: write

jobs:
update-example:
runs-on: ubuntu-latest
environment: cookiecutter-uv-example
# Forks don't have access to secrets, skip them
if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository
steps:
- name: Check out
uses: actions/checkout@v4

- name: Set up the environment
uses: ./.github/actions/setup-python-env

- name: Determine target branch
id: branch
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
echo "name=preview/pr-${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT
else
echo "name=main" >> $GITHUB_OUTPUT
fi

- name: Bake example project
run: |
uv run cookiecutter --no-input . --overwrite-if-exists \
author="Example User" \
email="example@osprey-oss.org" \
author_github_handle="osprey-oss" \
project_name="cookiecutter-uv-example"

- name: Patch baked workflow to trigger on preview branches
if: github.event_name == 'pull_request'
run: |
python -c "
import pathlib
f = pathlib.Path('cookiecutter-uv-example/.github/workflows/main.yml')
f.write_text(f.read_text().replace(
' - main\n',
' - main\n - \"preview/**\"\n',
1
))
"

- name: Push to cookiecutter-uv-example
env:
BRANCH: ${{ steps.branch.outputs.name }}
TOKEN: ${{ secrets.EXAMPLE_REPO_PUSH_TOKEN }}
run: |
cd cookiecutter-uv-example
uv sync
git init
git checkout -b "$BRANCH"
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add .
uv run pre-commit run -a || true
git add .
git commit -m "chore: sync from cookiecutter-uv@${{ github.sha }}"
git remote add origin "https://x-access-token:${TOKEN}@github.com/osprey-oss/cookiecutter-uv-example.git"
git push -f origin "$BRANCH"

- name: Post or update PR comment
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const branch = '${{ steps.branch.outputs.name }}';
const sha = '${{ github.sha }}';
const marker = '<!-- cookiecutter-example-bot -->';
const encodedBranch = branch.split('/').map(encodeURIComponent).join('/');
const repoUrl = 'https://github.com/osprey-oss/cookiecutter-uv-example';

const body = [
marker,
'### 🍪 Example repo updated',
'',
`The baked example for this PR has been pushed to [osprey-oss/cookiecutter-uv-example](${repoUrl}/tree/${branch}).`,
'',
'| | |',
'|---|---|',
`| Branch | [\`${branch}\`](${repoUrl}/tree/${branch}) |`,
`| CI | [View Actions →](${repoUrl}/actions?query=branch%3A${encodedBranch}) |`,
`| Synced at | \`${sha.slice(0, 7)}\` |`,
'',
'_Uses default configuration. Non-default options are validated by the slow tests in this repo\'s CI._',
].join('\n');

const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});

const existing = comments.find(c => c.body.includes(marker));
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body,
});
}
21 changes: 0 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,6 @@ bake-src: ## bake without inputs and overwrite if exists.
bake-with-inputs: ## bake with inputs and overwrite if exists.
@uv run cookiecutter . --overwrite-if-exists

.PHONY: bake-and-test-deploy
bake-and-test-deploy: ## For quick publishing to cookiecutter-uv-example to test GH Actions
@rm -rf cookiecutter-uv-example || true
@uv run cookiecutter --no-input . --overwrite-if-exists \
author="Florian Maas" \
email="foo@email.com" \
github_author_handle=fpgmaas \
project_name=cookiecutter-uv-example \
project_slug=cookiecutter_uv_example
@cd cookiecutter-uv-example; uv sync && \
git init -b main && \
git add . && \
uv run pre-commit install && \
uv run pre-commit run -a || true && \
git add . && \
uv run pre-commit run -a || true && \
git add . && \
git commit -m "init commit" && \
git remote add origin git@github.com:osprey-oss/cookiecutter-uv-example.git && \
git push -f origin main


.PHONY: install
install: ## Install the virtual environment
Expand Down
Loading