paint-brush
Git Hooks로 Git 워크플로를 자동화하는 방법 🚀~에 의해@smy
새로운 역사

Git Hooks로 Git 워크플로를 자동화하는 방법 🚀

~에 의해 Syed Muhammad Yaseen4m2024/07/30
Read on Terminal Reader

너무 오래; 읽다

Git 후크는 Git 저장소에서 특정 이벤트가 발생할 때마다 자동으로 실행되는 스크립트입니다. 이를 통해 Git의 내부 동작을 사용자 정의하고 개발 수명 주기의 주요 지점에서 사용자 정의 가능한 작업을 트리거할 수 있습니다. 예를 들어 파일이 커밋되기 전에 Lint 검사를 실행하려고 하므로 사전 커밋 후크를 사용합니다.
featured image - Git Hooks로 Git 워크플로를 자동화하는 방법 🚀
Syed Muhammad Yaseen HackerNoon profile picture
0-item
1-item

안녕하세요오오오오오!


당신이 잘하고 있기를 바랍니다! 스미입니다! 👋 바로 뛰어들자 🚀


....

나는 모든 커밋에 대해 예제 commit-msg 후크를 설정했습니다.


↳ 지점 이름 규칙(예: [유형]/ABC-[티켓번호]-[목적])을 확인합니다.


↳ commit-msg 규칙을 확인합니다. 즉, [ABC-ticketnumber] [type]: 목표


↳ 린트, 더 예쁜지 확인하고 단위 테스트를 실행합니다.


요지 링크: https://gist.github.com/smyaseen/17083d60d02a07b2a3122410e2d39b6f

.....

내용물:

  • Wait What?
  • But Why?
  • But How?

1️⃣ 뭐 -


↳ Git 후크는 Git 저장소에서 특정 이벤트가 발생할 때마다 자동으로 실행되는 스크립트입니다.


↳ 이를 통해 Git의 내부 동작을 사용자 정의하고 개발 수명 주기의 주요 지점에서 사용자 정의 가능한 작업을 트리거할 수 있습니다.


↳ 로컬 후크와 서버측 후크가 있습니다.

↳ 로컬 후크는 원격 저장소의 컴퓨터와 서버 측에서 실행됩니다.


로컬 후크:


↳ 커밋 전 ↳ 준비-커밋-msg ↳ 커밋-msg ↳ 커밋 후 ↳ 체크아웃 후 ↳ 리베이스 전


서버 후크:


↳ 사전 수신 ↳ 업데이트 ↳ 사후 수신


2️⃣ 왜 -


↳ 예를 들어 파일이 커밋되기 전에 Lint 검사를 실행하려고 하므로 사전 커밋 후크를 사용합니다.


↳ 표준 브랜치 이름과 commit-msg 규칙을 적용하려는 또 다른 예이므로 commit-msg 후크를 사용합니다.


↳ 그리고 더 많은 것...


3️⃣ 어떻게 -


↳ 손쉬운 설정을 위해 JavaScript 프로젝트에서 Husky라는 라이브러리를 설치하고 설명서를 따르십시오.

https://www.npmjs.com/package/husky


↳ 예를 들어 브랜치 이름을 적용하고, 메시지 규칙을 커밋하고, Lint, Prettier 및 테스트를 실행하고 싶습니다.


↳ 여러 개의 후크를 사용할 수 있습니다. 제 경우에는 commit-msg 후크를 추가하겠습니다. 사전 커밋을 선택하지 않은 이유는 커밋 전에 실행되므로 commit-msg를 확인하지 않기 때문입니다. 나는 개발자가 검사를 실행하고 커밋 메시지에 실패하고 수정하고 모두 다시 실행하는 것을 원하지 않습니다. 먼저 커밋 메시지를 확인하고 싶습니다.


↳ Husky의 문서에 따라 commit-msg 후크를 생성하고 다음 예를 붙여넣습니다.


 #!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" # Checks for branch name currentBranch=$(git rev-parse --abbrev-ref HEAD) requiredPattern="^(build|chore|feat|docs|refactor|perf|test)/ABC-\d+-.+$" if ! echo "$currentBranch" | grep -qE $requiredPattern; then echo "\nInvalid branch name: $currentBranch" echo "-" echo "Should follow this pattern: build|chore|feat|docs|refactor|perf|test/ABC-ticketnumber-any-text" echo "-" echo "example: docs/ABC-123-update-readme.md" echo "-" echo "Refer to this for convention:" echo "-" echo "build : Changes related to building the code (eg adding npm dependencies or external libraries)." echo "-" echo "chore: Changes that do not affect the external user (eg updating the .gitignore file or .prettierrc file)." echo "-" echo "feat: A new feature." echo "-" echo "fix: A bug fix." echo "-" echo "docs: Documentation a related changes." echo "-" echo "refactor: A code that neither fix bug nor adds a feature." echo "-" echo "perf: A code that improves performance style: A code that is related to styling." echo "-" echo "test: Adding new test or making changes to existing test" echo "-\n" exit 1 # Branch name doesn't match the pattern, exit with error code fi # Checks for commit message commit_message="$(cat "$1")" pattern='^\[ABC-[0-9]+\] (build|chore|feat|docs|refactor|perf|test): .+$' if [[ ! $commit_message =~ $pattern ]]; then echo "\nInvalid commit message: $commit_message" echo "-" echo "Should follow this pattern: [ABC-ticketnumber] build|chore|feat|docs|refactor|perf|test: objective" echo "-" echo "example: [ABC-15] chore: updated .gitignore" echo "-" echo "Refer to this for convention:" echo "-" echo "build : Changes related to building the code (eg adding npm dependencies or external libraries)." echo "-" echo "chore: Changes that do not affect the external user (eg updating the .gitignore file or .prettierrc file)." echo "-" echo "feat: A new feature." echo "-" echo "fix: A bug fix." echo "-" echo "docs: Documentation a related changes." echo "-" echo "refactor: A code that neither fix bug nor adds a feature." echo "-" echo "perf: A code that improves performance style: A code that is related to styling." echo "-" echo "test: Adding new test or making changes to existing test" echo "-\n" exit 1 fi # npx lint-staged -- uncomment when have lint setted up # npm run test -- uncomment when have test setted up


↳ 이제 커밋할 때마다 린트 검사 실행과 함께 분기 및 커밋 명명 규칙을 확인합니다. 마지막으로, 다른 모든 것이 정상화되면 결국 테스트를 실행합니다.

4️⃣ 결과 -

✨ 일관성.

✨ 표준화.

✨ 사소한 것에 신경쓰지 마세요.

✨ 영향력을 자동화하고 집중하세요.


마무리:


Git Hooks를 사용하여 개발 워크플로를 향상시켰습니다. 🚀


.....


이제 개발 워크플로를 강화할 수 있습니다 🚀


그게 다야, 여러분! 여러분에게 좋은 독서가 되었기를 바랍니다. 감사합니다! ✨


👉 GitHubLinkedIn 에서 나를 팔로우하세요