3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

aws_iam_policy_attachment を含むコミットを禁止する

Last updated at Posted at 2019-04-09

TL;DR

  • aws_iam_policy_attachment を追加差分に含むコミットを reject する pre-commit hook を作った

経緯

  • aws_iam_policy_attachment をうっかりコミットしてしまうメンバーが後を絶たない
  • CI とか作ってサーバ側で防ぎたいんだけど、時間もないのでとりあえずローカルで機械的に防ぎたい

aws_iam_policy_attachment がいかに悪事を働くかについては以下の記事を参照:

コード

.git/hooks/pre-commit
#!/bin/sh

TABOO_WORD=aws_iam_policy_attachment

# 最新のコミットとの差分を比較し、タブー語を追加差分に含む場合は reject する
git diff HEAD -U0 "*.tf" | grep '^+[^+]' | grep "$TABOO_WORD" 1>/dev/null 2>/dev/null
if [ $? -eq 0 ]; then
  echo "[pre-commit FAILED..] 🔥 $TABOO_WORD は見つけ次第撲滅します!!!🔥"
  exit 1
fi

仕込み方

  1. プロジェクトのフォルダに移動して、上記のコードを .git/hooks/pre-commit にコピーする
  2. 実行属性を与える
    1. chmod +x .git/hooks/pre-commit

実行例

hoge/main.tf
aws_iam_policy_attachment
$ git add hoge/main.tf
$ git commit
[pre-commit FAILED..] 🔥 aws_iam_policy_attachment は見つけ次第撲滅します!!!🔥

参考

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?