๐Ÿ” Auto-Sync Git Submodules with GitHub Actions

Automate Git submodule updates between repositories with GitHub Actions. Real-world setup using:

โš™๏ธ Real Scenario

You want changes in git-modules-repo to automatically sync into main-repo where it's used as a submodule.

๐Ÿš€ Step-by-Step Setup

๐Ÿ“ 1. Create the Submodule Repo

git clone https://github.com/learning-new-things-daily/git-modules-repo.git
cd git-modules-repo
echo "echo Hello from submodule!" > module-script.sh
chmod +x module-script.sh
git add .
git commit -m "Initial commit"
git push

๐Ÿ“ 2. Create the Main Repo and Add Submodule

git clone https://github.com/learning-new-things-daily/main-repo.git
cd main-repo
git submodule add https://github.com/learning-new-things-daily/git-modules-repo.git modules
git commit -m "Added submodule"
git push

๐Ÿ” 3. Create GitHub Secret (PAT)

  1. Generate a Personal Access Token from GitHub Developer Settings
  2. Go to git-modules-repo โ†’ Settings โ†’ Secrets โ†’ Actions
  3. Add a secret named MAIN_REPO_TOKEN with the token as value

๐Ÿ“œ 4. Add GitHub Workflow in git-modules-repo

Create .github/workflows/update-main-repo.yml:

name: Update Submodule in Main Repo

on:
  push:
    branches:
      - main

jobs:
  update-submodule:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout This Repo
        uses: actions/checkout@v3

      - name: Configure Git Identity
        run: |
          git config --global user.name "GitHub Actions"
          git config --global user.email "actions@github.com"

      - name: Clone Main Repo & Update Submodule
        run: |
          git clone https://x-access-token:${{ secrets.MAIN_REPO_TOKEN }}@github.com/learning-new-things-daily/main-repo.git
          cd main-repo

          git submodule update --init --remote modules
          git add modules
          git commit -m "Auto-update submodule to latest commit" || echo "No changes to commit"
          git push origin main

๐Ÿงช 5. Test

Make a change in git-modules-repo, push it, and watch GitHub Actions auto-update the submodule in main-repo.

โœ… Benefits

๐Ÿ“ฆ Repositories

๐Ÿ“Œ Pro Tips

![Submodule Sync](https://github.com/learning-new-things-daily/git-modules-repo/actions/workflows/update-main-repo.yml/badge.svg)

๐Ÿ“ฃ Share It!

If this guide helped you, feel free to โญ๏ธ the repos and share it with your team.