LoginSignup
15
15

More than 5 years have passed since last update.

Gitの`hook`で自動デプロイ(heteml編)

Last updated at Posted at 2015-01-24

herokuみたいにgit pushすると自動的にデプロイされるみたいなのできないかなと思ってやってみた。

0. 下準備

コントロールパネルからSSHを有効にしとくのと、公開鍵設定しとく。
参考: hetemlで公開鍵を使う方法

ざっと仕組み説明

リポジトリは3つ。
A. ローカルリポジトリ(作業用)
B. リモートリポジトリ(管理用)
C. リモートリポジトリ(公開用)

流れは、こんなかんじ。
1. ローカルの作業内容をAにcommit
2. Aの内容をBへpush
3. BのhookでBの内容をCがpull

本題。

1. リモートリポジトリの設定

hetemlにSSHログインした状態から説明。

$ cd ~
$ mkdir repos
$ ls
apps  repos  web

ここで、
repos ... この中にリポジトリがいろいろ入る
web ... 公開ディレクトリ(ドキュメントルート)

reposの名前はなんでもいいんだけど慣例っぽい。

で、早速リポジトリを作成。

$ cd repos
$ mkdir hoge.git
$ cd hoge.git
$ git init

その次にhookを設定するためにpost-receiveを編集。

$ vi hook/post-receive

hetemlなら、

#!/bin/sh

cd ~/web
git --git-dir=.git pull

実行権限をつけとく。

$ chmod +x post-receive

最後に、公開用のリポジトリを作成。
ドキュメントルートにぶちまける。

$ cd ~/web
$ git clone ~/repos/hoge.git
$ mv hoge.git/* ./
$ mv hoge.git/.??* ./
$ rm -r hoge.git

サーバーの設定は以上。

2. ローカルリポジトリの作成

作業ディレクトリに移動して、git initしてリモートリポジトリを追加。

$ git init
$ git remote add origin ssh://YOU@sshXXX.heteml.jp:2222/~/repos/hoge.git

おまけ:同時にbitbucketにもpushする

hookのスクリプトのとこでいろいろできるので、ついでにgithubやbitbucketにpushしとくなんてこともできる。

$ cd ~/web
$ git remote add bitbucket git@bitbucket.org:YOU/hoge.git 

originにするとすでにあるよって怒られるのでbitbucketなどてきとうに。

#!/bin/sh

cd ~/web
git --git-dir=.git pull
git --git-dir=.git push bitbucket master

成功するとこんなかんじ。


$ git push origin master
-----------------------------------------------------
--***-------------------------------------------***--
--***---------------***-------------------------***--
--***---------------******----------------------***--
--********-********-******-********-***********-***--
--********-***--***-***----***--***-***********-***--
--***--***-********-***----********-***-***-***-***--
--***--***-***------******-***------***-***-***-***--
--***--***-********-******-********-***-***-***-***--
-----------------------------------------------------


Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 650 bytes | 0 bytes/s, done.
Total 5 (delta 1), reused 0 (delta 0)
remote: From /home/sites/heteml/users/Y/O/U/YOU/web/../repos/hoge.git
remote:    e7c36f5..c870f24  master     -> origin/master
remote: Updating e7c36f5..c870f24
remote: Fast-forward
remote:  .DS_Store  | Bin 0 -> 6148 bytes
remote:  .swp       | Bin 0 -> 12288 bytes
remote:  index.html |   2 +-
remote:  3 files changed, 1 insertion(+), 1 deletion(-)
remote:  create mode 100644 .DS_Store
remote:  create mode 100644 .swp
remote: To git@bitbucket.org:YOU/hoge.git
remote:  * [new branch]      master -> master
To ssh://YOU@sshXXX.heteml.jp:2222/~/repos/hoge.git
   e7c36f5..c870f24  master -> master
15
15
1

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
15
15