AWS CodeCommit 設定
CodeCommitとは?
AWS(Amazon Web Service) が提供するリモートリポジトリサービス。
ローカルにあるGitリポジトリをAWS側にアップロードすることにより、
複数ユーザによるソースコード管理が実現でき、まさかのPC障害によるデータ破損にも対応できる。
この他にも有名なサービスに、GitHub や BitBucketがある。
不特定多数の人間が開発に関わることを強みとしているのがGitHub。
小人数であればプライベートリポジトリを無料で利用(容量制限なし)できるのがBitBucketという印象がある。
なお、料金はアクティブユーザが5人以下であれば無料らしい。
料金 - Amazon CodeCommit | AWS
今回の設定内容
環境は Windows 7 Professional(SP1) 64bit。
ローカル環境にGitリポジトリを作成、CodeCommit 上のリモートリポジトリにアップロード。
Git Bash を使って、git push でリポジトリに変更が反映できるまでの確認を行う。
AWS CLI インストール
以下にアクセス。
AWS コマンドラインインターフェイス | AWS
ダウンロードしたMSIをインストール。デフォルト設定で問題なし。
インストール完了後は、Git Bashを開きインストール確認。
aws --version
以下のコマンドを叩いてバージョン情報が出ればインストール完了。
リモートリポジトリのアクセスユーザ設定
AWSにアクセス
クラウドならアマゾン ウェブ サービス【AWS 公式】
「セキュリティ認証情報」を選択
(ログイン画面が出たらログインすること)
「IAMユーザの使用開始」を選択
ユーザ作成
グループが存在しなかった場合は作成。
グループに対する権限設定は後で変更できる。今回は管理者権限を付与。
グループができるので、選択して「確認」へ。
ユーザ作成に成功したら、念のために認証情報をダウンロードしておく方が良いと思う。
なお、中身はアクセスキーIDとシークレットアクセスキーの情報が書いてある。
ローカルリポジトリにリモートリポジトリ紐づけ
以下を参考に実施。
Code Commitの使い方 - Qiita
AWS認証情報をローカルに登録。
profile=codecommit aws configure --profile $profile
必要な情報を入力する。
AWS認証情報登録の確認。
cat ~/.aws/credentials cat ~/.aws/config
今回は、ローカルリポジトリをsampleディレクトリに作成。
mkdir sample cd sample/ git init
リモートリポジトリアクセス時にAWS認証情報を使用するよう設定。
今回リージョンは「us-west-2」。そこは適宜変更する。
profile=codecommit region=us-west-2 # 現在のリポジトリのみ設定する場合 git config --local credential.helper '!aws codecommit --region '$region' --profile '$profile' credential-helper $@' git config --local credential.UseHttpPath true # 全てのgitリポジトリに設定する場合 git config --global credential.helper '!aws codecommit --region '$region' --profile '$profile' credential-helper $@' git config --global credential.UseHttpPath true
設定確認。
# 現在のリポジトリのみ設定する場合 cat ./.git/config # 全てのgitリポジトリに設定する場合 cat ~/.gitconfig
リモートリポジトリ宛先取得。
リモートリポジトリ登録。
url=https://git-codecommit.us-west-2.amazonaws.com/v1/repos/JUnitSample git remote add origin $url
リモートリポジトリ設定確認。
git remote -v
設定完了、挙動確認
ローカルの内容をリモートリポジトリにアップロード。
echo "AAA" > a.txt git add -A git commit -m "First Commit." git push origin master
CodeCommitにも a.txt が反映されればOK。
上手くいかなかった場合...
git push 時に403のエラーコードが出た場合は、以下の原因が考えられる。
- 認証設定が上手くいっていない
- 作成ユーザに適切な権限がなく実行できない
私は、git push 時、認証情報用のプロンプトが現れて、
正しいアクセス情報を入力しても403エラーになる事象が起こった。
調べてみると、GitのCredential Managerが有効になっていたためだった。
Troubleshooting AWS CodeCommit - AWS CodeCommit
※インストール時デフォルトでは有効になっている。
おまけ
シェルを作成してみた。
git リポジトリ上のパスで credential.csv および下記シェルを(aws_codecommit.sh)として実行。
引数に CodeCommit のリポジトリパスを渡して実行する。なお英語はかなり適当。
#!/bin/bash #----------------- # Set up AWS CodeCommit Authenticator. # 1. Put this shell and your credentials.csv into a directory which has .git directory. # 2. Execute this shell with repository url. #----------------- # usage: aws_codecommit.sh <url> [-p <profile>] [-r <region>] [--local | --global] # param <url>: repository url # param <region>: [default] Extract from <url> # param <profile>: [default] "codecommit" #----------------- URL="" REGION="" PROFILE="codecommit" GLOBAL=true CRED_CSV="./credentials.csv" URL_PATTERN="https:\/\/git-codecommit\.\([^.]\+\)\.amazonaws\.com\/.*" function set_param() { test -z `echo $1 | grep -e $URL_PATTERN` && return 1 URL=$1 REGION=`echo $1 | sed -e 's/^'$URL_PATTERN'$/\1/g'` shift while [ ! -z $1 ]; do case $1 in '-p') PROFILE=$2 shift;; '-r') REGION=$2 shift;; '--local') GLOBAL=false;; '--global') GLOBAL=true;; * ) return 1;; esac shift done return 0 } function show_err() { echo "[Error]: $1" } function show_usage() { cat $0 | grep -e "^# \(usage:\|param \)" | sed 's/^# \(.\+\)$/\1/g' } #------------------ # Main #------------------ set_param $@ if [ $? -ne 0 ]; then show_usage exit 1 fi if [ ! -e $CRED_CSV ]; then show_err "Not found credential file." exit 1 fi TMP_IFS=$IFS IFS="," set `sed -n 2P $CRED_CSV` KEY_ID="$3" SECRET_KEY="$4" IFS=$TMP_IFS if [ -z $KEY_ID -o -z $SECRET_KEY ]; then show_err "Invalid credential." exit 1 fi which aws > /dev/null if [ $? -ne 0 ]; then show_err "AWS CLI is not installed." exit 1 fi aws configure --profile $PROFILE > /dev/null << EOL $KEY_ID $SECRET_KEY $REGION EOL if [ $? -ne 0 ]; then show_err "Failed \"aws configure\"" exit 1 fi which git > /dev/null if [ $? -ne 0 ]; then show_err "Git is not installed." exit 1 fi GCONF_OPT="" case $GLOBAL in true) GCONF_OPT="--global";; false) GCONF_OPT="--local";; esac git config $GCONF_OPT credential.helper '!aws codecommit --region '$REGION' --profile '$PROFILE' credential-helper $@' && git config $GCONF_OPT credential.UseHttpPath true if [ $? -ne 0 ]; then show_err "Failed \"git config.\"." exit 1 fi git remote add origin $URL if [ $? -ne 0 ]; then show_err "Failed \"git remote add\"." exit 1 fi exit 0