iT邦幫忙

第 12 屆 iT 邦幫忙鐵人賽

DAY 20
1
DevOps

遠離 DevOops系列 第 20

[Day 20] Oops!gRPC - Auto Protoc Builder with Git Hooks

Git Hooks

git也有跟webhook類似的強大功能Git Hooks,是在git action觸發的同時去進行一些腳本。

git hooks 列表

Hook 名稱 觸發指令
applypatch-msg git am
pre-applypatch git am
post-applypatch git am
pre-commit git commit
prepare-commit-msg git commit
post-commit git commit
post-checkout git checkout and git clone
post-merge git merge or git pull
pre-push git push
pre-receive git-receive-pack on the remote repo
update git-receive-pack on the remote repo
post-update git-receive-pack on the remote repo
post-rewrite git commit --amend, git-rebase

實作

Auto Protoc Builder

在commit之前觸發上自動幫你透過builder製作proto檔案,並自動git add build完成的files。

自動化流程

edit proto file -> git add proto file -> git commit -> trigger git hook 「pre-commit」->
docker run proto-builder -> 檢查有無異動proto -> git add .pb.go files -> git commit finish !

所以我們今天要建立的git hook是pre-commit
建立pre-commit的shellscript
add-pre-commit.sh

#/bin/sh

echo '
#/bin/sh

echo "proto builder start"

docker run -v $(pwd):/go/src/app -w /go/src/app rain123473/proto-builder:g1.14-p3.12.0 sh -c "protoc *.proto --go_out=plugins=grpc:.;"

if [[ ! $(git diff --name-only) ]]; then
        echo "Needless To add"
    else
        git add *.pb.go
fi
'>.git/hooks/pre-commit

chmod 775 .git/hooks/pre-commit

然後在該目錄執行

sh add-pre-commit.sh

查看有無新增.git/hooks/pre-commit

cat .git/hooks/pre-commit

有顯示下列 script內容即已經綁定好pre-commit

#/bin/sh

echo "proto builder start"

docker run -v $(pwd):/go/src/app -w /go/src/app rain123473/proto-builder:g1.14-p3.12.0 sh -c "protoc *.proto --go_out=plugins=grpc:.;"

if [[ ! $(git diff --name-only) ]]; then
        echo "Needless To add"
    else
        git add *.pb.go
fi

這樣以後此proto的編輯者,只需專注於proto檔案的編輯,再也不用在擔心build的問題囉~ /images/emoticon/emoticon01.gif


上一篇
[Day 19] Oops!gRPC - Protoc 容器化實作
下一篇
[Day 21] Oops!Helm - 起手式
系列文
遠離 DevOops30

尚未有邦友留言

立即登入留言