Skip to content

Instantly share code, notes, and snippets.

@waday
Last active January 15, 2017 02:39
Show Gist options
  • Save waday/3964d31ef9b366fa0976845743fc4454 to your computer and use it in GitHub Desktop.
Save waday/3964d31ef9b366fa0976845743fc4454 to your computer and use it in GitHub Desktop.
GitLabリポジトリをCodeCommitに自動同期するスクリプト(AWSにリポジトリがなければ自動作成)

awscliインストール

yum install epel-release yum install python-pip pip install awscli

cat ~/.aws/config

[default]
output = json
region = ap-northeast-1
[profile CodeCommitProfile]
region = us-east-1
output = json

cat ~/.aws/credentials

[default]
aws_access_key_id = AKI〜
aws_secret_access_key = 〜
[CodeCommitProfile]
aws_access_key_id = AKI〜
aws_secret_access_key = 〜

gitの設定

HTTPSでPUSHする場合

git config --global credential.helper '!aws --profile CodeCommitProfile codecommit credential-helper $@' git config --global credential.UseHttpPath true

Proxy配下からPUSHする場合

git config --global http.proxy http://認証ID:認証PW@プロキシサーバのURL:ポート番号 git config --global https.proxy http://認証ID:認証PW@プロキシサーバのURL:ポート番号

jqコマンドインストール

sudo curl -o /usr/bin/jq http://stedolan.github.io/jq/download/linux64/jq && sudo chmod +x /usr/bin/jq

sync_codecommit.shを任意の場所に配置して定期実行設定

chmod 755 /path/to/sync_codecommit.sh crontab -e

#!/bin/bash
# pip install awscli
# curl -o /usr/bin/jq http://stedolan.github.io/jq/download/linux64/jq && sudo chmod +x /usr/bin/jq
HOST_INFO="This is mirror repository of `hostname`. Changes are automatically reflected."
MIRROR_URL_BASE=https://git-codecommit.us-west-2.amazonaws.com/v1/repos
AWSCLI_PROFILE_OPT='--profile CodeCommitProfile'
gitlab_repo_dir_list=`find /srv/docker/gitlab/gitlab/repositories/ -mindepth 2 -maxdepth 2 -type d`
aws_repo_arr=(`aws ${AWSCLI_PROFILE_OPT} codecommit list-repositories | jq -r '.repositories[].repositoryName'`)
function print_log() {
aws_reponame=$1
msg=$2
date_txt=`date '+%Y/%m/%d-%H:%M:%S'`
echo "${date_txt} ${aws_reponame}: $msg"
}
function search_repo_from_aws() {
IFS_BAK=$IFS
IFS=$'\n'
echo "${aws_repo_arr[*]}" | grep -q $1
rc=$?
IFS=$IFS_BAK
echo $rc
}
for glrepopath in $gitlab_repo_dir_list; do
user_repo_arr=(`echo $glrepopath | awk -F'repositories/' '{print $2}' | awk -F'/' '{print $1" "$2}'`)
user=${user_repo_arr[0]}
repo=`echo ${user_repo_arr[1]} | cut -d'.' -f1`
aws_reponame=${user}_${repo}
if [ "$(search_repo_from_aws $aws_reponame)" != "0" ]; then
print_log "${aws_reponame}" "AWSにリポジトリがありません"
print_log "${aws_reponame}" "リポジトリを作成します"
aws ${AWSCLI_PROFILE_OPT} codecommit create-repository --repository-name "${aws_reponame}" --repository-description "${HOST_INFO}"
print_log "${aws_reponame}" "RC=$?"
fi
cd $glrepopath
print_log "${aws_reponame}" "AWSにpushします"
git push --mirror ${MIRROR_URL_BASE}/${aws_reponame}
print_log "${aws_reponame}" "RC=$?"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment