- git-secretsとは
- git-secretsの導入方法(Windows編)
git-secretsとは
- gitのcommit/commit message等の中に事前設定した公開すべきではない情報が含まれていた場合に、そのcommitを却下(リジェクト)する。
- コミットを却下するので、秘密情報を共有リポジトリ上に公開するリスクが低減される。
git-secretsの導入方法(Windows編)
手順1.git-secretsをzip
でダウンロードする
GitHubのgit-secrets
のページからzip
でダウンロードする
- awslabs/git-secrets ←ここから移動
手順2.適当な場所に展開する
- どこでもOKです
手順3.インストールする
.ps1
というファイルをコマンド実行する際のツールはWindows PowerShell
というツールです。- コマンドプロンプト上でコマンド実行してもエラーになります。
手順3-1.Windows PowerShell
を起動する
手順3-2.install.ps1
を実行する
Windows PowerShell
上のカレントディレクトリを1-2
で展開したディレクトリに移動してください。- ディレクトリ内に
install.ps1
というファイルが入っていることを確認してください。
- ディレクトリ内に
./install.ps1
を実行する。
Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved. 新しいクロスプラットフォームの PowerShell をお試しください https://aka.ms/pscore6 PS C:\Users\Test> cd C:\Users\Test\Documents\Program\git PS C:\Users\Test\Documents\Program\git> cd .\git-secrets\ PS C:\Users\Test\Documents\Program\git\git-secrets> ./install.ps1 ./install.ps1 : このシステムではスクリプトの実行が無効になっているため、ファイル C:\Users\Test\Documents\Program\git \Santana\other\git-secrets\install.ps1 を読み込むことができません。詳細については、「about_Execution_Policies」(https:/ /go.microsoft.com/fwlink/?LinkID=135170) を参照してください。 発生場所 行:1 文字:1 + ./install.ps1 + ~~~~~~~~~~~~~ + CategoryInfo : セキュリティ エラー: (: ) []、PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess
→ 上記エラーが発生しなかった場合は、1-3-5
まで飛ばして下さい。
手順3-3.install.ps1
でのエラーへの対応方法
→コマンド実行に対するセキュリティレベルの変更
上記エラーの原因は、
PowerShell のスクリプトの実行が実行ポリシーによって許可されていない
からです。今回は、実行ポリシーを
Restricted
からRemoteSigned
に変更したいと思います。まずは、変更前の実行ポリシーを確認します。
PS C:\Users\Test\Documents\Program\git\git-secrets> Get-ExecutionPolicy Restricted
- 次に、実行ポリシーを変更します。
PS C:\Users\Test\Documents\Program\git\git-secrets> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process 実行ポリシーの変更 実行ポリシーは、信頼されていないスクリプトからの保護に役立ちます。実行ポリシーを変更すると、about _Execution_Policies のヘルプ トピック (https://go.microsoft.com/fwlink/?LinkID=135170) で説明されているセキュリティ上の危険にさらされる可能性があります。実行ポリシーを変更しますか? [Y] はい(Y) [A] すべて続行(A) [N] いいえ(N) [L] すべて無視(L) [S] 中断(S) [?] ヘルプ (既定値は "N"):Y
- 変更後の実行ポリシーを確認します。
PS C:\Users\Test\Documents\Program\git\git-secrets> Get-ExecutionPolicy RemoteSigned
手順3-4.install.ps1
を実行する(2回目)
PS C:\Users\Test\Documents\Program\git\git-secrets> ./install.ps1 Checking to see if installation directory already exists... Creating installation directory. Copying files. Checking if directory already exists in Path... Adding to path. Adding to user session. Done.
→ install
が成功しました。
手順3-5.git securets
をグローバル登録しておく
PS C:\Users\Test\Documents\Program\git\git-secrets> git secrets --register-aws --global OK
→ここまでが基本的な準備です。
手順4.ワークスペースにgit securets
を適用する
- ワークスペースの
.git ディレクトリ
があるところでgit secrets --install
を実行します
PS C:\Users\Test\Documents\Program\git\git-secrets> cd ..\workspace\ PS C:\Users\Test\Documents\Program\git\workspace> git secrets --install ✓ Installed commit-msg hook to .git/hooks/commit-msg ✓ Installed pre-commit hook to .git/hooks/pre-commit ✓ Installed prepare-commit-msg hook to .git/hooks/prepare-commit-msg
手順5.gitのクライアントツールに適用する?
- 私は
SourceTree
というクライアントツールを使用しているのですが、以下の手続きを実施しないとエラーになってしまいました。 - Windowsでは
%USERPROFILE%\.git-secrets
にあるgit-secrets
、git-secrets.1
をGitコマンドのあるところ(C:\Program Files\Git\cmd
など)にコピーしてください。
上記で、設定は完了です。