Skip to content

Commit

Permalink
feat: refactor sync template workflow to work in a whitelist setting
Browse files Browse the repository at this point in the history
  • Loading branch information
zugdev committed Nov 8, 2024
1 parent d8aab2f commit 10486f6
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions .github/workflows/sync-template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: Sync branch to template

on:
workflow_dispatch:
inputs:
additional_files:
description: 'Comma-separated list of additional files to sync (i.e ".github/workflows/build.yml tsconfig.json")'
required: false
schedule:
- cron: '14 0 1 * *'

jobs:
sync:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Check if repository is ts-template
run: |
if [[ "${{ github.repository }}" == "ubiquity/ts-template" ]]; then
echo "Skipping sync: this is the template repository."
exit 0
fi
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Get GitHub App token
uses: tibdex/[email protected]
id: get_installation_token
with:
app_id: ${{ secrets.APP_ID }}
private_key: ${{ secrets.APP_PRIVATE_KEY }}

- name: Sync branch to template
env:
GH_TOKEN: ${{ steps.get_installation_token.outputs.token }}
WHITELIST_FILES: ".github/workflows/build.yml,tsconfig.json"
ADDITIONAL_FILES: ${{ github.event.inputs.additional_files }}
run: |
branch_name=$(git rev-parse --abbrev-ref HEAD)
original_remote=$(git remote get-url origin)
pr_branch="sync-template/${branch_name}"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
git checkout -b "$pr_branch"
git clone https://github.com/ubiquity/ts-template template-repo
# Convert ADDITIONAL_FILES input to an array
additional_files=""
if [[ -n "$ADDITIONAL_FILES" ]]; then
IFS=',' read -r -a additional_files <<< "$ADDITIONAL_FILES"
fi
# Copy whitelist files from template
for file in $WHITELIST_FILES; do
cp -f "template-repo/$file" "$file"
done
# Copy additional files from template (if any were specified)
for file in "${additional_files[@]}"; do
cp -f "template-repo/$file" "$file"
done
# Clean up
rm -rf template-repo/
git add .
git commit -m "chore: sync template"
git push "$original_remote" "$pr_branch"
gh pr create --title "Sync branch to template" --body "This pull request merges changes from the template repository, overwriting only files in the whitelist and specified files." --head "$pr_branch" --base "$branch_name"

0 comments on commit 10486f6

Please sign in to comment.