-
Notifications
You must be signed in to change notification settings - Fork 11
90 lines (76 loc) · 3.62 KB
/
sync-with-upstream.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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