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

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

検証目標

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

自作スクリプトの設置

ディレクトリ作成

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

/root/.webhook/run/activate_id_update-contents

touch /root/.webhook/run/activate_id_update-contents
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

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

chmod 0755     /root/.webhook/bin/mecho
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=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>

スナップクラフト

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

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

'crontab' の構文ハイライトはサポートされていません。パーサーのヘルプ を御覧下さい。
/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.

参考文献