프로그래밍/DevOps

Native(Android, iOS) 개인 개발환경 구축하기 - git commit 전에 ktlint, push된 이후에 자동빌드 하기 (ktlint, git hook, bitrise web hook)

freemmer 2021. 6. 28. 15:06

Native(Android, iOS) 개인 개발환경 구축하기 - git commit 전에 ktlint, push된 이후에 자동빌드 하기 (ktlint, git hook, bitrise web hook)

이번 포스트에서는 git hook에 관련된 내용을 다뤄 보겠습니다. (아래 빨간색 선 부분)
물론, 전문적인 DevOps를 다루시는 분들이 보신다면 너무 기초적인 내용이겠지만, 개인적으로 사이드 프로젝트를 진행하실 때는 괜찮게 써먹을 만한 자료가 될 것 같습니다.
그럼 각설하고 시작하겠습니다.

Git hook 이란?

In computer programming, the term hooking covers a range of techniques used to alter or augment the behaviour of an operating system, of applications, or of other software components by intercepting function calls or messages or events passed between software components. Code that handles such intercepted function calls, events or messages is called a hook.
Reference : wikipedia

함수 호출, 메세지나 이벤트가 발생 하기 전/후에 호출 코드를 Hook 이라고 합니다.

git에서도 Push, Commit 등의 이벤트가 발생했을 때 스크립트를 실행시킬 수 있는데 이를 git hook이라고 하며, Shell, Perl, Python 등의 Script를 사용할 수 있습니다.

클라이언트 훅(Client hook)과 서버 훅(Server hook)이 있는데, 우리는 클라이언트 훅에서 pre-commit, 서버 훅에서 post-update 를 사용합니다.

사용할 git hook

* pre-commit

git에 커밋(commit)하기 전에 클라이언트에서 호출되는 훅 입니다. 이 훅을 사용해 ktlint 검사를 하도록 합니다.

pre-coomit 훅은 exit가 0이 아닐 경우 커밋이 취소가 되며, git commit —no-verify를 통해 일시적으로 pre-commit 훅을 패스할 수 있습니다.

clone한 repository에서 숨김 처리된 hooks 디렉토리에 들어가보면 아래와 같이 다양한 hook의 sample이 있습니다.

cd .git/hooks
ls
applypatch-msg.sample post-update.sample pre-commit.sample pre-receive.sample
commit-msg.sample pre-applypatch.sample pre-push.sample prepare-commit-msg.sample
fsmonitor-watchman.sample pre-rebase.sample update.sample

cp pre-commit.sample pre-commit으로 pre-commit을 복사한 후 아래와 같이 수정해 commit 전에 ktlint를 수행하고 성공해야만 commit 되도록 합니다.

#!/bin/sh
#window : !/bin/bash
echo "> Starting ktlint..."
CHANGED_FILES="$(git --no-pager diff --name-status --no-color --cached -- TechBlog/ | awk '$1 != "D" && $NF ~ /\.kts?$/ { sub("TechBlog/","",$NF); print $NF}')"
echo "Running ktlint over these files:"
echo $CHANGED_FILES
result=$(./TechBlog/gradlew -p ./TechBlog ktlintCheck -PinternalKtlintGitFilter="$CHANGED_FILES" 2>&1)
status=$?
if [ "$status" = 0 ] ; then
echo "> Completed ktlint hook."
exit 0
else
echo 1>&2 "> Error ktlint hook."
echo "============================"
echo $result
echo "============================"
exit 1
fi
* post-update
git push 후 서버에서 실행되는 훅 입니다. 이 훅을 사용해 develop branch가 push되면 bitrise에 web hook을 호출하여 자동 빌드가 되도록 합니다.
모든 push 이벤트 후 자동 빌드를 해도 되지만, 제 경우 bitrise 무료 플랜을 사용하기 때문에 자동 빌드 횟수를 줄이고자 develop branch에서만 동작하도록 했습니다.
(git-flow를 사용하신다면, 대부분 feature를 생성해 개발하신 뒤 finish 했을 경우에 자동 빌드가 동작될 것 이며, release나 hot-fix의 경우에는 자동 빌드보다는 확인 후 메뉴얼 빌드를 하고자 제외 했습니다. 원하실 경우 아래 스크립트를 수정하셔서 사용하시면 됩니다) 
#!/bin/sh
branch=$(git log --branches -1 --pretty=format:'%D' | sed 's/.*, //g')
echo $branch
if [ x"develop" = x"$branch" ]; then
# bitrist web hook .
exec curl -X POST -H "Authorization: <bitrise ACCESS-TOKEN>" "https://api.bitrise.io/<bitrise app-slug>" -d \
'{
"hook_info": {
"type": "bitrise"
},
"build_params": {
"branch": "develop"
}
}'
# nas_notify.sh script .
# , .
#exec ./nas_notify.sh "git push & bitrise : $branch"
echo $branch
else
#
#exec ./nas_notify.sh "git push : $branch"
echo $branch
fi

Bitrise web hook

다음 'bitrise로 자동 빌드 하기’ 포스트에서 자세하게 다루겠지만, 우선 web hook를 전송할 때 필요한 값 ACCESS-TOKEN와 app-slug 코드를 얻는 부분만 언급 하겠습니다.
* ACCESS-TOKEN은 Account Settings > Security tab > Generate new token 버튼을 클릭해 얻을 수 있습니다.


* app-slug는 Dashboard에서 빌드할 app을 선택한 뒤 주소창에서 확인 할 수 있습니다. (https://app.bitrise.io/app/<app-slug># 마지막 #문자의 앞부분 까지가 app-slug 입니다)


반응형