博客的hexo的代码,一直是保存在本地的。上次电脑花了一次屏后,感觉到保存在本地实在是不够安全。因此考虑寻觅一个远端的私密git库,存起来。

开始寻寻觅觅合适的仓库.

  • Github?,私有库是收费的。但为了这些哪天就不更新的markdown,每月7刀开个Developer的Plan,感觉不划算。
  • 自建Gitlab?嫌麻烦麻烦。
  • oschina的私有库?不想用。
  • 。。。。。。

好吧,我承认我就是想尝试用一下aws的codecommit。

关于CodeCommit的免费额度,官网介绍:

  • 最初5位活动用户
    • 无限存储库
    • 50GB的月存储量
    • 每月10000个git请求

托管我一个小博客,妥妥的够了。毕竟除了我,没人还会来关心这点markdown文件, 5位用户免费足够了。至于50GB的月存储量么,除非把看过的电影都commit进git来,要不然应该是足够了。

创建Repository

  1. 在aws console的Services中,找到CodeCommit
    find_codecommit

  2. 在CodeCommit页面中点击Create, 打开新建Repository的页面,在Repository Name中填入仓库的名字,Description中填写仓库的描述, 然后点击Create repository创建仓库。
    codecommit_create_repository

配置ssh key

  1. CodeCommit有两种访问方式,分别是ssh和https模式。我习惯使用ssh方式。

  2. 首先,需要在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的按钮
      upload_ssh_for_codecommit.jpg
  3. 上传完毕后,就会生成一个新的Entry,复制或保存此处SSH key ID的值。
    publickey_of_IAM_used_for_codecommit.jpg

  4. 配置本地~/.ssh/config, 添加有关CodeCommit的Host条目, IdentityFile设置为private key, 并保存。如果本地还没有~/.ssh/config文件,则创建,并在保存后使用命令chmod 600 ~/.ssh/config将访问属性修改为600.

    1
    2
    3
    Host git-codecommit.*.amazonaws.com
    User APKAIHTDFHHOYTHJYI3Q
    IdentityFile ~/.ssh/id_rsa
  5. 打开codeCommit的repository, 点击Clone URL,选择SSH来获取仓库的ssh地址, 我此处的地址是 ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/my_blog
    get_ssh_url_of_codecommit

  6. 找个临时目录,使用git clone命令来测试是否可以正常访问新建的仓库

1
2
3
4
5
6
7
8
9
10
11
12
$ git clone ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/my_blog
Cloning into 'my_blog'...
The authenticity of host 'git-codecommit.ap-northeast-2.amazonaws.com (52.95.194.93)' can't be established.
RSA key fingerprint is 9f:68:48:9b:5f:fc:96:69:39:45:58:87:95:b3:69:ed.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'git-codecommit.ap-northeast-2.amazonaws.com,52.95.194.93' (RSA) to the list of known hosts.
warning: You appear to have cloned an empty repository.
Checking connectivity... done.
$ ll
total 0
drwxr-xr-x 3 carlshen staff 102 10 24 21:32 my_blog
$

push已有的repository到CodeCommit

刚建立的repository是空的,我们可以clone下来,然后逐次添加文件,也可以将已经存在的git reposigory push到CodeCommit上的空仓库中。

切换到已有的git 仓库中,然后使用如下命令将git仓库push到CodeCommit中

1
git push https://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyFirstRepo --all

如下是将本地的my_blog推送到远端的步骤:

  1. 本地的remote为空

    1
    2
    $ git remote -v
    $
  2. 推送本地的所有代码到CodeCommit上

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    $ git push ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/my_blog --all
    Warning: 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
    $
  3. 将CodeCommit上的仓库设置为远端的origin

    1
    2
    3
    4
    $ git remote add origin ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/my_blog
    $ git remote -v
    origin 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)
  4. 关联本地和远端的master分支

    1
    2
    3
    4
    5
    $ git branch --set-upstream-to=origin/master master
    Branch master set up to track remote branch master from origin.
    $ git pull
    Already up-to-date.
    $

Reference

  1. 将 Git 存储库迁移到 AWS CodeCommit - https://docs.aws.amazon.com/zh_cn/codecommit/latest/userguide/how-to-migrate-repository-existing.html
  2. 在 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

留言