This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
Update from Template #44
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
name: Update from Template | |
# This workflow keeps the repo up to date with changes from the template repo (REMOTE_URL) | |
# It duplicates the REMOTE_BRANCH (into UPDATE_BRANCH) and tries to merge it into the | |
# this repos default branch (which is checked out here) | |
# Note that this requires a PAT (Personal Access Token) - at best from a servicing account | |
# Also note that you should have at least once merged the template repo into the current repo manually | |
# otherwise a "refusing to merge unrelated histories" error might occur. | |
on: | |
schedule: | |
- cron: '55 2 * * 1' | |
workflow_dispatch: | |
env: | |
UPDATE_BRANCH: update-from-template | |
REMOTE_URL: https://github.com/xdev-software/xdev-swing-framework-template.git | |
REMOTE_BRANCH: master | |
permissions: | |
contents: write | |
pull-requests: write | |
jobs: | |
update: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
# Required because otherwise there are always changes detected when executing diff/rev-list | |
fetch-depth: 0 | |
# If no PAT is used the following error occurs on a push: | |
# refusing to allow a GitHub App to create or update workflow `.github/workflows/xxx.yml` without `workflows` permission | |
token: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }} | |
- name: Init Git | |
run: | | |
git config --global user.email "actions@github.com" | |
git config --global user.name "GitHub Actions" | |
- name: Main workflow | |
id: main | |
run: | | |
echo "Adding remote template-repo" | |
git remote add template ${{ env.REMOTE_URL }} | |
echo "Fetching remote template repo" | |
git fetch template | |
echo "Deleting local branch that will contain the updates - if present" | |
git branch -D ${{ env.UPDATE_BRANCH }} || true | |
echo "Checking if the remote template repo has new commits" | |
git rev-list ..template/${{ env.REMOTE_BRANCH }} | |
if [ $(git rev-list --count ..template/${{ env.REMOTE_BRANCH }}) -eq 0 ]; then | |
echo "There are no commits new commits on the template repo" | |
echo "Deleting origin branch that contains the updates - if present" | |
git push -f origin --delete ${{ env.UPDATE_BRANCH }} || true | |
echo "abort=1" >> $GITHUB_OUTPUT | |
exit 0 | |
fi | |
echo "Found new commits on the template repo" | |
echo "Creating update branch" | |
git branch ${{ env.UPDATE_BRANCH }} template/${{ env.REMOTE_BRANCH }} | |
git branch --unset-upstream ${{ env.UPDATE_BRANCH }} | |
echo "Pushing update branch" | |
git push -f -u origin ${{ env.UPDATE_BRANCH }} | |
echo "Getting current branch" | |
current_branch=$(git branch --show-current) | |
echo "Current branch is $current_branch" | |
echo "current_branch=$current_branch" >> $GITHUB_OUTPUT | |
echo "abort=0" >> $GITHUB_OUTPUT | |
- name: pull-request | |
if: steps.main.outputs.abort == 0 | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
gh_pr_up() { | |
gh pr create "$@" || gh pr edit "$@" | |
} | |
gh_pr_up -B "${{ steps.main.outputs.current_branch }}" \ | |
-H "${{ env.UPDATE_BRANCH }}" \ | |
--title "Update from template" \ | |
--body "An automated PR to sync changes from the template into this repo" |