Skip to content

Instantly share code, notes, and snippets.

@igorcosta
Last active September 21, 2023 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorcosta/e4a4a4061094510ae78d820a3c169651 to your computer and use it in GitHub Desktop.
Save igorcosta/e4a4a4061094510ae78d820a3c169651 to your computer and use it in GitHub Desktop.
How to maintain two repositories internal/private and one open-source

πŸ“– Managing dual repositories on GitHub

Managing multiple repositories that serve distinct but interconnected purposes can be a challenging task πŸ˜“. This scenario is common when an organization wishes to contribute to an open-source project 🌐 while also maintaining an internal space for private collaboration πŸ”’.

The challenge here lies in keeping these repositories in sync πŸ”„ without manual intervention, ensuring that changes made to one repository πŸ“ are accurately reflected in the other πŸ“.

In this step-by-step tutorial πŸ“ƒ, we'll walk through a use case where a customer wants to contribute to an open-source project πŸ’» and, at the same time, collaborate internally within their organization 🏒.

We will:

  1. Changing the ownership of the existing repository. Follow this steps
  2. 🍴 Create a fork of the open-source repository, providing the customer ownership and full control over this copy.
  3. πŸ—οΈ Set up an internal repository for private collaboration within the customer's organization.
  4. πŸ€– Implement GitHub Actions to automatically keep these repositories in sync.

πŸ› οΈ Step by step guide

1️⃣ Step 2: Fork the Open-Source Repository

  1. πŸ’» Navigate to the open-source repository on GitHub.
  2. ↗️ Click on the "Fork" button at the top-right corner to create a fork of the repository. This will be your customer's copy, where they can have owner-level permissions.

2️⃣ Step 3: Create an Internal Repository

  1. πŸ“— Create a new, empty repository in GitHub under your customer's organization account.
  2. ⬇️ Clone the forked repository to your local machine.
    git clone git@github.com:YourAccount/ForkedRepo.git
  3. πŸ”— Add the internal repository as a remote.
    git remote add internal git@github.com:CustomerOrg/InternalRepo.git
  4. πŸš€ Push all branches and tags to the internal repository.
    git push --all internal
    git push --tags internal

3️⃣ Step 4: Configure Syncing Mechanism

  1. :octocat: Create a GitHub Action in the internal repository to automatically push changes to the forked repository.
    # .github/workflows/sync.yml
    name: Sync to Fork
    on:
      push:
        branches:
          - main
    jobs:
      sync:
        runs-on: ubuntu-latest
        steps:
        - uses: actions/checkout@v2
        - run: |
            git remote add fork git@github.com:YourAccount/ForkedRepo.git
            git push fork main
  2. πŸ” Similarly, create a GitHub Action in the forked repository to automatically pull changes from the original open-source repository.
    # .github/workflows/pull.yml
    name: Pull from Original
    on:
      schedule:
        - cron: '0 0 * * *'
    jobs:
      pull:
        runs-on: ubuntu-latest
        steps:
        - uses: actions/checkout@v2
        - run: |
            git remote add upstream git@github.com:OriginalAccount/OriginalRepo.git
            git pull upstream main
            git push origin main

4️⃣ Step 5: Collaborate and Monitor

  1. πŸ‘₯ Your customer can now collaborate internally on their internal repository.
  2. πŸ”„ Any push to the main branch will automatically get pushed to their forked open-source repository via GitHub Actions.
  3. πŸ• Periodically, the forked repository will pull any new changes from the original open-source repository.

5️⃣ Step 6: Safeguard

  1. πŸ›‘οΈ Ensure you've set up proper permissions and protection Rulesets on both the internal and forked repositories.
  2. πŸ” Review the GitHub Action logs periodically to ensure syncing is working as expected.

By following this guide πŸ“œ, you'll set up an efficient workflow βš™οΈ for managing dual repositories, making it easier for your customer to contribute to the open-source community ❀️ while keeping internal developments private πŸ”.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment