はじめに
AWS CodeCommitを利用しようと思い、いざAWS CodeCommitを触ってみたら、自分のAWSアカウントはAWS SSOを利用していたので、
git cloneするときのユーザー名とかどうするのかわからなかったので調査しました。
環境/利用サービス
- AWS CodeCommit
- AWS SSO
- Git
事前準備
1. AWSのアカウントを準備
今回はAWSを利用するのでAWSのカウントを準備します。
AWS SSOの設定をしておきます。
リポジトリ作成
AWSコンソールからリポジトリを作成します。
サイドメニューの「リポジトリ」から「リポジトリを作成」ボタンを押します。
Git Clone
まず、リポジトリはCodeCommit上にあるので、AWSリソースへのアクセス権限が必要です。 AWS SSOを利用しているのでAWS SSOの画面から「Command line or programmatic access」からアクセス情報を取得して ~/.aws/credentials に記載します。
vim ~/.aws/credentials
[xxxxxxxxxxxx_PowerUserAccess]
aws_access_key_id=xxxxxxxxxxxx
aws_secret_access_key=xxxxxxxxxxxx
aws_session_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
その後、 git config コマンドでgitの設定を書き換えます。
git config --global credential.helper "!aws codecommit credential-helper $@"
git config --global credential.UseHttpPath true
無事、colneができたようです。
(base) xxxxx@test % git clone https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/test
Cloning into 'test'...
warning: You appear to have cloned an empty repository.
Git Push
pushも問題なくできました。
(base) xxxxx@test test % vim aa.txt
(base) xxxxx@test test % git add aa.txt
(base) xxxxx@test test % git commit -m 'test' aa.txt
[master (root-commit) 1e50894] test
1 file changed, 1 insertion(+)
create mode 100644 aa.txt
(base) xxxxx@test test % git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 208 bytes | 208.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/test
* [new branch] master -> master
macの問題点
Macを利用するとしばらくすると git pullしたときに「fatal: unable to access 'https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/test/': The requested URL returned error: 403」のエラーが発生します。
これは、CodeCommitが発行する認証情報が15分で無効になり、gitコマンドが使用するキーチェーンアクセスが無効になった認証情報を使ってアクセスを試行するために発生するようです。
fatal: unable to access 'https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/test/': The requested URL returned error: 403
その場合の対処方法としては、 macのキーチェーンアクセスを表示させ「CodeCommit」で検索、クリックして詳細画面を開き、「アクセス制御」タブから「git-credential-osxkeychain」を削除します。
osxkeychainを無効にする為、gitの設定を変更します。
sudo vim /Library/Developer/CommandLineTools/usr/share/git-core/gitconfig
[credential]
helper = osxkeychain
→
#helper = osxkeychain
まとめ
AWS CodeCommitを利用したことがなかったですが、一回設定するとgitの操作は同じなので問題なくできそうです。