使用aws codecommit作为私有的git远程服务器
博客的hexo的代码,一直是保存在本地的。上次电脑花了一次屏后,感觉到保存在本地实在是不够安全。因此考虑寻觅一个远端的私密git库,存起来。
开始寻寻觅觅合适的仓库.
- Github?,私有库是收费的。但为了这些哪天就不更新的markdown,每月7刀开个Developer的Plan,感觉不划算。
- 自建Gitlab?嫌麻烦麻烦。
- oschina的私有库?不想用。
- 。。。。。。
好吧,我承认我就是想尝试用一下aws的codecommit。
关于CodeCommit的免费额度,官网介绍:
- 最初5位活动用户
- 无限存储库
- 50GB的月存储量
- 每月10000个git请求
托管我一个小博客,妥妥的够了。毕竟除了我,没人还会来关心这点markdown文件, 5位用户免费足够了。至于50GB的月存储量么,除非把看过的电影都commit进git来,要不然应该是足够了。
创建Repository
在aws console的Services中,找到CodeCommit
在CodeCommit页面中点击Create, 打开新建Repository的页面,在Repository Name中填入仓库的名字,Description中填写仓库的描述, 然后点击Create repository创建仓库。
配置ssh key
CodeCommit有两种访问方式,分别是ssh和https模式。我习惯使用ssh方式。
首先,需要在IAM User中添加SSH keys, 用来访问CodeCommit。
- 打开IAM,切换到User界面,
- 在
Security credentials
的tab下,找到SSH keys for AWS CodeCommit
一栏 - 点击下面
Upload SSH public key
按钮 - 在打开的上传key的页面中输入常用的key pair的public key, 然后点击
Upload SSH public Key
的按钮
上传完毕后,就会生成一个新的Entry,复制或保存此处SSH key ID的值。
配置本地~/.ssh/config, 添加有关CodeCommit的Host条目,
IdentityFile
设置为private key, 并保存。如果本地还没有~/.ssh/config文件,则创建,并在保存后使用命令chmod 600 ~/.ssh/config
将访问属性修改为600.123Host git-codecommit.*.amazonaws.comUser APKAIHTDFHHOYTHJYI3QIdentityFile ~/.ssh/id_rsa打开codeCommit的repository, 点击
Clone URL
,选择SSH来获取仓库的ssh地址, 我此处的地址是 ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/my_blog找个临时目录,使用git clone命令来测试是否可以正常访问新建的仓库
|
|
push已有的repository到CodeCommit
刚建立的repository是空的,我们可以clone下来,然后逐次添加文件,也可以将已经存在的git reposigory push到CodeCommit上的空仓库中。
切换到已有的git 仓库中,然后使用如下命令将git仓库push到CodeCommit中
如下是将本地的my_blog推送到远端的步骤:
本地的remote为空
12$ git remote -v$推送本地的所有代码到CodeCommit上
12345678910$ git push ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/my_blog --allWarning: Permanently added the RSA host key for IP address '52.95.194.107' to the list of known hosts.Counting objects: 490, done.Delta compression using up to 8 threads.Compressing objects: 100% (463/463), done.Writing objects: 100% (490/490), 6.71 MiB | 441.00 KiB/s, done.Total 490 (delta 214), reused 0 (delta 0)remote: processing To ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/my_blog* [new branch] master -> master$将CodeCommit上的仓库设置为远端的origin
1234$ git remote add origin ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/my_blog$ git remote -vorigin ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/my_blog (fetch)origin ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/my_blog (push)关联本地和远端的master分支
12345$ git branch --set-upstream-to=origin/master masterBranch master set up to track remote branch master from origin.$ git pullAlready up-to-date.$
Reference
- 将 Git 存储库迁移到 AWS CodeCommit - https://docs.aws.amazon.com/zh_cn/codecommit/latest/userguide/how-to-migrate-repository-existing.html
- 在 Linux, macOS, or Unix 上设置到 AWS CodeCommit 存储库的 SSH 连接的步骤 - https://docs.aws.amazon.com/zh_cn/codecommit/latest/userguide/setting-up-ssh-unixes.html#setting-up-ssh-unixes-keys-unixes