|
1 | 1 | # Git Sync
|
2 | 2 |
|
3 |
| -A GitHub Action for syncing between two independent repositories using **force push**. |
4 |
| - |
| 3 | +A GitHub Action for syncing between two independent repositories using **force push**. |
5 | 4 |
|
6 | 5 | ## Features
|
7 |
| - * Sync branches between two GitHub repositories |
8 |
| - * Sync branches to/from a remote repository |
9 |
| - * GitHub action can be triggered on a timer or on push |
10 |
| - * To sync with current repository, please checkout [Github Repo Sync](https://github.com/marketplace/actions/github-repo-sync) |
11 | 6 |
|
| 7 | +- Sync branches between two GitHub repositories |
| 8 | +- Sync branches to/from a remote repository |
| 9 | +- GitHub action can be triggered on a timer or on push |
| 10 | +- To sync with current repository, please checkout [Github Repo Sync](https://github.com/marketplace/actions/github-repo-sync) |
12 | 11 |
|
13 | 12 | ## Usage
|
14 | 13 |
|
15 |
| -Always make a full backup of your repo (`git clone --mirror`) before using this action. |
| 14 | +> Always make a full backup of your repo (`git clone --mirror`) before using this action. |
16 | 15 |
|
17 |
| -### GitHub Actions |
| 16 | +- 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) |
| 17 | + |
| 18 | +```sh |
| 19 | +$ ssh-keygen -t rsa -b 4096 -C "[email protected]" |
18 | 20 | ```
|
19 |
| -# File: .github/workflows/repo-sync.yml |
| 21 | + |
| 22 | +- In GitHub, either: |
| 23 | + |
| 24 | + - 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 |
| 25 | + |
| 26 | + or |
| 27 | + |
| 28 | + - add the single public key (`key_name.pub`) to _Personal Settings > SSH keys_ |
| 29 | + |
| 30 | +- 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`) |
| 31 | + |
| 32 | +### GitHub Actions |
| 33 | + |
| 34 | +```yml |
| 35 | +# .github/workflows/repo-sync.yml |
20 | 36 |
|
21 | 37 | on: push
|
22 | 38 | jobs:
|
23 | 39 | repo-sync:
|
24 | 40 | runs-on: ubuntu-latest
|
25 | 41 | steps:
|
26 |
| - - name: repo-sync |
27 |
| - uses: wei/git-sync@v2 |
28 |
| - with: |
29 |
| - source_repo: "" |
30 |
| - source_branch: "" |
31 |
| - destination_repo: "" |
32 |
| - destination_branch: "" |
33 |
| - ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }} |
| 42 | + - name: repo-sync |
| 43 | + uses: wei/git-sync@v3 |
| 44 | + with: |
| 45 | + source_repo: "username/repository" |
| 46 | + source_branch: "main" |
| 47 | + destination_repo: "[email protected]:org/repository.git" |
| 48 | + destination_branch: "main" |
| 49 | + ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }} # optional |
| 50 | + source_ssh_private_key: ${{ secrets.SOURCE_SSH_PRIVATE_KEY }} # optional, will override `SSH_PRIVATE_KEY` |
| 51 | + destination_ssh_private_key: ${{ secrets.DESTINATION_SSH_PRIVATE_KEY }} # optional, will override `SSH_PRIVATE_KEY` |
| 52 | +``` |
| 53 | +
|
| 54 | +##### Alternative using https |
| 55 | +
|
| 56 | +The `ssh_private_key`, `source_ssh_private_key` and `destination_ssh_private_key` can be omitted if using authenticated https urls. |
| 57 | + |
| 58 | +```yml |
| 59 | +source_repo: "https://username:[email protected]/username/repository.git" |
34 | 60 | ```
|
35 |
| -`ssh_private_key` can be omitted if using authenticated HTTPS repo clone urls like `https://username:[email protected]/username/repository.git`. |
36 | 61 |
|
37 | 62 | #### Advanced: Sync all branches
|
38 | 63 |
|
39 | 64 | 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.
|
40 | 65 |
|
| 66 | +```yml |
| 67 | +source_branch: "refs/remotes/source/*" |
| 68 | +destination_branch: "refs/heads/*" |
| 69 | +``` |
| 70 | + |
41 | 71 | #### Advanced: Sync all tags
|
42 | 72 |
|
43 | 73 | To Sync all tags from source to destination, use `source_branch: "refs/tags/*"` and `destination_branch: "refs/tags/*"`. But be careful, tags with the same name will be overwritten.
|
44 | 74 |
|
45 |
| -### Docker |
| 75 | +```yml |
| 76 | +source_branch: "refs/tags/*" |
| 77 | +destination_branch: "refs/tags/*" |
46 | 78 | ```
|
47 |
| -docker run --rm -e "SSH_PRIVATE_KEY=$(cat ~/.ssh/id_rsa)" $(docker build -q .) \ |
| 79 | + |
| 80 | +### Docker |
| 81 | + |
| 82 | +```sh |
| 83 | +$ docker run --rm -e "SSH_PRIVATE_KEY=$(cat ~/.ssh/id_rsa)" $(docker build -q .) \ |
48 | 84 | $SOURCE_REPO $SOURCE_BRANCH $DESTINATION_REPO $DESTINATION_BRANCH
|
49 | 85 | ```
|
50 | 86 |
|
51 | 87 | ## Author
|
52 |
| -[Wei He ](https://github.com/wei) _[email protected]_ |
53 | 88 |
|
| 89 | +[Wei He](https://github.com/wei) [email protected]_ |
54 | 90 |
|
55 | 91 | ## License
|
| 92 | + |
56 | 93 | [MIT](https://wei.mit-license.org)
|
0 commit comments