Git / Sub Modules

Flexyz·2023년 9월 29일
0

Git

목록 보기
1/3
post-thumbnail

Projects를 Public Repository로 관리할 때

종종 아래의 상황을 만나게 됩니다.

1. 설정할 Secret이 많은 경우

  • Git에도 Secret이 있지만 하나하나 등록해야 합니다.
  • Vault 등 암호를 다루는 별도의 서비스도 있습니다만, 설정을 따로해야 합니다.
  • Private Repository로 변경하면 한 큐에 끝나지만 프로젝트를 공개하고 싶습니다.

2. 두 개 이상의 프로젝트를 하나로 관리해야 하는 경우

  • 각각 프로젝트를 관리하고 싶지만 하나의 상위 프로젝트로 묶고도 싶습니다.

3. Microservices에서 Dev Tool을 사용하고 싶은 경우

  • Microservices 는 각각의 서비스들을 가볍게 유지하는 것이 좋은데요
  • TypeScript, ES-lint, Husky 등 개발 시 필요한 Dev Tool 들을 사용하여 코드스타일을 유지하거나 문법 오류를 방지하고 싶습니다.
  • 모든 프로젝트에 각각 Dev Tool을 포함시키면 배보다 배꼽이 커지고 코드 중복이 발생합니다.


상황을 파악했으니

이제 문제를 해결해 봅시다


두괄식으로!


Git Submoules

사용으로 간단히 해결됩니다.

기존 프로젝트에 Submoules 적용

프로젝트 구조

  • Main Project
    • tuplus

  • Sub Project
    • db-fixture-rest-api
    • gateway
    • history
    • metadata
    • mock-storage
    • oci-storage
    • security
    • video-streaming
    • video-upload

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(video-streaming)에서 변경사항 반영 후 main project(tuplus)에서 변경사항을 추가하면 잘 반영됩니다.
# sub project pathcd video-streaming
➜  git add . && git commit -m "add test file" && git push 

# main project pathcd ..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

우리에게 시간은 돈입니다.

옵션을 넣어서 clone 합시다.
한 줄이면 됩니다.

git clone --recurse-submodules https://github.com/codelab-kr/tuplus.git

Pull

sub project를 각각 다른 팀원들이 관리한다면 우리는 sub project 마다 git pull 반복해야 합니다.
한번에 합시다.

git submodule update --remote

Foreach

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

profile
Think about a better architecture

0개의 댓글