GitHub Action to sync repositories (push to another repo when push detected on current repo)
- Create Personal Access Token (PAT) with
public_repo
scope (if your target repo is a private repo, you need a private scope userepo
instead) - Save PAT as secret named
RS_PAT
by going toGitHub -> Repo -> Settings -> Secrets -> New reposirory secret
- Create a secret named
RS_TARGET
with your target repo name inorg/repo
format e.g.Anthonykung/RepoSync
- Create a new GitHub action Workflow by
GitHub -> Repo -> Actions -> New workflow -> set up a workflow yourself
- Copy and paste the following to the text area
- Rename the file to
RepoSync.yml
- Hit save and be done!
name: AnthRepoSync
on:
push:
# Remove the `#` below if you only want a specific branch, leave it for all pushes
# branches:
# - main
workflow_dispatch:
jobs:
RepoSync:
runs-on: ubuntu-latest
steps:
- name: AnthRepoSync
uses: Anthonykung/[email protected]
with:
RS_TARGET: ${{ secrets.RS_TARGET }}
RS_PAT: ${{ secrets.RS_PAT }}
- Create Personal Access Token (PAT) with
public_repo
scope (if your target repo is a private repo, you need a private scope userepo
instead) - Save PAT as secret named
RS_PAT
by going toGitHub -> Repo -> Settings -> Secrets -> New reposirory secret
- If you use a branch that is not
main
, you will need to updateRepoSync.yml
with your branch name e.g.master
(sorry they don't let me use secrets 😭) - Create a secret named
RS_TARGET
with your target repo name inorg/repo
format e.g.Anthonykung/RepoSync
- Sit back and relex, check if the action successfully completes, otherwise you can grab that cup of coffee (or tea) now 😉.
Of course, why is something boring like this useful? Well, imagine you have a GitHub profile say Anthonykung/Anthonykung
but you also publish it on a GitHub.io page for no reason at Anthonykung/Anthonykung.github.io
. And you are too lazy to pull the repo and add a new upstream, then this action is very useful in that case. All the pushes to Anthonykung/Anthonykung
will by sync automatically to Anthonykung/Anthonykung.github.io
and you don't have to do a thing.
Sure, you can create a GitHub page directly in the directory and skip this (pretty sure I'll eventually do that 😶) but say if you have a project that requires you to sync two or more repo and since the repos would basically be the same so there is no point to use a submodule and you work with random people that may or may not remember to add a upstream then this should help. Whenever people push, no matter they remember to add upstream or not, it will sync your other repo.
You can add multiple target repo if you would like just add more of theses:
- name: Get target repo
uses: actions/checkout@v2
with:
repository: ${{ secrets.RS_TARGET }}
token: ${{ secrets.RS_PAT }}
path: target
Under the line # You can add more targets if you want
like so:
- name: Get target repo
uses: actions/checkout@v2
with:
repository: ${{ secrets.RS_TARGET }} # Create a secret with your GitHub repo in org/repo format e.g. Anthonykung/RepoSync
token: ${{ secrets.RS_PAT }} # Create a PAT with public repo access and put it as a secret named PUBLIC_GITHUB_REPO
path: target # (GitHub -> Repo -> Settings -> Secrets -> New repository secret)
# You can add more targets if you want
- name: Get target repo
uses: actions/checkout@v2
with:
repository: ${{ secrets.RS_TARGET }}
token: ${{ secrets.RS_PAT }}
path: target2
- name: Get target repo
uses: actions/checkout@v2
with:
repository: ${{ secrets.RS_TARGET }}
token: ${{ secrets.RS_PAT }}
path: target3
And don't forget to update the script to include the other targets like this:
- name: Copy source to target and push
run: |
git config user.name github-actions
git config user.email [email protected]
rsync -r --exclude '.git' $GITHUB_WORKSPACE/source/* $GITHUB_WORKSPACE/target
rsync -r --exclude '.git' $GITHUB_WORKSPACE/source/* $GITHUB_WORKSPACE/target2
rsync -r --exclude '.git' $GITHUB_WORKSPACE/source/* $GITHUB_WORKSPACE/target3
cd $GITHUB_WORKSPACE/target && git add -A && git commit -am "Generated update from RepoSync GitHub Action" && git push
cd $GITHUB_WORKSPACE/target2 && git add -A && git commit -am "Generated update from RepoSync GitHub Action" && git push
cd $GITHUB_WORKSPACE/target3 && git add -A && git commit -am "Generated update from RepoSync GitHub Action" && git push
Of course, you can use your imagination and have something else that does the same thing, I'll leave that up to you 😉.