Skip to content

Sync Configurable Files from Parent Repository #21

Sync Configurable Files from Parent Repository

Sync Configurable Files from Parent Repository #21

name: Sync Configurable Files from Parent Repository
on:
schedule:
# Schedule will be dynamically loaded from sync-config.yml if available
- cron: '0 9 * * 1' # Default value, overridden by config if present
workflow_dispatch:
inputs:
parent_repo:
description: 'Parent repository (owner/repo)'
required: false
default: ''
parent_branch:
description: 'Parent repository branch'
required: false
default: ''
files_to_sync:
description: 'Comma-separated list of files to sync (leave empty for config file)'
required: false
default: ''
use_config_file:
description: 'Use .github/sync-config.yml for configuration'
required: false
default: true
type: boolean
env:
CONFIG_FILE: '.github/sync-config.yml'
jobs:
sync-files:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
actions: write
steps:
- name: Checkout current repository
uses: actions/checkout@v4
with:
token: ${{ secrets.SYNC_PAT || secrets.GITHUB_TOKEN }}
fetch-depth: 0
- name: Setup Git and Sync Files with PowerShell
id: sync
shell: pwsh
env:
GITHUB_TOKEN: ${{ secrets.SYNC_PAT || secrets.GITHUB_TOKEN }}
HAS_SYNC_PAT: ${{ secrets.SYNC_PAT != '' }}
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
./.github/scripts/sync-configurable-files.ps1
- name: Check for existing PRs and create individual PRs
if: steps.sync.outputs.changes_made == 'true'
shell: pwsh
env:
GITHUB_TOKEN: ${{ secrets.SYNC_PAT || secrets.GITHUB_TOKEN }}
run: |
# Get list of changed files from environment variable
$changedFiles = $env:CHANGED_FILES -split ' '
Write-Host "Processing $($changedFiles.Count) changed files..."
foreach ($file in $changedFiles) {
if ([string]::IsNullOrWhiteSpace($file)) { continue }
# Create a branch name based on the file path
$branchName = "sync-file/$($file -replace '[/\\]', '-' -replace '\.', '-')"
Write-Host "`nProcessing file: $file"
Write-Host "Branch name: $branchName"
# Check if a PR already exists for this file
$existingPRs = gh pr list --json title,headRefName,state --jq ".[] | select(.headRefName == `"$branchName`" and .state == `"OPEN`")" 2>$null
if ($existingPRs) {
Write-Host "PR already exists for $file, skipping..."
continue
}
# Create a new branch for this file
git checkout -b $branchName
# Stage only this specific file
git reset --hard HEAD
$targetFile = Join-Path -Path ${{ github.workspace }} -ChildPath $file
$tempFile = Join-Path -Path $env:TEMP -ChildPath "parent-repo" -ChildPath $file
Copy-Item -Path $tempFile -Destination $targetFile -Force
git add $targetFile
# Commit the file
git commit -m "Sync $file from parent repository" -m "Source: ${{ steps.sync.outputs.parent_repo }}@${{ steps.sync.outputs.parent_branch }}" -m "🤖 Generated with GitHub Actions"
# Push the branch
git push origin $branchName --force
# Create PR using gh CLI
$prTitle = "Sync $file from parent repository"
$prBody = @"
## Sync Configurable File
This PR updates a configurable file from the parent repository to maintain consistency.
**File:** ``$file``
**Source Repository:** ``${{ steps.sync.outputs.parent_repo }}``
**Source Branch:** ``${{ steps.sync.outputs.parent_branch }}``
---
🤖 This PR was created automatically by the sync-configurable-files workflow.
"@
gh pr create --title "$prTitle" --body "$prBody" --base ${{ github.ref_name }} --head $branchName
# Return to main branch for next file
git checkout ${{ github.ref_name }}
}
- name: Output summary (already handled in PowerShell script)
shell: pwsh
run: |
Write-Host "Summary has been written by the PowerShell script."