Home

Keep two repositories in sync

Gunar Gessner

Gunar Gessner

Aug 12, 2021

This is a GitHub Action template for force-pushing one repo into another once every day.

  1. Create a new repo called repo-sync
  2. Create a Personal Access Token for GitHub
  3. Add the token as a secret called PERSONAL_ACCESS_TOKEN
  4. Put the code below into .github/workflows/sync.yml and replace the <variables>
  5. Profit (i.e. you're done!)
name: "Daily sync"

on:
  push:
    branches:
      - main
  schedule:
    -   cron: "0 0 * * *"

jobs:
  backup:
    runs-on: ubuntu-latest
    name: Sync repos
    steps:
    - name: sync
      env:
        PERSONAL_ACCESS_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
        USERNAME: <your-username>
        REPO_FROM: <org/repo>
        REPO_TO: <org/repo>
      run: |
        set -xe
        git clone "https://${USERNAME}:${PERSONAL_ACCESS_TOKEN}@github.com/${REPO_FROM}.git" .
        git remote add fork "https://{$USERNAME}:${PERSONAL_ACCESS_TOKEN}@github.com/${REPO_TO}.git" 
        git fetch fork
        git push --force fork origin/main:main

Sign up for the newsletter


Read other stuff