When building for a microservice based architecture, often developers will have a folder structure like this, with frontend and backend in the same repo:
some-microservice
│
└───/front-end
│ │ index.js
│ │ /src
│
└───/back-end
│ index.js
│ /src
We can avoid this and have our code in its own seperate repos, this allows you to create a “parent” repo where you can import submodules. This means you can use different repos in different services.
For example, developer John has just made a repo for authentication called “happy-login”.
I’ve just build a login UI and I need a login backend, so I can run the following command and import his repo:
git submodule add https://github.com/john/happy-login
This login UI I made of course is generic and reusable. Next, Sam is running an online store and she tells me she needs a login system to connect to allow customers to login and see her products she is selling. She’s already started work in her own repo called “sam-store” and doesn’t really want to overcrowd her repo with other people’s repos.
So instead, she makes a new repo called “store-microservice”
Then she runs the following:
git init
git submodule add https://github.com/john/happy-login
git submodule add https://github.com/clem/login-ui
git submodule add https://github.com/sam/sam-store
Here she is initializing the repo and importing the other people’s repo’s she needs to make her project work.
She can also pass in her own environment variables, as she may want to use a different database for storing user logins.
All these submodules have their own Dockerfile, so now all Sam has to do is write a docker-compose.yml file to link all the different repos together.
services:
login-ui:
build: ./login-ui
ports: 1234:1234
login-service:
build: ./happy-login
ports: 1111:1111
store:
build: ./sam-store
ports: 1111:1111
There is unfortunately one caveat, by design this folder will not update automatically so once you make changes to your service the main repo needs to be updated.
A quick hack is the following:
git submodule foreach git pull origin master