Memo

メモ > サーバ > 各論: Git > GitHub

■GitHub
GitHub https://github.com/ GitHub Status https://www.githubstatus.com/ 一番メジャーだと思われる。オープンソースで公開する場合に最適。非公開リポジトリを作りたい場合は有料 …だったが、2020年4月15日現在、非公開リポジトリもチーム利用も無料になった GitHub、チームでの利用も無料に。プライベートリポジトリ数も制限なく、チームディスカッション、ActionsによるCI/CDも可能 − Publickey https://www.publickey1.jp/blog/20/githubactionscicd.html 新規に立てたgithubリポジトリからソースをcloneする http://qiita.com/monhan/items/b467e5f58ebcbc5f5b3c GitHubでリポジトリを作ってSSHでgit cloneするまで https://cloudpack.media/416 githubから、Action Required - SSH Key Vulnerabilityなるメールが来た http://blog.bornneet.com/Entry/328/ gitでシンプルなデプロイ環境を作る http://qiita.com/zaburo/items/8886be1a733aaf581045 githubのwebhookを使って、push時に自動でサーバーを更新させる http://blog.manaten.net/entry/573 ■インストール(古いバージョンでダメなら試す)
# yum list installed | grep git.x86_64 … gitのインストールを確認 # yum remove git … 古いものがあれば削除 # yum install git … gitをインストール
■gitの動作確認
# git --version … インストールを確認
■Apacheへログイン(nginxの場合は、以降も含めてnginx用のユーザを使う)
# passwd apache … apacheにパスワードを設定(rootを経由するなら不要) # su -s /bin/bash - apache … シェルを指定してログイン
■デプロイ環境を構築 ※apacheユーザで作業 ※/var/www 内全体をデプロイ対象にすると、 「/var/www/.ssh/ などが存在するのでデプロイできないが、リポジトリへアクセスするためには /var/www/.ssh/ が必要」 となって作業が難しいので注意。他の場所にすることを推奨 サイト全体をデプロイ対象にするなら、バーチャルホストを設定するのが無難
# mkdir /var/www/html/test … デプロイ先を作成 # chmod 775 /var/www/html/test/ … apacheグループのユーザに読み書き実行を許可 # ll /var/www/html/ … デプロイ先の所有者を確認 $ cd /var/www/html/test … デプロイ先に移動 $ git clone git@github.com:refirio/test.git /var/www/html/test … クローン実行 Initialized empty Git repository in /var/www/html/test/.git/ The authenticity of host 'github.com (192.30.252.128)' can't be established. RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'github.com,192.30.252.128' (RSA) to the list of known hosts. Permission denied (publickey). … Permissionで弾かれている(鍵の登録が必要) fatal: The remote end hung up unexpectedly
■鍵 ※apacheユーザで作業
$ cd … apacheのホームディレクトリで作業 $ mkdir /var/www/.ssh $ chown -R apache:apache /var/www/.ssh/ $ ssh-keygen -t rsa … gitへSSH接続するための鍵を作成 Generating public/private rsa key pair. Enter file in which to save the key (/var/www/.ssh/id_rsa): … 空Enter(デフォルトの場所に作成) Enter passphrase (empty for no passphrase): … 空Enter(パスワード無しの鍵を作成) Enter same passphrase again: … 空Enter Your identification has been saved in /var/www/.ssh/id_rsa. Your public key has been saved in /var/www/.ssh/id_rsa.pub. The key fingerprint is: 42:b3:c9:c3:f9:85:e4:be:9a:3e:28:82:cf:48:83:a9 apache@refirio.net The key's randomart image is: +--[ RSA 2048]----+ | | | | | o . | | + B . | | O S . | |.. = . | |+o . o | |=oo . .. . | |Eoo. .+o. | +-----------------+ $ ll /var/www/.ssh/ … 鍵(id_rsa と id_rsa.pub)の作成を確認 合計 8 -rw------- 1 apache apache 1675 10月 18 12:13 2015 id_rsa -rw-r--r-- 1 apache apache 400 10月 18 12:13 2015 id_rsa.pub -rw-r--r-- 1 apache apache 407 10月 18 12:19 2015 known_hosts $ vi /var/www/.ssh/id_rsa … 秘密鍵の内容を確認
-----BEGIN RSA PRIVATE KEY----- MIIEogIBAAKCAQEA7QltpgTVx5XNpS3sA/qatl/NlFHxGHR55cnZw2zsGpkqadVN 〜略〜 ePtIQN8d07QbxJ/RLzzLfkgs6w0tezCv40tXtCSD7LdeJTaRwIg= -----END RSA PRIVATE KEY-----
$ vi /var/www/.ssh/id_rsa.pub … 公開鍵の内容を確認(アカウントとドメインが記入されているのを確認)
ssh-rsa AAAA〜略〜3PQ== apache@refirio.net
$ vi /var/www/.ssh/known_hosts … gitへのログインを試みたので、known_hostsとして公開鍵が登録されている
github.com,192.30.252.128 ssh-rsa AAAA〜略〜AaQ==
■Protectを解除(この手順は不要かも?)
$ ssh-keygen -lf ~/.ssh/id_rsa.pub … フィンガープリントを確認 2048 b4:dd:17:〜略〜:6e:fd:a4 /root/.ssh/id_rsa.pub (RSA)
https://github.com/settings/ssh/audit からフィンガープリントを登録 …と思ったけど、https://github.com/settings/ssh/audit でapproveボタンを押せばOKだった。 未承認のアクセスがあったというだけ? ■鍵を登録 https://github.com/settings/ssh もしくは https://github.com/ユーザ名/リポジトリ名/settings/keys から「refirio.net」や「staging」などタイトルとともに公開鍵を登録 パスワードはGitHubへのログインパスワードを登録 ■動作確認 ※apacheユーザで作業
$ cd /var/www/html/test … 作業用ディレクトリへ移動 $ git clone git@github.com:refirio/test.git /var/www/html/test … クローン実行 Initialized empty Git repository in /var/www/html/test/.git/ remote: Counting objects: 146, done. remote: Compressing objects: 100% (25/25), done. remote: Total 146 (delta 10), reused 0 (delta 0), pack-reused 121 Receiving objects: 100% (146/146), 18.03 KiB, done. Resolving deltas: 100% (74/74), done. $ ll … 確認 合計 28 -rw-r--r-- 1 apache apache 23 10月 18 12:24 2015 README.md drwxr-xr-x 2 apache apache 4096 10月 18 12:24 2015 css -rw-r--r-- 1 apache apache 615 10月 18 12:24 2015 index.html -rw-r--r-- 1 apache apache 370 10月 18 12:24 2015 page1.html -rw-r--r-- 1 apache apache 343 10月 18 12:24 2015 page2.html -rw-r--r-- 1 apache apache 343 10月 18 12:24 2015 page3.html -rw-r--r-- 1 apache apache 384 10月 18 12:24 2015 test.php $ git pull … プルする場合 Already up-to-date. $ git pull … gitのmasterを更新してから再度プルした場合 remote: Counting objects: 5, done. remote: Compressing objects: 100% (3/3), done. remote: Total 5 (delta 3), reused 2 (delta 2), pack-reused 0 Unpacking objects: 100% (5/5), done. From github.com:refirio/test d3c714b..e12a45e develop -> origin/develop f555d5f..5915266 master -> origin/master * [new branch] text -> origin/text Updating f555d5f..5915266 Fast-forward index.html | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-) $ ll … 確認 合計 28 -rw-r--r-- 1 root root 23 10月 12 14:16 2015 README.md drwxr-xr-x 2 root root 4096 10月 12 14:16 2015 css -rw-r--r-- 1 root root 514 10月 12 14:29 2015 index.html … ファイルが更新された -rw-r--r-- 1 root root 343 10月 12 14:16 2015 page1.html -rw-r--r-- 1 root root 343 10月 12 14:16 2015 page2.html -rw-r--r-- 1 root root 343 10月 12 14:16 2015 page3.html -rw-r--r-- 1 root root 343 10月 12 14:16 2015 page4.html
■デプロイ用スクリプト作成 ※apacheユーザで作業
$ mkdir /var/www/html/test-deploy … デプロイツール用ディレクトリを作成 $ chmod 775 /var/www/html/test-deploy/ … apacheグループのユーザに読み書き実行を許可 $ cd /var/www/html/test-deploy/ … 作業用ディレクトリへ移動 $ vi deploy.sh … デプロイ用スクリプトを作成
#!/bin/bash cd /var/www/html/test; git pull;
$ chmod 744 deploy.sh … デプロイ用スクリプトに実行権限を付加 $ /var/www/html/test-deploy/deploy.sh … 実行テスト Already up-to-date. $ vi /var/www/html/test-deploy/index.php … ./deploy.sh を実行するPHPプログラムを作成
<?php if ($fp = fopen('logs/' . date('YmdHis') . '.log', 'a')) { fwrite($fp, print_r($_POST['payload'], true)); fwrite($fp, "\n"); fwrite($fp, $_SERVER['HTTP_USER_AGENT']); fclose($fp); } else { exit('NG'); } $payload = $_POST['payload']; $payload = json_decode($payload, true); if ($payload['ref'] == 'refs/heads/master') { echo shell_exec('/var/www/html/test-deploy/deploy.sh'); }
ブラウザから index.php にアクセスして、デプロイできることを確認する ■更新を自動通知 https://github.com/refirio/test/settings/hooks Payload URL: http://refirio.net/test-deploy/ Content type: application/json Which events would you like to trigger this webhook?: Send me everything. everythingにしておかないと、マージのタイミングで知らせてくれない? もしくは「Let me select individual events.」で、任意のタイミングで知らせてもらうのが良さそう ■GitHub Actions ※未検証 GitHubでCIのような仕組みを構築できる 自由度は高いようだが、規約で禁止されている使い方もあるようなので注意 プランによってジョブの数などに制限はある GitHubの新機能「GitHub Actions」で試すCI/CD | さくらのナレッジ https://knowledge.sakura.ad.jp/23478/ ■続き パーミッションをどうするか 開発環境がMacなら、パーミッションも含めてコミットされる? シェルスクリプトに「特定箇所にあるディレクトリのパーミッションを自動設定する」コードを追加する? …と思ったけど、Apache権限でデプロイするので、パーミッションは問題にならなさそう

Advertisement