運用ファイルを途中からgitで管理したい

Web制作

カラフルボックスサーバーを使用しています。運用ファイルをgitで管理しようと思い、カラフルボックスで設定してみました。githubは使用せず、カラフルボックスとローカルマシンでの環境です。
(*ssh接続ができて、sshのconfigファイルに設定を記述しています)

▼管理したいリポジトリ(WordPressのテーマファイル)
/home/アカウント名/ディレクトリパス/cocoon-child-master

▼bareリポジトリ
/home/アカウント名/git/tsuzu9jp/cocoon-child-master.git

(サーバー側)git管理したいディレクトリ

管理したいディレクトリをgitのリポジトリにする

sshでサーバーに接続した後、管理したいディレクトリに移動して、git initでリポジトリにします。

$ cd 管理したいディレクトリに移動
$ git init
$ git add -A
$ git commit -m "first commit"

globalの設定を尋ねられました。

Author identity unknown
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.

なので、メールアドレスと名前を登録して、でメールアドレスと名前を登録してもう一度コミットしました。

$ git config --global user.email "メールアドレス"
$ git config --global user.name "名前"
$ git commit -m "first commit"

コミットしたら、後でパスを使うのでpwdでパスを調べておきます。

$ pwd

/home/アカウント名/ディレクトリパス/cocoon-child-master

(サーバー側)bareリポジトリ

リポジトリ用にフォルダを作成

カラフルボックにbareリポジトリを作成します。
アカウント直下のウェブに公開していない部分にディレクトリを作成して、bareリポジトリを配置しました。(/home/アカウント名/git/tsuzu9jp/ここにbareリポジトリを作成イメージ)

$ cd ~
$ mkdir git
$ cd git
$ mkdir tsuzu9jp
$ cd tsuzu9jp

bareリポジトリをcloneで準備

管理したいgitリポジトリにしたパスと、リポジトリ名.git(ベアリポジトリ)を使用してbareリポジトリを作成します。(ベアリポジトリには「.git」を用いるようです)

$ git clone --bare /home/アカウント名/ディレクトリパス/cocoon-child-master cocoon-child-master.git
Cloning into bare repository 'cocoon-child-master.git'…
done.

bareリポジトリのhooksのディレクトリにpost-reciveを作成

ローカルからpushしたときに自動でデプロイできるように、cocoon-child-master.gitのhooksフォルダの中にpost-receiveファイルを作成します。

ファイル: cocoon-child-master.git/hooks/post-receiveを作成
$ cd cocoon-child-master.git
$ vi hooks/post-receive


viはコマンドモードと入力モードがあるので、「i」で挿入モードにして、以下を入力します。
管理したいリポジトリに移動して、ベアリポジトリからpullしてくるという意味です。

!/bin/bash

cd /home/アカウント名/フォルダパス/cocoon-child-master
git --git-dir=.git pull origin master

「ESC」をタイプして挿入モードを解除して、コマンドモードに切り替えて、保存とファイルを閉じます。

キーボードのEscをおしてiの挿入モードを抜ける
:w…ファイル保存
:q…ファイルを閉じる
(:wqでも大丈夫)

post-reciveファイルのパーミッションを変更する

作成したpost-reciveファイルに実行権限を与えます。

$ chmod 755 hooks/post-receive

filezillaなどのソフトでパーミッションを755でも大丈夫だと思います。

bareリポジトリのディレクトリのパスもここで確認しておきます。

$ pwd
/home/アカウント名/git/tsuzu9jp/cocoon-child-master.git

(サーバー側)管理したいgitのディレクトリ

リモートリポジトリを追加

さきほど作成した管理したいリポジトリに戻り、リモートリポジトリを追加します。

$ cd /home/アカウント名/ディレクトリパス/cocoon-child-master
$ git remote add origin ~/git/tsuzu9jp/cocoon-child-master.git
$ git pull origin master
From /home/vogrigvx/git/tsuzu9jp/cocoon-child-master
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
Already up to date.

ローカルマシンでcloneを実行するので、サーバーからはログアウトします。

$ exit

(ローカル)

ディレクトリを準備

ローカル側で管理したいディレクトリを準備して移動します。

$ mkdir site
$ cd site
$ mkdir tsuzu9.jp
$ cd tsuzu9.jp

リポジトリをcloneする

ローカルマシンでbareリポジトリのパスを使用してcloneをします。

$ git clone cbox:/home/アカウント名/git/tsuzu9jp/cocoon-child-master.git

(*)cboxはローカルのsshのconfigファイルで設定している名前です。

pushして自動デプロイ

テストファイルを作成して、自動でデプロイできているか確認します。

$ cd クローンしたディレクトリに移動
$ touch test_file
$ git add -A
$ git commit -m "test push"
$ git push

デプロイを確認する

アップしたファイルをサーバーにデプロイできているか確認します。sshで接続し直して、管理したいディレクトリに移動し、lsコマンドでファイルを確認します。

$ ssh サーバーにアクセス
$ cd /home/アカウント名/ディレクトリパス/cocoon-child-master
$ ls -la

ファイルがアップできているか確認します。
または、ブラウザでパスのURLを確認するか、FTPソフト等で確認します。

コメント

タイトルとURLをコピーしました