Skip to content

Commit

Permalink
Update sync-from-template.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
Witteborn authored Sep 20, 2024
1 parent bf0dc80 commit 8ee27da
Showing 1 changed file with 76 additions and 77 deletions.
153 changes: 76 additions & 77 deletions .github/workflows/sync-from-template.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
---
#
# This workflow implementation has been copied from https://github.com/solvaholic/template
# - Run this workflow to pull changes from the template repository.
#
# - Clone this repository. Check out a branch
# - Copy files from the template onto this clone
# - Push the branch to this repository
# - Create a pull request in this repository
#
name: Sync changes from template

# TODO:
# - Switch to gh. Check https://github.com/cli/cli/issues/297 for updates
# Define the permissions required for the GITHUB_TOKEN
permissions:
contents: write # Allows the workflow to push commits
pull-requests: write # Allows the workflow to create pull requests

on:
# Run at 0517 UTC each Friday
# Schedule the workflow to run once a day at midnight UTC
schedule:
- cron: "17 5 * * 5"
- cron: "0 0 * * *"

# Run when this file changes
# Trigger the workflow when the workflow file itself is modified
push:
paths:
- .github/workflows/sync-from-template.yml

# Run when manually triggered
# Allow manual triggering of the workflow
workflow_dispatch:

env:
Expand All @@ -36,82 +28,89 @@ env:

jobs:
sync-from-template:
# Do not run on the template repository itself
# Prevent the workflow from running on the template repository itself
if: github.repository != 'Witteborn/GitHubBoilerplate'
name: Sync changes from Witteborn/GitHubBoilerplate
runs-on: ubuntu-latest
continue-on-error: true
continue-on-error: false # Set to false to fail the workflow on errors

steps:
# Clone the template repository
# Step 1: Checkout the template repository using the new PAT
- name: Check out template repository
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
repository: ${{ env.REPO_TEMPLATE }}
token: ${{ github.token }}
path: ${{ env.REPO_TEMPLATE }}
token: ${{ secrets.SYNC_TOKEN }} # Use the new PAT
path: template-repo

# Clone the target repository. Check out a branch
- name: Check out ${{ github.repository }}
uses: actions/checkout@v2
# Step 2: Checkout the target repository using the new PAT
- name: Check out target repository
uses: actions/checkout@v3
with:
repository: ${{ github.repository }}
token: ${{ github.token }}
path: ${{ github.repository }}
- name: Create branch in ${{ env.THIS_REPO }}
repository: ${{ env.THIS_REPO }}
token: ${{ secrets.SYNC_TOKEN }} # Use the new PAT
path: target-repo

# Step 3: Create or switch to the synchronization branch
- name: Create or switch to sync branch
run: |
git -C "${THIS_REPO}" fetch origin "${HEAD_BRANCH}" || true
git -C "${THIS_REPO}" branch -a
git -C "${THIS_REPO}" checkout -B "${HEAD_BRANCH}" \
"remotes/origin/${HEAD_BRANCH}" || \
git -C "${THIS_REPO}" checkout -b "${HEAD_BRANCH}"
git -C target-repo fetch origin "${HEAD_BRANCH}" || true
git -C target-repo checkout -B "${HEAD_BRANCH}" "origin/${HEAD_BRANCH}" || git -C target-repo checkout -b "${HEAD_BRANCH}"
# Copy files from the template onto the target clone
# Step 4: Copy files from the template to the target repository
- name: Copy template contents
run: |
_files="$(find ${REPO_TEMPLATE} \
! -path "*/.git/*" \
! -path "*/.github/workflows/*" \
! -name ".gitignore" \
! -name "README.md" \
-type f \
-print)"
for _file in ${_files}; do
_src="${_file}"
_dst="${THIS_REPO}/${_file#${REPO_TEMPLATE}/}"
# TODO: Find a more robust / elegant way to get this :point_down:
_dst="${_dst%/*}/"
mkdir -p "${_dst}"
echo "INFO: Copy '${_src}' to '${_dst}'."
cp "${_src}" "${_dst}"
done
git -C "${THIS_REPO}" diff
# Commit changes, if there are any
- name: Commit changes, if any
rsync -av --exclude='.git/' --exclude='.gitignore' --exclude='README.md' template-repo/ target-repo/
git -C target-repo status
# Step 5: Commit changes if there are any
- name: Commit changes
run: |
git -C target-repo config user.name "${GIT_AUTHOR_NAME}"
git -C target-repo config user.email "${GIT_AUTHOR_EMAIL}"
git -C target-repo add .
git -C target-repo commit -m "Sync from template@${{ github.sha }}" || echo "No changes to commit."
# Step 6: Push the synchronization branch to the target repository
- name: Push synchronization branch
run: git -C target-repo push -u origin "${HEAD_BRANCH}"

# Step 7: Install GitHub CLI
- name: Install GitHub CLI
run: |
git -C ${THIS_REPO} config user.name "${GIT_AUTHOR_NAME}"
git -C ${THIS_REPO} config \
user.email "${GIT_AUTHOR_EMAIL}"
git -C ${THIS_REPO} add .
git -C ${THIS_REPO} commit \
-m "Sync from template@${{ github.sha }}"
# Push the branch to the target repository
- name: Push topic branch
run: git -C ${THIS_REPO} push -u origin "${HEAD_BRANCH}"

# Create a pull request in the target repository
- name: Create pull request
type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y)
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg
sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
sudo apt update
sudo apt install gh -y
# Step 8: Authenticate GitHub CLI using the PAT
- name: Authenticate GitHub CLI
env:
GITHUB_TOKEN: ${{ github.token }}
SYNC_TOKEN: ${{ secrets.SYNC_TOKEN }}
run: |
unset GITHUB_TOKEN # Prevent 'gh' from using the default GITHUB_TOKEN
echo "${SYNC_TOKEN}" | gh auth login --with-token
# Step 9: Create a pull request using GitHub CLI
- name: Create Pull Request
env:
SYNC_TOKEN: ${{ secrets.SYNC_TOKEN }}
GITHUB_USER: ${{ github.actor }}
run: |
pushd ${THIS_REPO}
hub pull-request \
-b "${BASE_BRANCH}" \
-h "${HEAD_BRANCH}" \
--no-edit \
-m "Pull updates from ${REPO_TEMPLATE}" \
-m "Pull updates from ${REPO_TEMPLATE}"
popd
# Navigate to the target repository directory
cd target-repo
# Ensure the current directory is a git repository
if [ ! -d ".git" ]; then
echo "Error: target-repo is not a git repository."
exit 1
fi
# Create a pull request
gh pr create \
--base "${BASE_BRANCH}" \
--head "${HEAD_BRANCH}" \
--title "Sync updates from ${REPO_TEMPLATE}" \
--body "This pull request synchronizes changes from the ${REPO_TEMPLATE} template repository."

0 comments on commit 8ee27da

Please sign in to comment.