GitHubで回すWebサイト制作

検証環境

用途

ソフトウェア名

バージョン番号

備考

OS

CentOS 7

7.7.1908

SELinuxは有効とする

Webサーバー

Apache 2.4

2.4.41

IUSリポジトリ

Webフック

webhook

2.6.10

スナップクラフト

ファイルフック

incron

0.5.12

ワークアラウンドのため使用

検証目標

ミドルウェアの初期セットアップ

自作スクリプトの設置

ディレクトリ作成

sudo mkdir -p /root/.webhook/bin
sudo mkdir -p /root/.webhook/run
sudo mkdir -p /etc/httpd/libexec
sudo restorecon -RFv /root/.webhook
sudo restorecon -RFv /etc/httpd/libexec

/root/.webhook/run/activate_id_update-contents

以下のコマンドを実行してファイルを作成する。

sudo touch /root/.webhook/run/activate_id_update-contents
sudo restorecon -Fv /root/.webhook/run/activate_id_update-contents

/root/.webhook/bin/mecho

#!/bin/sh
: APPEND=no
: OUTFILE=-

while getopts ao: OPT; do
        case $OPT in
        a)  APPEND=yes
                ;;
        o)  OUTFILE=$OPTARG
                ;;
        \?) continue
                ;;
        esac
done
shift $((OPTIND - 1))

if [ x"$OUTFILE" = x"" -o x"$OUTFILE" = x"-" ]; then
        echo $*
else
        if [ x"$APPEND" = x"yes" ]; then
                echo $* >> "$OUTFILE"
        else
                echo $* >  "$OUTFILE"
        fi
fi

上記ファイル作成後、下記コマンドを実行すること。

sudo chmod 0755     /root/.webhook/bin/mecho
sudo restorecon -Fv /root/.webhook/bin/mecho

/etc/httpd/libexec/update-contents.sh

#!/bin/sh
cd /var/www/html

git pull -q
if [ x"$1" = x"refs/heads/master" ]; then
        git checkout -qf master
        restorecon -RF .
fi

OS

基本的な(とは何かは議論しない)セットアップは実施済みであることとする。

ファイアウォール設定

sudo firewall-cmd --add-service=http  --permanent
sudo firewall-cmd --add-service=https --permanent
sudo firewall-cmd --reload

Apache

sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpm
sudo yum -y install httpd24u httpd24u-tools

/etc/httpd/conf/httpd.conf

ProxyPass /hooks/ http://localhost:9000/hooks/
<Proxy *>
    Require all granted
</Proxy>

もちろん、本設定は <Virtual ~></Virtual> 中で設定してもよい。

スナップクラフト

sudo yum -y install snapd
sudo systemctl enable --now snapd.socket
sudo ln -s /var/lib/snapd/snap /snap
sudo systemctl reboot

※スナップクラフトの有効化のために一度再起動しておくこと。

Webhook

sudo snap install webhook

※スナップクラフトが稼働してる状態で上記コマンドを実行すること。

/etc/systemd/system/snap.webhook.webhook.service.d/override.conf

sudo systemctl edit snap.webhook.webhook.service コマンドを呼び出して以下の設定を行う。

[Service]
EnvironmentFile=-/etc/sysconfig/webhook
ExecStart=
ExecStart=/usr/bin/snap run webhook $OPTIONS

※Webhook起動のための設定オプションの指定ができなかったので、/etc/sysconfig/webhook を参照するように改変してみた。

/etc/sysconfig/webhook

OPTIONS="-hooks /etc/httpd/conf/hooks.yaml -hotreload -ip 127.0.0.1 -port 9000"

/etc/httpd/conf/hooks.yaml

- id: update-contents
  command-working-directory:    "/root"
  execute-command:              "/root/.webhook/bin/mecho"
  pass-arguments-to-command:
    - source: string
      name: "-o"
    - source: string
      name: "/root/.webhook/run/activate_id_update-contents"
    - source: payload
      name: ref
  response-message: |
    Updated, Done.
  trigger-rule-mismatch-http-response-code: 400
  trigger-rule:
    and:
      - match:
          type: payload-hash-sha1
          secret: "GitHubに設定するシークレット"
          parameter:
            source: header
            name: "X-Hub-Signature"
      - match:
            type: value
            value: push
            parameter:
                source: header
                name: "X-GitHub-Event"

後始末

sudo restorecon -RFv /etc/systemd/system/snap.webhook.webhook.service.d/ /etc/sysconfig/webhook /etc/httpd/conf/webhook.yaml

InCron

sudo yum -y install epel-release
sudo yum -y install incron

sudo systemctl enable incrond
sudo systemctl start incrond

/etc/incron.d/update-contents

/root/.webhook/run/activate_id_update-contents  IN_CLOSE_WRITE,loopable=false   /etc/httpd/libexec/update-contents.sh $$(cat $@)

上記ファイル設定後、以下のコマンドを実行する。

restorecon -Fv /etc/incron.d/update-contents

デプロイ用SSH鍵の準備

sudo ssh-keygen -t ssh-ed25519 -C root@$(hostname) -N "" -f /root/.ssh/id_ed25519

以下の点に注意すること。

よくある質問とその答え

Q.ProxyPass設定したならProxyReverseの設定は必須では?

A.webhookに高度な応答(リダイレクトやらクッキーやら)機能が無いので必要ないです。

Q.ウェブフックへのアクセス制限したいのだが?

A.https://api.github.com/meta の応答(JSON)見ると接続元IPアドレスがリストアップしてますねー(棒)。

詳細はREST API v3 - Meta を参考に設定してみてください。時々変更してるようなので頑張ってメンテナンスしてください。

Q.ライトなWebサイト運用してる割には凝った設定では?

A.二度と編集しないようなページを作ってるなら必要ない作業ですね。年に一回くらいしか更新しならペイしないかも。

やれ誤字だ脱字だ、一行追加したい、すぐにやれとかワガママに対応したいならここまでこだわらなくていいでしょう。自分にはとてもても。

Q.CMS使いたくない割には、GitHub使うって無いのでは?

A.Webコンソールでの作業は抵抗無いのですが、HTMLを直接書きたい向けですね。

要はWikiのような軽量マークアップ言語は使いたくない(制御できない、表現力が低い)、覚えたくない(アレするのにコレ、が思い出せない)人向けというのが。 システムサイドとしては、オマケでイシュー管理(めんどくさい習性の丸投げ)やバージョン管理(勝手に変更の追跡)してくれるお手軽さが重要。

参考文献