종종 아래의 상황을 만나게 됩니다.
1. 설정할 Secret이 많은 경우
2. 두 개 이상의 프로젝트를 하나로 관리해야 하는 경우
3. Microservices에서 Dev Tool을 사용하고 싶은 경우
상황을 파악했으니
사용으로 간단히 해결됩니다.
프로젝트 구조
Main Project(tuplus) 아래 9개의 Sub Project가 있습니다.
위 구조를 만들어 봅시다.
# main project path
cd tuplus
# git submodule add <sub-module git url> <folder name>
git submodule add https://github.com/codelab-kr/tuplus-db-fixture-rest-api.git db-fixture-rest-api
git submodule add https://github.com/codelab-kr/tuplus-history.git history
git submodule add https://github.com/codelab-kr/tuplus-gateway.git gateway
...
.gitmoules
파일이 생겼습니다.
내용을 봅시다.
[submodule "db-fixture-rest-api"]
path = db-fixture-rest-api
url = https://github.com/codelab-kr/tuplus-db-fixture-rest-api.git
[submodule "history"]
path = history
url = https://github.com/codelab-kr/tuplus-history.git
[submodule "gateway"]
path = gateway
url = https://github.com/codelab-kr/tuplus-gateway.git
[submodule "metadata"]
path = metadata
url = https://github.com/codelab-kr/tuplus-metadata.git
...
main project에만 속한 경로에 파일을 추가해 보면 익숙한 로그가 나옵니다.
➜ git commit -m "Add README.md"
[feature 5532bb0] Add README.md
1 file changed, 94 insertions(+)
create mode 100644 README.MD
main과 sub project 모두에 속하는 경로에 파일을 추가하면 어떻게 될까요?
추가하자마자 뭔가 이상합니다.
분명 파일 1개만 추가했는데 git 소스제어 쪽에 변경사항이 2개로 표시됩니다.
➜ cd tuplus
➜ git add .
➜ git status
현재 브랜치 feature
브랜치가 'origin/feature'에 맞게 업데이트된 상태입니다.
커밋하도록 정하지 않은 변경 사항:
(무엇을 커밋할지 바꾸려면 "git add <파일>..."을 사용하십시오)
(use "git restore <file>..." to discard changes in working directory)
(하위 모듈의 추적되지 않는 파일이나 수정된 내용을 커밋하거나 버리십시오)
수정함: video-streaming (추적하지 않은 내용)
커밋할 변경 사항을 추가하지 않았습니다 ("git add" 및/또는 "git commit -a"를 사용하십시오)
# sub project path
➜ cd video-streaming
➜ git add . && git commit -m "add test file" && git push
# main project path
➜ cd ..
➜ git add . && git commit -m "add test file"
main과 sub project의 관계를 알아보기 위해 git repository로 가봅시다.
sub project들이 마치 디렉토리 링크처럼 보입니다.
video-streaming @ e9fb253
를 클릭하면 tuplus-video-streaming
로 이동합니다.
tuplus-video-streaming
의 마지막 commit hash(e9fb253)과 video-streaming @ e9fb253
가 일치합니다.
tuplus를 clone 하면 sub project는 빈 폴더만 있고 내용이 없습니다.
우리는 sub project의 전체 소스를 한 번에 가져오고 싶습니다.
다소 무식한 방법은 아래를 9번 반복하는 겁니다.
➜ rm -rf video-streaming
➜ git submodule add --force --name video-streaming https://github.com/codelab-kr/tuplus-video-streaming.git video-streaming
Reactivating local git directory for submodule 'video-streaming'
우리에게 시간은 돈입니다.
옵션을 넣어서 clone 합시다.
한 줄이면 됩니다.
git clone --recurse-submodules https://github.com/codelab-kr/tuplus.git
sub project를 각각 다른 팀원들이 관리한다면 우리는 sub project 마다 git pull 반복해야 합니다.
한번에 합시다.
git submodule update --remote
git submodule foreach npm install
security에는 각종 설정정보와 민감정보들이 포함되어 있습니다.
공개되면 곤란합니다.
repo를 만들 때 공개정도를 private로 설정하도록 합니다.
(다른 sub projects는 public 으로 두도록 합니다.)
이렇게 하면 권한이 없는 사람들은 tuplues에 security이라는 sub project가 있다는 것은 알 수 있지만 security를 clone 하거나 내용을 볼 수 없습니다.
https://velog.io/@codelab/Git-Submodules에-husky-적용
# copy the new URL to your local config
$ git submodule sync --recursive
# update the submodule from the new URL
$ git submodule update --init --recursive
https://git-scm.com/book/en/v2/Git-Tools-Submodules
https://git-scm.com/docs/git-submodule