Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Devops/refactor/#573 husky 리팩토링 #574

Merged
merged 28 commits into from
Feb 26, 2024

Conversation

kimyu0218
Copy link
Collaborator

@kimyu0218 kimyu0218 commented Feb 23, 2024

🔮 resolved #573

변경 사항

  • husky 최상위 디렉토리로 이동
    • pre-commit : lint-staged 실행
    • pre-push : 브랜치 네임 검사
  • frontend, backend env 파일 합침
  • backend/was에 별칭 적용

고민과 해결 과정

1️⃣ pre-commit - 커밋 전에 lint-staged 실행하기

pre-commit은 커밋에 의해 트리거되는 훅이다. 커밋을 실행하기 전에 코드 포맷팅과 린트를 수행하도록 만들었다.

#!/usr/bin/env sh

. "$(dirname -- "$0")/_/husky.sh"
npx lint-staged

lint-staged스테이징된 파일에 대해서만 린트를 수행한다. 이전에는 전체 프로젝트에 대해 린트를 수행했는데, 이는 실행 속도를 저하시키고, 이번 커밋에서 수정하지 않은 파일도 수정하기 때문에 비효율적이라고 판단했다.

// backend/was/package.json

"lint-staged": {
  "{src,apps,libs,test}/**/*.ts": [
    "prettier --write",
    "eslint --cache --fix"
  ]
},

npx lint-staged를 실행하면 package.jsonlint-staged가 실행되어 코드 포맷팅, 린트가 자동으로 수행된다.

2️⃣ pre-push - 푸시 전에 브랜치 네이밍 강제하기

pre-push는 푸시에 의해 호출되는 훅이다. PR을 설정하는 깃헙 액션에서 pr-labeler가 브랜치명을 기반으로 라벨을 부착하기 때문에 브랜치 네이밍을 강제해야 한다.

#!/usr/bin/env sh

. "$(dirname -- "$0")/_/husky.sh"

BRANCH=$(git rev-parse --abbrev-ref HEAD)

ALLOWED_BRANCH_PATTERN="^(BE|FE|DEVOPS|BE,FE|FE,BE)\/(feature|bugfix|hotfix|refactor)(,(feature|bugfix|hotfix|refactor))*\/#[0-9]+(-#[0-9]+)*.+$"

if ! [[ $BRANCH =~ $ALLOWED_BRANCH_PATTERN ]]; then
  echo "Error: You are not allowed to push to the branch \"$BRANCH\""
  exit 1
fi

exit 0

브랜치명을 가져오고, 이를 정규식과 비교하여 일치하는 경우에만 푸시할 수 있도록 한다.

(선택) 테스트 결과

  • 브랜치 이름이 정규식과 일치하지 않는 경우, 푸시할 수 없음

image

변경된 파일에 대해서 코드 포맷터, 린트를 실행하는 lint-staged 설치
공통으로 사용하는 패키지는 최상위에서 관리
최상위 디렉토리에서 husky 사용하기로 결정
커밋을 수행하기 전에 변경사항에 대해 린트 및 포맷터 실행
푸시를 수행하기 전에 브랜치 패턴을 검사하여 정규식을 만족하지 않는
브랜치로 푸시하는 것을 예방
pre-push 훅에 의해 브랜치명이 강제되므로 깃허브에서 액션을 돌릴 필요가
없음
`\d`가 동작하지 않아 `0-9`로 변경
- 기여자가 자동으로 표시되므로 기여자를 작성한 푸터 삭제
- 이스케이프 문자 수정
@kimyu0218 kimyu0218 force-pushed the DEVOPS/refactor/#573-husky-리팩토링 branch from 069789a to 1855ef3 Compare February 24, 2024 07:48
@kimyu0218 kimyu0218 marked this pull request as ready for review February 24, 2024 08:07
@kimyu0218
Copy link
Collaborator Author

Important

frontend/_husky/pre-commit에 프리티어 쉘 스크립트를 실행하던데, 해당 파일이 깃허브에 안 올라와있어서 처리를 못했어. 없어도 되는 건지 확인부탁해~

아래 코드는 프론트엔드 lint-staged야. 프리티어는 내 마음대로 작성했고, 린트는 스크립트 보고 작성했어!!

"lint-staged": {
  "*.{ts,tsx}": [
    "prettier --write",
    "eslint --cache --fix --report-unused-disable-directives --max-warnings 0"
  ]
},

src 디렉토리 외부에 위치하는 test 디렉토리에서 src에서 사용하는 paths
별칭을 이해할 수 있도록 moduleNameMapper 작성
- include : ts 컴파일러가 컴파일할 파일 지정하는 옵션
- e2e 테스트를 돌리기 위해서는 test 디렉토리도 포함해야 함
@@ -19,5 +19,5 @@
"noFallthroughCasesInSwitch": true
},
"typeRoots": ["../../@types"],
"include": ["src", "../../@types"]
"include": ["src", "test", "../../@types"]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e2e 테스트 파일이 src 디렉토리 외부에 존재해서 test 디렉토리도 추가해야 e2e 테스트 실행할 수 있어!!

- 원격 서버로 복사하는 파일이 많아 source 옵션이 너무 길어짐
- 가독성을 위해 와일드카드 사용
  - env 파일 복사
  - 쉘 스크립트 복사
  - deploy용 컴포즈 파일 복사
  - nginx 도커파일 복사
@Doosies
Copy link
Collaborator

Doosies commented Feb 26, 2024

Important

frontend/_husky/pre-commit에 프리티어 쉘 스크립트를 실행하던데, 해당 파일이 깃허브에 안 올라와있어서 처리를 못했어. 없어도 되는 건지 확인부탁해~

아래 코드는 프론트엔드 lint-staged야. 프리티어는 내 마음대로 작성했고, 린트는 스크립트 보고 작성했어!!

"lint-staged": {
  "*.{ts,tsx}": [
    "prettier --write",
    "eslint --cache --fix --report-unused-disable-directives --max-warnings 0"
  ]
},

없어도 괜찮아! 전에 작성하다 말았던거라서 작동을 잘 안해...

Copy link
Collaborator

@Doosies Doosies left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋다!! 👍 👍 👍

@kimyu0218 kimyu0218 merged commit 1a1d2ef into dev Feb 26, 2024
@kimyu0218 kimyu0218 deleted the DEVOPS/refactor/#573-husky-리팩토링 branch February 26, 2024 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🔁 husky 리팩토링
3 participants