think/scripts
fantasticit 5f56dc0448 Update README in scripts 2022-04-24 22:52:57 +08:00
..
README.md Update README in scripts 2022-04-24 22:52:57 +08:00
deploy.sh refactor: rename bin to scripts 2022-04-24 22:18:25 +08:00
update.sh refactor: rename bin to scripts 2022-04-24 22:18:25 +08:00

README.md

scripts

该目录下包含两个脚本。

deploy.sh

在服务端首次部署时使用,依赖 nodejspnpmpm2

update.sh

项目更新时使用,配合 webhook 实现自动化部署。

自动化部署

当在服务器完成首次部署后,后续如果每次迭代都手动进行更新会很麻烦,这时候就可以利用 github webhook 实现自动化部署。

第一步:在你的服务器配置 webhook

在你的服务器上安装 webhook,然后编写 hooks,然后后台运行 webhook

# 第一步:编写 hooks.json
touch hooks.json

# 第二步:创建 webhook.log
touch webhook.log

# 第三步:后台运行 webhook
nohup webhook -hooks hooks.json -hotreload -logfile webhook.log &

hooks 配置示例:

[
  {
    "id": "think-auto-update",
    "execute-command": "/apps/think/scripts/update.sh", // 注意根据项目修改脚本路径
    "command-working-directory": "/apps/think/",
    "http-methods": ["POST"],
    "pass-arguments-to-command": [
      {
        "source": "payload",
        "name": "head_commit.id"
      },
      {
        "source": "payload",
        "name": "pusher.name"
      },
      {
        "source": "payload",
        "name": "pusher.email"
      }
    ],
    "trigger-rule": {
      "and": [
        {
          "match": {
            "type": "payload-hmac-sha256",
            "secret": "please-rebuild-think",
            "parameter": {
              "source": "header",
              "name": "X-Hub-Signature-256"
            }
          }
        },
        {
          "match": {
            "type": "value",
            "value": "refs/heads/main",
            "parameter": {
              "source": "payload",
              "name": "ref"
            }
          }
        }
      ]
    }
  }
]

第二步:测试 webhook

本质上webhook 就是在服务器启动一个服务,然后通过 HTTP 调用相应的 URL 触发执行指定的脚本。

例如 think 项目的 webhookhttp://124.221.147.83:9000/hooks/think-auto-update

以 Github 为例,在设置中找到 webhooks 配置指定 URL注意 Content typeapplication/json