【CI】 成功

2020.02.15

自動デプロイの実行 (git push production master)

(注意)

/public
/storage
などを.gitignoreに含めて、更新を消してしまわないように運用に配慮。  

hotfixにpush

[参考資料] https://qiita.com/zaburo/items/8886be1a733aaf581045

リポジトリ構成

[リモートPC] ~/git_vps_sns => bareリポジトリを作成

(1) bareリポジトリを作成 (~/git_vps_sns)

[リモート側PC]
$ cd ~
$ mkdir git_vps_sns
$ cd git_vps_sns
$ git init --bare

リモートリポジトリを登録するには、

https://qiita.com/onigra/items/326ba157d5552f0076be

[ローカルPC]


$ git remote add production ssh://[configのHostに記述した名前]/home/[VPSユーザ名]/[プロジェクト名]
    (* 注意: プロジェクト名の拡張子に.gitを付けると、ファイル不一致が起こるので、付けないこと。)
$ git push production master
[ローカルPC]
$ git remote add production ssh://sakura-vps/home/(VPSユーザ名)/git_vps_sns
$ git push production master

git remote -vで確認

[ローカルPC]

(Macユーザ名)noMacBook-puro:(ディレクトリ名) (ユーザ名)$ git remote -v

origin  git@github.com:[Githubアカウント名]/modify-laravel.git (fetch)
origin  git@github.com:[Githubアカウント名]/modify-laravel.git (push)
production  ssh://[(~/.ssh/configで指定した)Host項目]/home/(VPSユーザ名)/git_vps_sns (fetch)
production  ssh://[(~/.ssh/configで指定した)Host項目]/home/(VPSユーザ名)/git_vps_sns (push)

(Macユーザ名)noMacBook-puro:(ディレクトリ名) (ユーザ名)$ 

鍵認証を求められずに完了

(Macユーザ名)noMacBook-puro:(ディレクトリ名) (ユーザ名)$ git push production master

Counting objects: 10505, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (7637/7637), done.
Writing objects: 100% (10505/10505), 28.66 MiB | 10.85 MiB/s, done.
Total 10505 (delta 3651), reused 8616 (delta 2492)
To ssh://[(~/.ssh/configで指定した)Host項目]/home/[VPSユーザ名]/git_vps_sns
 * [new branch]      master -> master

(Macユーザ名)noMacBook-puro:(ディレクトリ名) (ユーザ名)$

cloneまでできた

[ユーザー名@(VPSホスト名) ~]$ ls -l
合計 28
drwxrwxr-x  2 (VPSユーザ名) (VPSユーザ名) 4096  2月 15 13:25 git_bare_sns
drwxrwxr-x  3 (VPSユーザ名) (VPSユーザ名) 4096  2月 15 13:28 git_produ_sns
drwxrwxr-x  7 (VPSユーザ名) (VPSユーザ名) 4096  2月 15 13:37 git_vps_sns
drwxr-xr-x 17 (VPSユーザ名) (VPSユーザ名) 4096  2月 12 09:29 modify_sns
drwxr-xr-x 14 (VPSユーザ名) (VPSユーザ名) 4096 12月 26 14:53 modify_sns20191226
drwxr-xr-x 15 (VPSユーザ名) (VPSユーザ名) 4096  1月 20 11:39 modify_sns20200122
drwxr-xr-x 13 (VPSユーザ名) (VPSユーザ名) 4096 12月  9 13:30 tweet-1st
[(VPSユーザ名)@(VPSホスト名) ~]$ pwd
/home/(VPSユーザ名)
[(VPSユーザ名)@(VPSホスト名) ~]$ git clone /home/(VPSユーザ名)/git_vps_sns test
Cloning into 'test'...
done.
[(VPSユーザ名)@(VPSホスト名) ~]$ ls -l
合計 32
drwxrwxr-x  2 (VPSユーザ名) (VPSユーザ名) 4096  2月 15 13:25 git_bare_sns
drwxrwxr-x  3 (VPSユーザ名) (VPSユーザ名) 4096  2月 15 13:28 git_produ_sns
drwxrwxr-x  7 (VPSユーザ名) (VPSユーザ名) 4096  2月 15 13:37 git_vps_sns
drwxr-xr-x 17 (VPSユーザ名) (VPSユーザ名) 4096  2月 12 09:29 modify_sns
drwxr-xr-x 14 (VPSユーザ名) (VPSユーザ名) 4096 12月 26 14:53 modify_sns20191226
drwxr-xr-x 15 (VPSユーザ名) (VPSユーザ名) 4096  1月 20 11:39 modify_sns20200122
drwxrwxr-x 18 (VPSユーザ名) (VPSユーザ名) 4096  2月 15 13:56 test
drwxr-xr-x 13 (VPSユーザ名) (VPSユーザ名) 4096 12月  9 13:30 tweet-1st
[(VPSユーザ名)@(VPSホスト名) ~]$ cd test

第二章

bareリポジトリの中身を見に行く。

bareリポジトリ内部にあるhooksに、トリガーを追加

[(VPSユーザ名)@(VPSホスト名) git_vps_sns]$ ls -l
合計 32
-rw-rw-r-- 1 (VPSユーザ名) (VPSユーザ名)   23  2月 15 13:37 HEAD
drwxrwxr-x 2 (VPSユーザ名) (VPSユーザ名) 4096  2月 15 13:37 branches
-rw-rw-r-- 1 (VPSユーザ名) (VPSユーザ名)   66  2月 15 13:37 config
-rw-rw-r-- 1 (VPSユーザ名) (VPSユーザ名)   73  2月 15 13:37 description
drwxrwxr-x 2 (VPSユーザ名) (VPSユーザ名) 4096  2月 15 13:37 hooks
drwxrwxr-x 2 (VPSユーザ名) (VPSユーザ名) 4096  2月 15 13:37 info
drwxrwxr-x 4 (VPSユーザ名) (VPSユーザ名) 4096  2月 15 13:37 objects
drwxrwxr-x 4 (VPSユーザ名) (VPSユーザ名) 4096  2月 15 13:37 refs


[(VPSユーザ名)@(VPSホスト名) git_vps_sns]$ pwd
/home/(VPSユーザ名)/git_vps_sns


[(VPSユーザ名)@(VPSホスト名) git_vps_sns]$ cd hooks
[(VPSユーザ名)@(VPSホスト名) hooks]$ ls -l
合計 40
-rwxrwxr-x 1 (VPSユーザ名) (VPSユーザ名)  452  2月 15 13:37 applypatch-msg.sample
-rwxrwxr-x 1 (VPSユーザ名) (VPSユーザ名)  896  2月 15 13:37 commit-msg.sample
-rwxrwxr-x 1 (VPSユーザ名) (VPSユーザ名)  189  2月 15 13:37 post-update.sample
-rwxrwxr-x 1 (VPSユーザ名) (VPSユーザ名)  398  2月 15 13:37 pre-applypatch.sample
-rwxrwxr-x 1 (VPSユーザ名) (VPSユーザ名) 1704  2月 15 13:37 pre-commit.sample
-rw-rw-r-- 1 (VPSユーザ名) (VPSユーザ名) 1348  2月 15 13:37 pre-push.sample
-rwxrwxr-x 1 (VPSユーザ名) (VPSユーザ名) 4951  2月 15 13:37 pre-rebase.sample
-rwxrwxr-x 1 (VPSユーザ名) (VPSユーザ名) 1239  2月 15 13:37 prepare-commit-msg.sample
-rwxrwxr-x 1 (VPSユーザ名) (VPSユーザ名) 3611  2月 15 13:37 update.sample
[(VPSユーザ名)@(VPSホスト名) hooks]$ 

bareじゃない方、実体の方を見に行く。

[参照] https://qiita.com/zaburo/items/8886be1a733aaf581045

[ファイル名] 
#!/bin/sh

cd /home/(VPSユーザ名)/[bareじゃない方]
git --git-dir=.git pull

bareの方を見に行く。

hooks]$ ls -l | grep post-receive
-rw-rw-r-- 1 (VPSユーザ名) (VPSユーザ名)   62  2月 15 14:19 post-receive

hooks]$ chmod a+x post-receive
hooks]$ ls -l | grep post-receive
-rwxrwxr-x 1 (VPSユーザ名) (VPSユーザ名)   62  2月 15 14:19 post-receive

hooks]$ ./post-receive
Already up-to-date.

成功