Skip to content

Sync changes from template #87

Sync changes from template

Sync changes from template #87

name: Sync changes from template
# 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:
# Schedule the workflow to run once a day at midnight UTC
schedule:
- cron: "0 0 * * *"
# Trigger the workflow when the workflow file itself is modified
push:
paths:
- .github/workflows/sync-from-template.yml
# Allow manual triggering of the workflow
workflow_dispatch:
env:
BASE_BRANCH: master
HEAD_BRANCH: chore/sync-from-template
GIT_AUTHOR_NAME: ${{ github.repository_owner }}
GIT_AUTHOR_EMAIL: ${{ github.repository_owner }}@users.noreply.github.com
REPO_TEMPLATE: Witteborn/GitHubBoilerplate
THIS_REPO: ${{ github.repository }}
jobs:
sync-from-template:
# 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: false # Set to false to fail the workflow on errors
steps:
# Step 1: Checkout the template repository using the new PAT
- name: Check out template repository
uses: actions/checkout@v3
with:
repository: ${{ env.REPO_TEMPLATE }}
token: ${{ secrets.SYNC_TOKEN }} # Use the new PAT
path: template-repo
# Step 2: Checkout the target repository using the new PAT
- name: Check out target repository
uses: actions/checkout@v3
with:
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 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}"
# Step 4: Copy files from the template to the target repository
- name: Copy template contents
run: |
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: |
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:
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: |
# 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."