LoginSignup
2
3

More than 5 years have passed since last update.

SSH接続があればGitでデプロイできる

Last updated at Posted at 2017-03-21

前提

ここでいうサーバーとはAWS EC2インスタンス (Amazon Linux) のこととする

手順

1. サーバー側にGitをインストールする

$ sudo yum install -y git

2. サーバー側にGitリポジトリを作成する

$ mkdir -p /home/ec2-user/git-repo/my-git-repo.git
$ cd /home/ec2-user/git-repo/my-git-repo.git
$ git init --bare

3. サーバー側にPost-receive Hookを追加する

Gitリポジトリがコードを受け取った後、Webサーバーのドキュメントディレクトリにプッシュさせる必要がある

$ cd /home/ec2-user/git-repo/my-git-repo.git

$ cat > hooks/post-receive
#!/bin/bash
GIT_WORK_TREE=/var/www/html/public
export GIT_WORK_TREE
git checkout -f

$ chmod +x hooks/post-receive

4. ローカル側でSSH接続を設定する

ec2-serverという名前で接続できるようにする

~/.ssh/config
Host ec2-server
Hostname xx.xx.xx.xx
User ec2-user
IdentityFile ~/.ssh/secret-key.pem

5. ローカル側でSSH接続をテストする

$ ssh ec2-server  #これで接続できればオーケー

6. ローカル側でGitリポジトリを追加する

すでに~/my-projectというディレクトリで開発が進行中とする
ec2という名前のリモートリポジトリを登録する

$ cd ~/my-project
$ git remote add ec2 ec2-server:/home/ec2-user/git-repo/my-git-repo.git

7. ローカル側からサーバーへデプロイする

$ git push ec2 +master:refs/heads/master  #初回デプロイ
$ git push ec2 master #2回目以降

参照

Jeff Hoefs » Setup git deploy for AWS ec2 Ubuntu instance

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3