Git / Submodules에 Husky 적용

Flexyz·2023년 9월 30일
0

Git

목록 보기
2/3
post-thumbnail

submodule + webhook

submodule의 소스를 commit 할 때 husky로 webhook을 실행하는 방법은 크게 두 가지 입니다.

  1. 모든 submodule에 husky 및 관련 종속성을 설치한다.

  2. main project 경로의 husky 및 관련 종속성을 submodule이 이용하도록 한다.


2번은 모든 서브모듈이 동일한 Git hook을 구성을 사용하도록 보장합니다. 프로젝트의 일관성과 신뢰성을 향상시키고 중복을 줄이기 위해 2번을 선택하기로 합니다.


서브 모듈의 웹훅 경로 변경 shell script를 작성합니다


mkdir -pv scripts/submodule
vi hooks-path.sh

git submodule foreach \
'path=$(echo $path | cut -d/ -f2) && \
if [ "$path" != "db-fixture-rest-api" ]; \
then git config core.hooksPath ../.husky && \
echo "$path changed hooksPath to ../.husky"; \
fi;'

위 코드는 특정 모듈(db-fixture-rest-api)을 제외한 모든 서브 모듈에 Git hook 경로를 "../.husky"로 설정합니다.



메인 프로젝트의 package.json에 husky 관련 설정을 추가합니다

{ ...
  "lint-staged": {
    "**/*{.ts,.js}": [
      "prettier --write",
      "eslint --fix"
    ]
  },
  "scripts": {
	...
    "prepare": "husky install",
    "postinstall": "sh ./scripts/submodule/hooks-path.sh" 
  },
  "devDependencies": {
    ...
    "@types/node": "^20.7.2",
    "@typescript-eslint/eslint-plugin": "^6.7.3",
    "@typescript-eslint/parser": "^6.7.3",
    "eslint": "^8.50.0",
    "eslint-config-airbnb-base": "^15.0.0",
    "eslint-config-prettier": "^9.0.0",
    "eslint-plugin-import": "^2.28.1",
    "eslint-plugin-jest": "^27.4.2",
    "eslint-plugin-prettier": "^5.0.0",
    "husky": "^8.0.3",
    "lint-staged": "^14.0.1",
    "prettier": "^3.0.3",
    "ts-node": "^10.9.1",
    "typescript": "^5.2.2"
  }
}

husky 적용 후 테스트 합니다.

# husky 적용
npm install
npx husky add .husky/pre-commit "npx lint-staged"

# ...서브 모듈 경로에서 소스 수정
git add . & git commit -m "test

# web hook 실행 확인

보너스

일괄 작업 & 전체 커밋


git submodule foreach \
'path=$(echo $path | cut -d/ -f2) && \
if [ "$path" != "scripts" | "$path" != "security" ]; \
then npm install && git add . && git commit -m "update @types/node version" && push && \
echo "$path push"; \
fi;'

npm install && git add . && git commit -m "update @types/node version"

참조: https://typicode.github.io/husky
커버이미지: Pixabay / Amy

profile
Think about a better architecture

0개의 댓글