name: Merge and Push to Other Repository on: pull_request: branches: - main types: - closed permissions: contents: write actions: read jobs: export_pr: if: github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'main' && github.event.pull_request.head.ref == 'sync_branch' runs-on: ubuntu-latest steps: # Step 1: Checkout the current repository - name: Checkout current repository uses: actions/checkout@v3 with: fetch-depth: 0 # Fetch all history for all branches - name: Get Repository Name id: get_repo_name run: | REPO_NAME=${GITHUB_REPOSITORY#*/} # Step 2: Clone the target repository - name: Remove existing directory run: | rm -rf ${{ env.TARGET_DIR }} # Ensure the directory is clean before cloning - name: Clone target repository run: | git clone https://$GITHUB_ACTOR:${{ secrets.TARGET_REPO_PAT }}@github.com/${{ vars.TARGET_REPO }}.git cd ${{ vars.TARGET_DIR }} git checkout main # Ensure we are on the main branch # Step 3: Set Git user info - name: Set Git user info run: | cd ${{ vars.TARGET_DIR }} git config user.name "GitHub Actions" # Set a name for the user git config user.email "actions@github.com" # Set an email for the user # Step 4: Fetch changes from the current repository - name: Fetch current repository changes run: | cd ${{ vars.TARGET_DIR }} # Add current repo as a remote git remote add current-repo ../ # Give it a descriptive name git fetch current-repo # Fetch from the current repo # Get the current branch name CURRENT_BRANCH=$(echo $GITHUB_REF | awk -F'/' '{print $3}') # Get the current branch name # Checkout the current branch from the current repository git checkout -b sync_branch current-repo/$CURRENT_BRANCH # Create a new branch based on the current repo's branch # Step 5: Merge the target repository's main into the temporary branch - name: Merge target repository's main into temp branch run: | cd ${{ vars.TARGET_DIR }} git merge -s ours origin/main --allow-unrelated-histories # Allow unrelated histories if necessary # Step 6: Push the merged changes to the target repository - name: Push merged changes to target repository run: | cd ${{ vars.TARGET_DIR }} git push origin sync_branch --force # Push the temporary branch to the target repo # Step 7: Create a PR from the temporary branch to the target repo's main - name: Create PR from temporary branch to target repo's main run: | curl -X POST -H "Authorization: token ${{ secrets.TARGET_REPO_PAT }}" \ -H "Accept: application/vnd.github.v3+json" \ https://api.github.com/repos/${{ vars.TARGET_REPO }}/pulls \ -d "{\"title\":\"Sync $REPO_NAME (public) into main\",\"head\":\"sync_branch\",\"base\":\"main\",\"body\":\"Automated PR from GitHub Actions\"}"