Automate Git submodule updates between repositories with GitHub Actions. Real-world setup using:
You want changes in git-modules-repo to automatically sync into main-repo where it's used as a submodule.
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
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
git-modules-repo โ Settings โ Secrets โ Actions
MAIN_REPO_TOKEN
with the token as valuegit-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
Make a change in git-modules-repo
, push it, and watch GitHub Actions auto-update the submodule in main-repo
.
git push
with gh pr create
cron
in Actions to check for updates weekly
If this guide helped you, feel free to โญ๏ธ the repos and share it with your team.