2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

git pushしたら自動的に本番環境でgit pull されるWordPress(KUSANAGI)の共同開発環境

Last updated at Posted at 2018-11-26

はじめに

過去にWordPressサイトを共同開発することがありました。バージョン管理はをgithubでやっていましたが、本番への反映が面倒だったので自動化したお話

これまではgithubにソース(テーマファイル)をgit pushしたあとに
サーバのストレージをMacにsshfsコマンドを使いマウントしてからgit pullするという面倒なことをやっていた

環境

  • Mac OS MAMPにローカルサーバを立ててテストをしていた
  • 本番環境へのサーバへSSHログイン可能
  • 本番環境はKUSANAGIでラクラク構築

実装

サーバ側の作業1

ログインユーザはkusanagi
リポジトリに使うディレクトリを作成

$ mkdir /var/git_service/wp_dev/hogehogehuga.com.git/

リポジトリに移動

$ cd /var/git_service/wp_dev/hogehogehuga.com.git/

Macでの作業

$ cd /Applications/MAMP/htdocs/wordpress/wp-content/themes/hogehogehuga.com/
$ git init
$ git add .
$ git commit -m '1st'

.gitの設定ファイルを変更

その一番下の行に kusanagi@hogehogehuga.com:/var/git_service/wp_dev/hogehogehuga.com.git を追加

$ vi .git/config
.git/config
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = git@github.com:merarli/wp_hogehogehuga.com.git
fetch = +refs/heads/*:refs/remotes/origin/*
url = kusanagi@hogehogehuga.com:/var/git_service/wp_dev/hogehogehuga.com.git

pushしてgithubとサーバのリポジトリにファイルがあれば成功

サーバ側の作業2

サーバのリポジトリからクローンする

$ cd /home/kusanagi/hogehogehuga.com/DocumentRoot/wp-content/themes/
$ git clone /var/git_service/wp_dev/hogehogehuga.com.git

クローンしたファイルの中にデータがあるか確認

$ cd /home/kusanagi/hogehogehuga.com/DocumentRoot/wp-content/themes/hogehogehuga.com
$ ls

githubにもpushされているか確認(Macのプラウザで確認)

自動化設定

サーバのリポジトリの設定ファイルを変えるために移動

$ cd /var/git_service/wp_dev/hogehogehuga.com.git/hooks

post-update.sampleをpost-updateとしてコピー

$ cp post-update.sample post-update

設定ファイルを以下のように設定

$ vi post-update
post-update
# !/bin/sh
#
# An example hook script to prepare a packed repository for use over
# dumb transports.
#
# To enable this hook, rename this file to "post-update".

cd /home/kusanagi/hogehogehuga.com/DocumentRoot/wp-content/themes/hogehogehuga.com/
git --git-dir=.git pull origin master

実行権限を与える

$ chmod a+x post-update

実行して動けば成功

$ ./post-update
2
4
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
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?