0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【nginx+webhook】github actionsに依存しない自動デプロイ

Posted at

背景

github actionsで自動デプロイ最高!

あっという間に無料枠を使いきる

枠があるの知らなかった😭
気にせず使いたいなあ。githubのwebhookを使えばよさそうだ!

やりたいこと

  1. ローカルからgithubにpush
  2. githubからサーバーにpushされたことをお知らせ
  3. サーバーが自動デプロイ

環境

サーバーの環境は以下のとおり

  • Ubuntu(22.04.4 LTS)
  • nginx(1.24.0)

手順

自動デプロイの実行ファイルを作成

あらかじめサーバー上に実行ファイルを作成しておいてください
ここは今回説明したいこととすこしずれるので割愛
こんなのをつくったとする↓

/opt/webhook/deploy.sh
#!/bin/bash

git fetch origin main
git merge origin/main

実行ファイルの権限はchmod a+x deploy.shしておく

adnanh/webhookをダウンロード

adnanh/webhook とは

「webhookを受け取ってコマンドを実行する」をあっという間にできるツールを使わせていただきます。すぎょい

adnanh/webhookをcurl経由でダウンロード

aptコマンドでもダウンロードできますが、最新版が欲しかったのでcurlでダウンロードします

なお執筆時点(2024/05/21)の最新バージョンは2.8.1でした

/opt/webhook/.
$ curl -L https://github.com/adnanh/webhook/releases/download/2.8.1/webhook-linux-amd64.tar.gz -O
$ tar zxvf webhook-linux-amd64.tar.gz

設定ファイルを作成

/opt/webhook/webhook-linux-amd64/.
$ vi github-webhook.json
/opt/webhook/webhook-linux-amd64/github-webhook.json
[
  {
    "id": "github-webhook",
    "execute-command": "/opt/webhook/deploy.sh",
    "command-working-directory": "/path/to/your/project",
    "trigger-rule":
    {
      "and":
      [
        {
          "match":
          {
            "type": "payload-hmac-sha1",
            "secret": "mysecret",
            "parameter":
            {
              "source": "header",
              "name": "X-Hub-Signature"
            }
          }
        },
        {
          "match":
          {
            "type": "value",
            "value": "refs/heads/main",
            "parameter":
            {
              "source": "payload",
              "name": "ref"
            }
          }
        }
      ]
    }
  }
]

各パラメータの説明

  • id
    • エンドポイントのパラメータになる値
  • execute-command
    • 実行ファイル(今回の例だとdeploy.sh)の絶対パス
  • command-working-directory
    • 実行ファイルの作業ディレクトリ
  • trigger-rule
    • 実行ファイルの発動条件

この例だと、mainブランチにプッシュされたとき、/path/to/your/project上で/opt/webhook/deploy.shを実行します

サーバー上でテストしてみる

webhookを起動してみる

/opt/webhook/.
$ ./webhook -hooks github-webhook.json -verbose

[webhook] 2024/05/29 12:22:29 version 2.8.1 starting
[webhook] 2024/05/29 12:22:29 setting up os signal watcher
[webhook] 2024/05/29 12:22:29 attempting to load hooks from github-webhook.json
[webhook] 2024/05/29 12:22:29 os signal watcher ready
[webhook] 2024/05/29 12:22:29 found 1 hook(s) in file
[webhook] 2024/05/29 12:22:29   loaded: github-webhook
[webhook] 2024/05/29 12:22:29 serving hooks on http://0.0.0.0:9000/hooks/{id}

デフォルトが9000番を使うようになってます
別のコンソールを起動して、同じサーバー上からアクセスしてみる

$ curl http://17.0.0.1:9000/hooks/github-webhook
/opt/webhook/.
$ ./webhook -hooks github-webhook.json -verbose

[webhook] 2024/05/29 12:22:29 version 2.8.1 starting
[webhook] 2024/05/29 12:22:29 setting up os signal watcher
[webhook] 2024/05/29 12:22:29 attempting to load hooks from github-webhook.json
[webhook] 2024/05/29 12:22:29 os signal watcher ready
[webhook] 2024/05/29 12:22:29 found 1 hook(s) in file
[webhook] 2024/05/29 12:22:29   loaded: github-webhook
[webhook] 2024/05/29 12:22:29 serving hooks on http://0.0.0.0:9000/hooks/{id}
# アクセスしたら、ここにアクセスされたよーてきなログがでる
# ただしtrigger-ruleがマッチしないので実行ファイルは実行されない

できたら、ctrl+cで一度終了します

デーモン化

webhookは動いていそうなので、systemdでデーモン化します

$ vi /etc/systemd/system/webhook.service
/etc/systemd/system/webhook.service
Description=webhook

[Service]
Type=simple
User=nginx
ExecStart=/opt/webhook/webhook-linux-amd64/webhook -hooks /opt/webhook/webhook-linux-amd64/github-webhook.json -verbose
TimeoutSec=60
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=webhook
Restart=always

[Install]
WantedBy=multi-user.target

細かいところはお好みで

$ sudo systemctl daemon-reload
$ sudo systemctl enable webhook
$ sudo systemctl start webhook

自動起動を有効化して、webhookを起動

nginxのプロキシを設定

プロジェクト用のnginxの設定ファイルを編集します

$ vi /etc/nginx/conf.d/project.conf
/etc/nginx/conf.d/project.conf
server{
  server_name your-project.jp;

  # これを追加
    location /github-webhook/ {
        proxy_pass http://127.0.0.1:9000/hooks/github-webhook;
    }

    # かならずインデックスより上に書くこと!
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
}

これでhttps://your-project.jp/github-webhook/に接続すると、先ほどのwebhookに飛ばされます

githubにwebhookを追加

リポジトリ>Settings>Webhooks>Add webhook

image.png

content typeをapplication/jsonにしないとうごきませんでした

これで作成してpingが通ればOK!

参考

この方々がいなければ一生github actionsの苦しみから逃れられませんでした。マジ感謝。ありがとうございます

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?