Spaces:
Sleeping
Sleeping
| name: Merge and Push to Other Repository | |
| on: | |
| push: | |
| branches: | |
| - main # Trigger the workflow when changes are pushed to the main branch | |
| permissions: | |
| contents: write | |
| actions: read | |
| jobs: | |
| export_pr: | |
| 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: Check for specific labels in commit messages | |
| - name: Check for specific labels | |
| id: label_check | |
| run: | | |
| # Fetch the last commit message | |
| COMMIT_MESSAGE=$(git log -1 --pretty=%B) | |
| # Define labels you want to check for | |
| LABELS=("release-candidate") | |
| # Initialize a flag | |
| MATCHING_LABEL=false | |
| # Check if any label is in the commit message | |
| for LABEL in "${LABELS[@]}"; do | |
| if [[ "$COMMIT_MESSAGE" == *"$LABEL"* ]]; then | |
| MATCHING_LABEL=true | |
| break | |
| fi | |
| done | |
| # Set the output based on the label check | |
| echo "matching_label=${MATCHING_LABEL}" >> $GITHUB_ENV | |
| # Step 3: Clone the target repository | |
| - name: Remove existing directory | |
| if: env.matching_label == 'true' | |
| run: | | |
| rm -rf ${{ vars.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 4: 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 5: Fetch changes from the current repository | |
| - name: Fetch current repository changes | |
| run: | | |
| cd ${{ vars.TARGET_DIR }} | |
| 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 6: 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 7: 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 8: 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\":\"Merges $REPO_NAME (private) into main\",\"head\":\"sync_branch\",\"base\":\"main\",\"body\":\"Automated PR from GitHub Actions\"}" | |