Sync Child Repo Changes #8
This file contains hidden or 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: Sync Child Repo Changes | |
| on: | |
| workflow_dispatch: {} # Manual trigger | |
| # Triggered by child repo when main branch is updated | |
| jobs: | |
| sync-child-repo: | |
| runs-on: ubuntu-latest | |
| concurrency: | |
| group: sync-child-repo | |
| cancel-in-progress: false | |
| env: | |
| PARENT_REPO: ${{ github.repository }} | |
| PARENT_BRANCH: main # Base branch for new sync branch | |
| CHILD_REPO: ${{ vars.CHILD_REPO || 'devmaster-x/ex_child' }} # Child repo reference | |
| CHILD_BRANCH: main # Child repo main branch | |
| CHILD_MODULE_PATH: ./app # Root of child repo | |
| PARENT_MODULE_PATH: ./app # Target path in parent repo | |
| TIMESTAMP: ${{ format('YYYY-MM-DD-HH-mm-ss', 'now') }} | |
| steps: | |
| - name: Checkout parent repo (main branch) | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: ${{ env.PARENT_BRANCH }} | |
| fetch-depth: 0 | |
| token: ${{ secrets.CHILD_REPO_TOKEN }} # Use PAT for authentication | |
| - name: Configure Git Authentication | |
| run: | | |
| git config --global user.name "github-actions[bot]" | |
| git config --global user.email "github-actions[bot]@users.noreply.github.com" | |
| # Configure git to use the PAT for authentication | |
| git remote set-url origin https://x-access-token:${{ secrets.CHILD_REPO_TOKEN }}@github.com/${{ github.repository }}.git | |
| echo "Updated git remote URL with PAT authentication" | |
| - name: Update main branch | |
| run: | | |
| # Ensure we have the latest changes from main branch | |
| git fetch origin | |
| git reset --hard origin/${{ env.PARENT_BRANCH }} | |
| echo "Updated to latest ${{ env.PARENT_BRANCH }} branch" | |
| - name: Create sync branch | |
| run: | | |
| # Create a more unique branch name with timestamp and run ID | |
| BRANCH_NAME="sync-child-repo-${{ github.run_id }}-${{ env.TIMESTAMP }}" | |
| echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV | |
| # Check if branch already exists locally and delete it | |
| if git branch --list "$BRANCH_NAME" | grep -q "$BRANCH_NAME"; then | |
| echo "Local branch $BRANCH_NAME already exists, deleting it..." | |
| git branch -D "$BRANCH_NAME" | |
| fi | |
| # Create the new branch | |
| git checkout -b "$BRANCH_NAME" | |
| echo "Created branch: $BRANCH_NAME" | |
| - name: Checkout child repo | |
| uses: actions/checkout@v4 | |
| with: | |
| repository: ${{ env.CHILD_REPO }} | |
| ref: ${{ env.CHILD_BRANCH }} | |
| path: child-repo | |
| token: ${{ secrets.CHILD_REPO_TOKEN }} # PAT with access to child repo | |
| fetch-depth: 0 | |
| - name: Sync child repo -> parent repo | |
| run: | | |
| # Create target directory | |
| mkdir -p "${{ env.PARENT_MODULE_PATH }}" | |
| # Remove existing content | |
| rm -rf "${{ env.PARENT_MODULE_PATH }}"/* | |
| # Copy all files from child repo | |
| cp -r child-repo/* "${{ env.PARENT_MODULE_PATH }}/" | |
| # Remove .git directory if it exists | |
| rm -rf "${{ env.PARENT_MODULE_PATH }}/.git" | |
| # Remove any other unnecessary files | |
| rm -rf "${{ env.PARENT_MODULE_PATH }}/.github" | |
| rm -rf "${{ env.PARENT_MODULE_PATH }}/.gitignore" | |
| rm -rf "${{ env.PARENT_MODULE_PATH }}/.gitattributes" | |
| echo "Synced child repo content to ${{ env.PARENT_MODULE_PATH }}" | |
| - name: Check for changes | |
| id: check-changes | |
| run: | | |
| if [[ -n "$(git status --porcelain)" ]]; then | |
| echo "has_changes=true" >> $GITHUB_OUTPUT | |
| echo "Changes detected, will create PR" | |
| else | |
| echo "has_changes=false" >> $GITHUB_OUTPUT | |
| echo "No changes detected" | |
| fi | |
| - name: Commit changes | |
| if: steps.check-changes.outputs.has_changes == 'true' | |
| run: | | |
| git add "${{ env.PARENT_MODULE_PATH }}" | |
| git commit -m "Sync child repo from ${{ env.CHILD_REPO }} @ ${{ github.sha }} | |
| - Source: ${{ env.CHILD_REPO }}@${{ env.CHILD_BRANCH }} | |
| - Target: ${{ env.PARENT_MODULE_PATH }} | |
| - Timestamp: ${{ env.TIMESTAMP }} | |
| - Triggered by: ${{ github.event_name }}" | |
| - name: Clean up existing remote branch | |
| if: steps.check-changes.outputs.has_changes == 'true' | |
| run: | | |
| # Check if remote branch exists and delete it if it does | |
| if git ls-remote --heads origin "${{ env.BRANCH_NAME }}" | grep -q "${{ env.BRANCH_NAME }}"; then | |
| echo "Remote branch ${{ env.BRANCH_NAME }} already exists, deleting it first..." | |
| git push origin --delete "${{ env.BRANCH_NAME }}" || true | |
| echo "Remote branch deleted successfully" | |
| else | |
| echo "No existing remote branch found" | |
| fi | |
| - name: Push sync branch | |
| if: steps.check-changes.outputs.has_changes == 'true' | |
| run: | | |
| # Push the new branch | |
| git push origin "${{ env.BRANCH_NAME }}" | |
| echo "Pushed branch: ${{ env.BRANCH_NAME }}" | |
| - name: Create Pull Request | |
| if: steps.check-changes.outputs.has_changes == 'true' | |
| uses: peter-evans/create-pull-request@v6 | |
| with: | |
| token: ${{ secrets.CHILD_REPO_TOKEN }} | |
| commit-message: "Sync child repo from ${{ env.CHILD_REPO }} @ ${{ github.sha }}" | |
| branch: "${{ env.BRANCH_NAME }}" | |
| base: ${{ env.PARENT_BRANCH }} | |
| title: "Sync child repo from ${{ env.CHILD_REPO }} - ${{ env.TIMESTAMP }}" | |
| body: | | |
| ## Automated Sync of Child Repository | |
| This PR syncs the latest changes from the **child repository** to the **parent repository**. | |
| ### Details | |
| - **Source**: `${{ env.CHILD_REPO }}@${{ env.CHILD_BRANCH }}` | |
| - **Target**: `${{ env.PARENT_MODULE_PATH }}` | |
| - **Sync Time**: `${{ env.TIMESTAMP }}` | |
| - **Trigger**: `${{ github.event_name }}` | |
| ### What Changed | |
| - Updated `${{ env.PARENT_MODULE_PATH }}/` directory with latest child repo content | |
| - All files from child repo have been synchronized | |
| ### Review Notes | |
| - This is an automated sync - please review the changes | |
| - The sync preserves the structure and content from the source repository | |
| - Any conflicts should be resolved manually if they occur | |
| --- | |
| *This PR was automatically generated by GitHub Actions* | |
| labels: | | |
| automated | |
| sync | |
| child-repo | |
| assignees: | | |
| ${{ github.repository_owner }} | |
| # reviewers: | | |
| # ${{ github.repository_owner }} | |
| - name: Cleanup on no changes | |
| if: steps.check-changes.outputs.has_changes == 'false' | |
| run: | | |
| echo "No changes to sync. Skipping PR creation." | |
| # Delete the branch if no changes | |
| git push origin --delete "${{ env.BRANCH_NAME }}" || true |