Skip to content

Sync with template repository #272

Sync with template repository

Sync with template repository #272

# 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:
schedule:
- cron: 11 11 * * *
permissions:
contents: read
env:
# 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: jenstroeger@users.noreply.github.com
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@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
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@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
with:
token: ${{ secrets.REPO_ACCESS_TOKEN }}
fetch-depth: 0
ref: staging
path: repo
- name: Sync with template
env:
GH_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}
run: |
LATEST_VERSION=$(cd template && git describe --tags --abbrev=0)
CURRENT_VERSION=$(test -f repo/.github/workflows/.template_version \
&& cat repo/.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.
pushd template || exit
git diff "$CURRENT_VERSION..$LATEST_VERSION" "$(find . docs/ .github/ .github/workflows/ .github/codeql/ -maxdepth 1 -type f ! -name """*.md""" ! -name """.template_version""")" > diff.patch
popd || exit
# Apply the generated patch to the current repo.
pushd repo || exit
patch --strip 1 --batch --merge --input ../template/diff.patch || true
find . -name "*.orig" -type f -delete
find . -name "*.rej" -type f -delete
popd || exit
# 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 --title "chore: sync with package template $LATEST_VERSION" \
--body "This PR was generated automatically." --head "$BRANCH_NAME"
fi
fi