サーバー環境

git push でデプロイ自動化 およびpost-receive-email の実験

GitHubは、エンジニアにとっては当たり前のサービスですが、CIツールで自動化も敷居が高いものです。 ここでは Gitの一連の流れ(commit > pushu > pull)を検証すべく 独自サーバー上にレポジトリを作成し、サーバー側でフックしてpost-script-mail および pullの自動化を検証してみることにします。

  1. sshの設定
  2. サーバー上に独自レポジトリーを作成
  3. [独自レポジトリ.git]/hooks/post-receive-emailの内容を検証
  4. 本番サイトへのpullの検証

sshの設定

ローカルホスト側での設定
$ vi ~/.ssh/config
HOST [git名] // 例) sample_git_host Hostname gitserver.sample.com Port 22 user sampleuser IdentityFile /path/to/xxxx.key
$ chmod 600 .ssh/config
鍵ペアーの作成およびリモートホストに公開鍵をコピーする
$ ssh-keygen -t rsa // キーペアの作成 // ssh-copy-id, scp 等を利用してリモートホストに公開鍵をコピーする // ex. ssh-copy-id -i /path/to/keyfile.pub user@host1
sshのログインに鍵による認証を行うように/etc/ssh/sshd_configを編集

注:設定するとキー入力以外ではログインできなくなるので 確実に鍵方式でログインを確認後に設定する。初めから認証鍵のみで提供のプロバイダーもあります。その場合は無視してください。

$ sudo vi /etc/ssh/sshd_config
PasswordAuthentication no // ←鍵方式のみログインを許可 // (必ずしも設定する必要はない)
$ systemctl restart sshd // 設定完了後,SSH を再起動します。
リモートリポジトリの登録
$ git remote add origin sample.git:template.git $ git remote -v
origin codingsock:codingstock.git (fetch) origin codingsock:codingstock.git (push)

サーバー上に独自レポジトリーを作成

// ——- リモートリポジトリホルダー作成 ————- $ mkdir sample.git $ cd sample.git // ——- リモートリポジトリとして git初期化 $ git init –bare
Initialized empty Git repository in /path/to/sample.git/
// —— 作成された .git を確認 —————— $ cd .git $ ls -al
合計 20 drwxr-xr-x 7 codingstock apache 111 5月 10 03:45 . drwxr-x— 18 codingstock apache 4096 5月 10 03:40 .. -rw-r–r– 1 codingstock apache 23 5月 10 03:45 HEAD drwxr-xr-x 2 codingstock apache 6 5月 10 03:45 branches -rw-r–r– 1 codingstock apache 66 5月 10 03:45 config -rw-r–r– 1 codingstock apache 73 5月 10 03:45 description drwxr-xr-x 2 codingstock apache 4096 5月 10 03:45 hooks drwxr-xr-x 2 codingstock apache 20 5月 10 03:45 info drwxr-xr-x 4 codingstock apache 28 5月 10 03:45 objects drwxr-xr-x 4 codingstock apache 29 5月 10 03:45 refs

デフォルトのpost-receive-email確認およびシンボリックリンク

// —— 実行権限確認 ———– $ ls -al /usr/share/git-core/contrib/hooks/post-receive-email
-rwxr-xr-x 1 root root 21630 5月 14 07:33 /usr/share/git-core/contrib/hooks/post-receive-email
// —— hooks/post-receive としてシンボリックリンク ———– $ cd /path/to/sample.git/hooks $ ln -s /usr/share/git-core/contrib/hooks/post-receive-email ./post-receive

git configにパラメータ設定

hooks.mailinglistpush 時の送信先メールアドレス
hooks. announcelistgit tag 作成時の送信先メールアドレス
hooks.envelopesender 送信元メールアドレス
hooks.emailprefixメールの件名の先頭につく文字列(default は [SCM])
hooks.showrevメールにcommit差分(diff)を追加
git config に登録
$ git config –global hooks.mailinglist ‘gituser@sample.com’ $ git config –global add hooks.envelopesender ‘gituser@sample.com’ $ git config –global add hooks.emailprefix ‘[MyProject]’ $ git config –global hooks.showrev “git show -C %s; echo” $ git config -l
hooks.mailinglist=gituser@sample.com hooks.envelopesender=gituser@sample.com hooks.emailprefix=[MyProject] hooks.showrev=git show -C %s; echo

Configの内容(原文 ソースコードより)
# Config # —— # hooks.mailinglist # This is the list that all pushes will go to; leave it blank to not send # emails for every ref update. # hooks.announcelist # This is the list that all pushes of annotated tags will go to. Leave it # blank to default to the mailinglist field. The announce emails lists # the short log summary of the changes since the last annotated tag. # hooks.envelopesender # If set then the -f option is passed to sendmail to allow the envelope # sender address to be set # hooks.emailprefix # All emails have their subjects prefixed with this prefix, or “[SCM]” # if emailprefix is unset, to aid filtering # hooks.showrev # The shell command used to format each revision in the email, with # “%s” replaced with the commit id. Defaults to “git rev-list -1 # –pretty %s”, displaying the commit id, author, date and log # message. To list full patches separated by a blank line, you # could set this to “git show -C %s; echo”. # To list a gitweb/cgit URL *and* a full patch for each change set, use this: # “t=%s; printf ‘http://…/?id=%%s’ \$t; echo;echo; git show -C \$t; echo” # Be careful if “…” contains things that will be expanded by shell “eval” # or printf. # # Notes # —– # All emails include the headers “X-Git-Refname”, “X-Git-Oldrev”, # “X-Git-Newrev”, and “X-Git-Reftype” to enable fine tuned filtering and # give information for debugging.

自動的に本番サイトに git pull

post-receive-email の main部分最終行に git pull を追加
$ vi /usr/share/git-core/contrib/hooks/post-receive-email
cd /var/www/html/codingstock/template git --git-dir=.git pull

以上で ローカルの開発環境で push するとメールが送信されデプロイ(git pullにて)されます。

$ git status $ git add . $ git commit $ git origin master

実際のpushテストで届いたメールの一部分
git log や diff も記載されています。

実際の運用には定番の「Jenkins」等のCIツールを利用すると思いますが、今回はあくまでも hooksの検証とします。

最近の記事

  1. wordpres5をubuntu nginxにインストール
  2. ubuntu 20.04 nginx php mysql Webサーバー環境整備
  3. Ubuntu nginx SSL
  4. EC-CUBE 4.0 インストール
  1. ubuntu 20.04 nginx php mysql Webサーバー環境整備

    サーバー環境

    ubuntu 20.04 nginx php mysql Webサーバー環境…
  2. サーバー環境

    Aws EC2 Apache + Mysql + php7 構築の記録
  3. Web・デザイン

    JavaScriptのアニメーションライブラリ AniJS
  4. サーバー環境

    無料のSSL証明書 Let’s Encrypt 試してみました。
  5. プログラミング

    郵便番号データベースで多対多のリレーション
PAGE TOP