Skip to content

[pre-commit.ci] pre-commit autoupdate (#486) #1131

[pre-commit.ci] pre-commit autoupdate (#486)

[pre-commit.ci] pre-commit autoupdate (#486) #1131

name: Create PRs to the instance repo
on:
pull_request:
branches: [main]
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
env:
PYTHON_VERSION: "3.14"
INSTANCE_REPO_GITHUB: scverse/cookiecutter-scverse-instance
INSTANCE_REPO: "TEMPLATE_INSTANCE"
# directory in which cookiecuter generates the new repository
INSTANCE_GENERATED: "TEMPLATE_INSTANCE_GENERATED"
PROJECT_NAME: "cookiecutter-scverse-instance"
APP_USERNAME: "scverse-pr-creator[bot]"
APP_EMAIL: "272303624+scverse-pr-creator[bot]@users.noreply.github.com"
steps:
- name: Install the latest version of uv
uses: astral-sh/setup-uv@v7
with:
cache-dependency-glob: .github/workflows/deploy-instance-repo.yml
- name: Set git identity
run: |
git config --global user.name "${{ env.APP_USERNAME }}"
git config --global user.email "${{ env.APP_EMAIL }}"
- name: Checkout template repository
uses: actions/checkout@v6
- name: Generate tokens
id: app-token
uses: actions/create-github-app-token@v2
with:
app-id: ${{ vars.PR_CREATOR_APP_ID }}
private-key: ${{ secrets.PR_CREATOR_PRIVATE_KEY }}
repositories: cookiecutter-scverse-instance
- name: Authenticate gh CLI with app token
run: echo "${{ steps.app-token.outputs.token }}" | gh auth login --with-token
- name: Checkout instance repository
uses: actions/checkout@v6
with:
repository: ${{ env.INSTANCE_REPO_GITHUB }}
token: ${{ steps.app-token.outputs.token }}
path: ${{ env.INSTANCE_REPO }}
persist-credentials: true
- name: define sister PR branch name for pull request
if: github.event_name == 'pull_request'
run: |
echo "SISTER_PR_BRANCH=pr-${{ github.event.pull_request.number }}" >> $GITHUB_ENV
- name: define branch name for push event
if: github.event_name == 'push'
run: |
echo "SISTER_PR_BRANCH=main" >> $GITHUB_ENV
- name: Build from template
run: |
uvx --with pre-commit cruft create \
--no-input \
--output-dir $INSTANCE_GENERATED \
--extra-context-file .github/assets/cookiecutter-scverse-instance.json \
.
- name: Transfer changes to instance repo
run: |
# checkout branch or create if it does not exist
git fetch origin $SISTER_PR_BRANCH || true
git checkout $SISTER_PR_BRANCH 2>/dev/null || git checkout -b $SISTER_PR_BRANCH
# transfer changes from generated repo
rsync -va --delete --exclude .git $INSTANCE_GENERATED/$PROJECT_NAME/ $INSTANCE_REPO
# check if there is a commit to be made
if [[ `git status --porcelain` ]]; then
echo "GIT_HAS_CHANGES=TRUE" >> $GITHUB_ENV
else
echo "GIT_HAS_CHANGES=FALSE" >> $GITHUB_ENV
fi
env:
GIT_WORK_TREE: ${{ env.INSTANCE_REPO }}
GIT_DIR: ${{ env.INSTANCE_REPO}}/.git
- name: Commit changes
if: ${{ env.GIT_HAS_CHANGES == 'TRUE' }}
uses: EndBug/add-and-commit@v9
with:
author_name: ${{ env.APP_USERNAME }}
author_email: ${{ env.APP_EMAIL }}
default_author: user_info
commit: "--no-verify" # no need to run pre-commit at this point - saves runtime!
cwd: ${{ env.INSTANCE_REPO }}
message: Update instance repo from cookiecutter template
new_branch: ${{ env.SISTER_PR_BRANCH }}
push: false
- name: Push to instance repo
if: ${{ env.GIT_HAS_CHANGES == 'TRUE' }}
uses: ad-m/github-push-action@v0.8.0
with:
github_token: ${{ steps.app-token.outputs.token }}
branch: ${{ env.SISTER_PR_BRANCH }}
repository: scverse/cookiecutter-scverse-instance
force: false
directory: ${{ env.INSTANCE_REPO }}
# Run the following steps only on the pull request event -- in the other case we just committed
# to the main branch and are done with it.
- name: Check if "sister PR" exists and find its ID
if: github.event_name == 'pull_request'
run: |
PR_ID=$( \
gh pr view $SISTER_PR_BRANCH \
-R $INSTANCE_REPO_GITHUB \
--json number --jq '.number' \
|| echo "NA"
)
echo "SISTER_PR_ID=$PR_ID" >> $GITHUB_ENV
- name: Create sister PR in instance repo
if: ${{ github.event_name == 'pull_request' && env.GIT_HAS_CHANGES == 'TRUE' && env.SISTER_PR_ID == 'NA' }}
run: |
cd $INSTANCE_REPO
gh pr create \
--base main \
--head $SISTER_PR_BRANCH \
--body "Created automatically from scverse/cookiecutter-scverse#${{ github.event.pull_request.number }}" \
--title "Update instance repo (PR ${{ github.event.pull_request.number }} -- ${{ github.event.pull_request.title }})" \
-R $INSTANCE_REPO_GITHUB
# now an ID should be available
PR_ID=$( \
gh pr view $SISTER_PR_BRANCH \
-R $INSTANCE_REPO_GITHUB \
--json number --jq '.number' \
)
echo "SISTER_PR_ID=$PR_ID" >> $GITHUB_ENV
- name: Add comment about sister PR
if: ${{ github.event_name == 'pull_request' && env.GIT_HAS_CHANGES == 'TRUE' }}
uses: mshick/add-pr-comment@v2
with:
message: |
A PR has been generated to the instance repo: ${{ env.INSTANCE_REPO_GITHUB }}#${{ env.SISTER_PR_ID }}
[![Checks Status][checks-badge]][checks] [![Documentation Preview][docs-badge]][docs]
You can check out the PR to preview your changes in an instance of the cookiecutter template.
It will be kept in sync with this PR automatically.
[checks-badge]: https://github.com/scverse/cookiecutter-scverse-instance/actions/workflows/test.yaml/badge.svg?branch=pr-${{ github.event.pull_request.number }}
[checks]: https://github.com/scverse/cookiecutter-scverse-instance/pull/${{ env.SISTER_PR_ID }}/checks
[docs-badge]: https://app.readthedocs.org/projects/cookiecutter-scverse-instance/badge/?version=${{ env.SISTER_PR_ID }}
[docs]: https://cookiecutter-scverse-instance--${{ env.SISTER_PR_ID }}.org.readthedocs.build/
# use default token, which allows to comment in the cookiecutter-scverse repo.
# The token generated by the app is scoped to the cookiecutter-scverse-instance repo instead.
repo-token: ${{ secrets.GITHUB_TOKEN }}
allow-repeats: false
- name: Query status of checks in template repository
if: ${{ github.event_name == 'pull_request' && env.SISTER_PR_ID != 'NA' }}
run: |
sleep 5 # without sleep, 0 checks are discovered.
gh pr checks ${{ env.SISTER_PR_ID }} --watch -R $INSTANCE_REPO_GITHUB