Skip to content

Instantly share code, notes, and snippets.

@shotahorii
Last active July 17, 2019 22:29
Show Gist options
  • Save shotahorii/7e99e577ff09a2cf49a0c4ec27224ed0 to your computer and use it in GitHub Desktop.
Save shotahorii/7e99e577ff09a2cf49a0c4ec27224ed0 to your computer and use it in GitHub Desktop.
Using Git on EC2

Using Git on EC2

patternables.comはEC2上にWebサーバを立てて動かしているが、これまでは特にGitなどでコードを管理していなかった。
EC2にGitを入れてバージョン管理(更新管理)するようにしたのでその手順を記録しておく。

Step1

EC2上にバージョン管理用のベアリポジトリを作る。
Server

$ mkdir patternables.git
$ cd patternables.git
$ git init --bare
Step2

次に、ローカルPCで上のgitフォルダをクローンする。
Client

$ git clone ssh://[email protected]:/home/ec2-user/path/to/patternables.git

もしpermissionが無いと言われた場合は以下。

$ ssh-agent bash -c 'ssh-add /path/to/pem_file; git clone ssh://[email protected]:/home/ec2-user/path/to/patternables.git'

これで作業ディレクトリができるのでここにコードを置いたり更新したりしていく。

Step3

ローカルの作業ディレクトリの更新が終わったら、リモートにpushする。
Client

$ git add .
$ git commit -m "message"
$ git push origin master
Step4

この段階でリモート上にはベアリポジトリ(作業ディレクトリ無し、更新ログのみ)しかないので、webサーバとして動かす為に実際のコードをクローンする。
Server

$ git clone patternables.git

これで最新のコードがEC2上に上がったことになる。

Step5

あとはサーバをRunするだけ。npm installなど必要な処理をした上で、以下のようにサーバを動かす。
Server

$ cd  
$ sudo service nginx start
$ cd patternables
$ forever start ./bin/www 

詳しくはここ

その後の更新: Client側でコードを更新した場合

Client

$ git add .
$ git commit -m "message"
$ git push origin master

Server

$ cd patternables
$ git pull origin master
その後の更新: サイト上でデータベースが更新された場合

Server

$ cd patternables
$ git add .
$ git commit -m "message"
$ git push origin master

Client

$ git pull origin master
諸々クライアント側からの操作:permissionが無いと言われた場合
$ ssh-agent bash -c 'ssh-add /path/to/pem_file; git push origin master'

$ ssh-agent bash -c 'ssh-add /path/to/pem_file; git clone ssh://[email protected]:/home/ec2-user/path/to/patternables.git'

$ ssh-agent bash -c 'ssh-add /path/to/pem_file; git pull origin master'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment