解决 Netlify 免费 Build 额度太少的问题

解决 Netlify 免费 Build 额度太少的问题

January 25, 2024
工具箱 ,

这个月因为测试主题,所以 build 时常用的多了一些,今天上 Netlify 后台一看才发现 Build 时常已经超过了 45 分钟,估计账单出来了之后还必须要交完费才能正常访问,各位用 Netlify 的用户要注意一下这个问题了。

Netlify 每个月只有 300 分钟的 Build 时常,相较于 Github Action 的 2000 分钟,为了避免之后 Netlify 还出现这种情况,所以研究了一下,看能不能在 Github 上完成 build 操作,Netlify 只做静态网站运行。

根据这个需求,用 ChatGPT 简单糊了一个 Action 配置,目前能用,没什么问题。

配置中需要用到 NETLIFY_AUTH_TOKEN 可以在 Netlify 后台 Personal access tokens 里获取,NETLIFY_SITE_ID 直接在站点信息里就能看到。

另外,如果想要使用 webhook 触发 Github hook,需要使用 repository_dispatch 来自定义 action 类型,具体可以参考:触发工作流的事件考

最后的 Action 如下。

on: 
  repository_dispatch:
    types:
      - update

jobs:
  build_deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@master

      - name: Set Node.js 20.x
        uses: actions/setup-node@master
        with:
          node-version: 20.x        

      - name: Run install
        uses: borales/actions-yarn@v4
        with:
          cmd: install # will run `yarn install` command

      - name: Build production bundle
        uses: borales/actions-yarn@v4
        with:
          cmd: prod # will run `yarn prod` command


      - name: Deploy to Netlify
        uses: netlify/actions/cli@master
        env:
          NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
          NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
          build_directory: dist
        with:
          args: deploy --prod

这个时候我们就可以通过代码发起 POST 请求触发 Action 了。

代码中的 TRIGGER_TOKEN前文提到的 去 Github 生成 一个具有 rep 权限的 token(要常规模式的 token,不要问我怎么知道的)

curl -X POST https://api.github.com/repos/:owner/:repo/dispatches \
    -H "Accept: application/vnd.github.everest-preview+json" \
    -H "Authorization: Bearer TRIGGER_TOKEN" \
    --data '{"event_type": "TRIGGER_EVENT"}'

结合我们上次提到的 用 GithubAction 替代 Zapier?? 一文,依旧可以通过这个方式来配合 Ghost 的 webhook 触发 Github Action 的 webhook。

actionflow 的配置文件如下:

on:
  webhook:
jobs:
  request:
    name: Make a HTTP Request
    runs-on: ubuntu-latest
    steps:
      - name: Make a HTTP Request
        uses: actionsflow/axios@v1
        id: info
        with:
          url: https://api.github.com/repos/rebron1900/11ty-theme-notability/dispatches
          method: POST
          headers: '{"Accept":"application/vnd.github.everest-preview+json", "Authorization":"Bearer ${{secrets.ACTION_TOKEN}}"}'
          data: '{"event_type": "update"}'
💡
不知道我的私有库可以用这个方式发布吗??下次试试。

加入评论