A Git Action for syncing repositories
  • Shell 94.6%
  • Dockerfile 5.4%
Find a file
2024-08-05 09:28:49 +08:00
action.yml initial commit 2024-08-05 09:28:49 +08:00
Dockerfile initial commit 2024-08-05 09:28:49 +08:00
entrypoint.sh initial commit 2024-08-05 09:28:49 +08:00
git-sync.sh initial commit 2024-08-05 09:28:49 +08:00
LICENSE initial commit 2024-08-05 09:28:49 +08:00
README.md initial commit 2024-08-05 09:28:49 +08:00

Git Sync

A GitHub Action for syncing between two independent repositories using force push.

Features

  • Sync branches between two GitHub repositories
  • Sync branches to/from a remote repository
  • GitHub action can be triggered on a timer or on push
  • Support syncing tags
  • To sync with current repository, please checkout GitHub Sync

Usage

Caution

Always make a full backup of your repo (git clone --mirror) before using this action.

GitHub Actions

# .github/workflows/git-sync.yml

on: push
jobs:
  git-sync:
    runs-on: ubuntu-latest
    steps:
      - name: git-sync
        uses: astounds/git-sync@v1
        with:
          source_repo: "source_org/repository"
          source_branch: "main"
          destination_repo: "destination_org/repository"
          destination_branch: "main"
          ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }} # optional
          source_ssh_private_key: ${{ secrets.SOURCE_SSH_PRIVATE_KEY }} # optional, will override `SSH_PRIVATE_KEY`
          destination_ssh_private_key: ${{ secrets.DESTINATION_SSH_PRIVATE_KEY }} # optional, will override `SSH_PRIVATE_KEY`
Using shorthand

You can use GitHub repo shorthand like username/repository.

Using ssh

Note

The ssh_private_key, or source_ssh_private_key and destination_ssh_private_key must be supplied if using ssh clone urls.

source_repo: "git@github.com:username/repository.git"

or

source_repo: "git@gitlab.com:username/repository.git"
Using https

Note

The ssh_private_key, source_ssh_private_key and destination_ssh_private_key can be omitted if using authenticated https urls.

source_repo: "https://username:personal_access_token@github.com/username/repository.git"

Set up deploy keys

Important

You only need to set up deploy keys if repository is private and ssh clone url is used.

  • Either generate different ssh keys for both source and destination repositories or use the same one for both, leave passphrase empty (note that GitHub deploy keys must be unique for each repository)
$ ssh-keygen -o -a 100 -t ed25519 -f ~/.ssh/sync -C "john@example.com"
  • In GitHub, either:

    • add the unique public keys (key_name.pub) to Repo Settings > Deploy keys for each repository respectively and allow write access for the destination repository

    or

    • add the single public key (key_name.pub) to Personal Settings > SSH keys
  • Add the private key(s) to Repo > Settings > Secrets for the repository containing the action (SSH_PRIVATE_KEY, or SOURCE_SSH_PRIVATE_KEY and DESTINATION_SSH_PRIVATE_KEY)

Advanced: Sync all branches

To Sync all branches from source to destination, use source_branch: "refs/remotes/source/*" and destination_branch: "refs/heads/*". But be careful, branches with the same name including master will be overwritten.

source_branch: "refs/remotes/source/*"
destination_branch: "refs/heads/*"

Docker

$ docker run --rm -e "SSH_PRIVATE_KEY=$(cat ~/.ssh/id_ed25519)" $(docker build -q .) \
  $SOURCE_REPO $SOURCE_BRANCH $DESTINATION_REPO $DESTINATION_BRANCH

Author

Bot

License

GNU GPLv3+