A GitHub Action for syncing two repositories using force push.
- Sync branches between two GitHub repositories
- Sync branches from one remote repository to another
- GitHub action can be triggered on a timer or on push
- To push to a remote repository, please checkout git-sync
- Support syncing tags.
This action operates in two "modes", a local mode (using the current GitHub repository as a source of truth where the workflow is running) or remote mode to sync two repositories outside of the workflow repo.
Create a personal access token and add to repository's secret as PAT
# File: .github/workflows/repo-sync.yml
on:
schedule:
- cron: "*/15 * * * *"
workflow_dispatch:
jobs:
repo-sync:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
persist-credentials: false
- name: repo-sync
uses: repo-sync/github-sync@v2
with:
source_repo: ""
source_branch: ""
destination_branch: ""
github_token: ${{ secrets.PAT }}
If source_repo
is private or with another provider, either (1) use an authenticated HTTPS repo clone url like https://${access_token}@github.com/owner/repository.git
or (2) set a SSH_PRIVATE_KEY
secret environment variable and use the SSH clone url
Set the requisite secrets for syncing two remote repositories on the GitHub repository that will execute this action.
# File: .github/workflows/repo-sync.yml
on:
schedule:
- cron: "*/15 * * * *"
workflow_dispatch:
jobs:
repo-sync:
runs-on: ubuntu-latest
steps:
- env:
SOURCE_TOKEN: ${{ secrets.SOURCE_TOKEN }}
run: |
git clone https://$SOURCE_TOKEN@<remote-host>/user/foo-repo.git foo-repo/
- name: repo-sync
uses: repo-sync/github-sync@v2
working-directory: ./foo-repo
with:
source_repo: "user1/foo-repo"
source_host: github.com # If omitted, defaults to github.com
source_token: ${{ secrets.SOURCE_TOKEN }}
source_user: "user1"
source_branch: "main"
destination_repo: "user2/bar-repo"
destination_host: gitlab.com
destination_user: oauth2
destination_token: ${{ secrets.DESTINATION_TOKEN }}
destination_branch: "main"
If destination_branch
and the branch where you will create this workflow will be the same, The workflow (and all files) will be overwritten by source_branch
files. A potential solution is: Create a new branch with the actions file and make it the default branch.
- Make a backup
- Create a new branch in your repo (destination repo), it should not share the name with any branch in source repo
- Make the new branch the default branch under repo settings
- Use
*
for bothsource_branch
anddestination_branch
- Optionally, you can force sync all tags:
with: sync_tags: "true"
This will force sync ALL branches to match source repo. Branches that are created only in the destination repo will not be affected but all the other branches will be hard reset to match source repo.
Thanks goes to these wonderful people (emoji key):
Wei He 💻 📖 🎨 🤔 |
Zeke Sikelianos 📖 🤔 |
Alex Page 🐛 |
xtqqczze 💻 📖 |
Kay Harrison-Sims 🐛 |
Gibby 💻 |
This project follows the all-contributors specification. Contributions of any kind welcome!