Syncing Specific Folders Between Different GitHub Accounts
Another Github Action to sync between different accounts.
To synchronize specific folders between repositories belonging to different GitHub accounts (Account A and Account B), you’ll need to set up Personal Access Tokens (PATs) for both accounts and store them as secrets in the respective repositories. Here’s how you can adjust the GitHub Actions workflows to work across two accounts:
Steps to Set Up Syncing Between Two GitHub Accounts
-
Generate Personal Access Tokens (PATs):
- Account A: Generate a PAT for Account A with the
repo
scope. - Account B: Generate a PAT for Account B with the
repo
scope.
- Account A: Generate a PAT for Account A with the
-
Store PATs as Secrets:
- In Repository A (Account A’s repository):
- Store Account A’s PAT as a secret named
PAT_A
. - Store Account B’s PAT as a secret named
PAT_B
.
- Store Account A’s PAT as a secret named
- In Repository B (Account B’s repository):
- Store Account B’s PAT as a secret named
PAT_B
. - Store Account A’s PAT as a secret named
PAT_A
.
- Store Account B’s PAT as a secret named
- In Repository A (Account A’s repository):
Adjusted GitHub Actions Workflows
Make sure you replace the branch name with the correct one for each repository. If your default branch is not main
but master
, please update the code with the right name.
Workflow for Repository A (Sync Folder-A1
to Folder-B1
)
In Repository A’s workflow (repo-a
):
# .github/workflows/sync_folder_a1_to_b1.yml
name: Sync Folder-A1 to Folder-B1
on:
push:
paths:
- 'Folder-A1/**'
branches:
- main
jobs:
sync:
runs-on: ubuntu-latest
steps:
- name: Checkout Repo A (repo-a)
uses: actions/checkout@v3
with:
token: ${{ secrets.PAT_A }}
fetch-depth: 0
- name: Install rsync
run: sudo apt-get install -y rsync
- name: Clone Repo B (repo-b)
run: |
git clone --branch main https://username-b:${{ secrets.PAT_B }}@github.com/username-b/repo-b.git
cd repo-b
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Compare and Sync Folder-A1 to Folder-B1
run: |
cd $GITHUB_WORKSPACE
if git rev-parse HEAD~1 >/dev/null 2>&1; then
git diff --name-only HEAD~1 -- Folder-A1/ > changed_files.txt
else
find Folder-A1 -type f > changed_files.txt
fi
cd repo-b
while IFS= read -r file; do
if [ -f "../$file" ]; then
rsync -av --delete "../$file" "Folder-B1/${file#Folder-A1/}"
else
rm -f "Folder-B1/${file#Folder-A1/}"
fi
done < ../changed_files.txt
git add Folder-B1
git commit -m "Sync changes from Folder-A1 to Folder-B1"
git push origin main
env:
PAT_B: ${{ secrets.PAT_B }}
Workflow for Repository B (Sync Folder-B1
to Folder-A1
)
In Repository B’s workflow (repo-b
):
# .github/workflows/sync_folder_b1_to_a1.yml
name: Sync Folder-B1 to Folder-A1
on:
push:
paths:
- 'Folder-B1/**'
branches:
- main
jobs:
sync:
runs-on: ubuntu-latest
steps:
- name: Checkout Repo B (repo-b)
uses: actions/checkout@v3
with:
token: ${{ secrets.PAT_B }}
fetch-depth: 0
- name: Install rsync
run: sudo apt-get install -y rsync
- name: Clone Repo A (repo-a)
run: |
git clone --branch main https://username-a:${{ secrets.PAT_A }}@github.com/username-a/repo-a.git
cd repo-a
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Compare and Sync Folder-B1 to Folder-A1
run: |
cd $GITHUB_WORKSPACE
if git rev-parse HEAD~1 >/dev/null 2>&1; then
git diff --name-only HEAD~1 -- Folder-B1/ > changed_files.txt
else
find Folder-B1 -type f > changed_files.txt
fi
cd repo-a
while IFS= read -r file; do
if [ -f "../$file" ]; then
rsync -av --delete "../$file" "Folder-A1/${file#Folder-B1/}"
else
rm -f "Folder-A1/${file#Folder-B1/}"
fi
done < ../changed_files.txt
git add Folder-A1
git commit -m "Sync changes from Folder-B1 to Folder-A1"
git push origin main
env:
PAT_A: ${{ secrets.PAT_A }}
Key Adjustments
-
Clone with Authentication: When cloning the other repository, include the username and token in the URL, e.g.,
https://username-b:${{ secrets.PAT_B }}@github.com/username-b/repo-b.git
. -
Separate PATs: Use the respective PATs from each account (
PAT_A
andPAT_B
) to authenticate actions in each workflow.
Conclusion
By modifying the GitHub Actions workflows to include authentication details for both GitHub accounts, you can effectively sync specific folders between repositories owned by different users. This setup ensures seamless and secure synchronization across multiple accounts.