Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ci): workflow that syncs a repository with this template had multiple problems on patch generation and pushing; also, it can now be triggered via Github UI #670

Merged
merged 1 commit into from
Jan 20, 2024
Merged
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
33 changes: 16 additions & 17 deletions .github/workflows/sync-with-upstream.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@

name: Sync with template repository
on:
workflow_dispatch:

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:
Expand Down Expand Up @@ -44,14 +41,18 @@ jobs:
- 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: jenstroeger@users.noreply.github.com
working-directory: ./repo
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."
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
if [ "${CURRENT_VERSION}" == "${LATEST_VERSION}" ]; then
echo "Unable to find a new version, exiting..."
else

Expand All @@ -61,17 +62,16 @@ jobs:
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
# 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.
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"
Expand All @@ -84,8 +84,7 @@ jobs:
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"
gh pr create --base staging --head "$BRANCH_NAME" --title "chore: sync with template $LATEST_VERSION" --body "This PR was generated automatically."

fi
fi