Sync with template repository #657
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a PR to sync with the upstream template repo. | |
# The template repo is https://github.com/jenstroeger/python-package-template. | |
name: Sync with template repository | |
on: | |
workflow_dispatch: | |
schedule: | |
- cron: 11 11 * * * | |
permissions: | |
contents: read | |
jobs: | |
sync: | |
if: github.repository != 'jenstroeger/python-package-template' | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
pull-requests: write | |
steps: | |
- name: Check out template repository | |
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | |
with: | |
# If you decide to change the upstream template repository to a private one, uncomment | |
# the following argument to pass the required token to be able to check it out. | |
# token: ${{ secrets.<TEMPLATE_REPO_TOKEN> }} | |
repository: jenstroeger/python-package-template | |
fetch-depth: 0 | |
path: template | |
- name: Check out current repository | |
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | |
with: | |
token: ${{ secrets.REPO_ACCESS_TOKEN }} | |
fetch-depth: 0 | |
ref: staging | |
path: repo | |
- name: Sync with template | |
env: | |
GH_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }} | |
# This is the username and email for the user who creates a branch and commits | |
# the changes. In an organisation that should be a dedicated devops account. | |
USER_NAME: jenstroeger | |
USER_EMAIL: [email protected] | |
working-directory: ./repo | |
run: | | |
LATEST_VERSION=$(cd ../template && git describe --tags --abbrev=0) | |
CURRENT_VERSION=$(test -f .github/workflows/.template_version && cat .github/workflows/.template_version || echo "v0.0.0") | |
echo "Latest version is ${LATEST_VERSION} and current version is ${CURRENT_VERSION}." | |
# Check if the template repo was changed/updated. | |
if [ "${CURRENT_VERSION}" == "${LATEST_VERSION}" ]; then | |
echo "Unable to find a new version, exiting..." | |
else | |
# Check if the branch already exists in the current repo. | |
BRANCH_NAME="sync-$LATEST_VERSION" | |
if [ "$(git rev-parse --verify origin/"""$BRANCH_NAME""" 2>/dev/null)" ]; then | |
echo "Branch $BRANCH_NAME already exists, exiting..." | |
else | |
# Generate a patch file of all template changes in the cloned template repository. | |
pushd ../template || exit | |
# shellcheck disable=SC2046 | |
git diff "${CURRENT_VERSION}".."${LATEST_VERSION}" -- $(find . docs/ .github/ .github/workflows/ -maxdepth 1 -type f ! -name "*.md" ! -name ".template_version") > diff.patch | |
popd || exit | |
# Apply the generated patch to the current repo. | |
patch --strip 1 --batch --merge --input ../template/diff.patch || true | |
find . -name "*.orig" -type f -delete | |
find . -name "*.rej" -type f -delete | |
# Create a branch, commit, and push the changeset. | |
git checkout -b "$BRANCH_NAME" | |
echo "$LATEST_VERSION" > .github/workflows/.template_version | |
git add . | |
git config --global user.name "$USER_NAME" | |
git config --global user.email "$USER_EMAIL" | |
git config --list --global # For debug purposes. | |
git commit --message "chore: sync with template $LATEST_VERSION" | |
git push --set-upstream origin "$BRANCH_NAME" | |
# Create the pull request. | |
gh pr create --base staging --head "$BRANCH_NAME" --title "chore: sync with template $LATEST_VERSION" --body "This PR was generated automatically." | |
fi | |
fi |