You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过GitHub Actions自动执行Squash and Merge?遇模块缺失问题求解

问题

我正在通过GitHub Actions实现自动化,希望当仓库所有者提交特定comment时,触发现有Pull Request的squash-and-merge操作。我的Workflow代码如下:

merge-release:
  if: |
    ${{ github.repository == 'repo/to/avoid/execution/from/forks' }} &&
    ${{ (github.triggering_actor == 'xyz' }} &&
    ${{ github.event.client_payload.slash_command.command == "approve-merge" }}
  runs-on: ubuntu-latest
  steps:
    - uses: actions/github-script@v6
      with:
        script: |
          const { Octokit } = require("@octokit/rest");
          const octokit = new Octokit({ auth: '${{ secrets.GITHUB_TOKEN }}' });
          await octokit.request('PUT /repos/${{ github.action_repository }}/pulls/${{ github.event.number }}/merge', {
            owner: context.repo.owner,
            repo: context.repo.repo,
            pull_number: '${{ github.event.number }}',
            commit_title: 'Example',
            commit_message: 'Example.',
            headers: { 'X-GitHub-Api-Version': '2022-11-28' }
          })

运行时出现以下错误:

Error: Cannot find module '@octokit/rest'
Require stack:
- /home/runner/work/_actions/actions/github-script/v6/dist/index.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at resolve (node:internal/modules/cjs/helpers:108:19)
    at Object.apply (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15188:43)
    at eval (eval at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15143:16), <anonymous>:3:21)
    at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15144:12)
    at main (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15236:26)
    at /home/runner/work/_actions/actions/github-script/v6/dist/index.js:15217:1
    at /home/runner/work/_actions/actions/github-script/v6/dist/index.js:15268:3
    at Object.<anonymous> (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15271:12)
    at Module._compile (node:internal/modules/cjs/loader:1105:14) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
Error: Unhandled error: Error: Cannot find module '@octokit/rest'
Require stack:
- /home/runner/work/_actions/actions/github-script/v6/dist/index.js
    '/home/runner/work/_actions/actions/github-script/v6/dist/index.js'
  ]
}

请问我遗漏了什么配置?是否有其他实现该功能的方法?


解决方案

1. 错误原因及核心修复

报错的直接原因是actions/github-script@v6已经内置了预配置好的Octokit实例(通过github变量直接使用),不需要手动引入@octokit/rest包。同时原代码还有几处语法和逻辑问题,修复后的完整代码如下:

merge-release:
  if: |
    ${{ github.repository == 'repo/to/avoid/execution/from/forks' }} &&
    ${{ github.triggering_actor == 'xyz' }} &&
    ${{ github.event.client_payload.slash_command.command == "approve-merge" }}
  runs-on: ubuntu-latest
  steps:
    - uses: actions/github-script@v6
      with:
        script: |
          await github.rest.pulls.merge({
            owner: context.repo.owner,
            repo: context.repo.repo,
            pull_number: context.issue.number,
            commit_title: 'Example',
            commit_message: 'Example.',
            merge_method: 'squash', # 显式指定squash合并方式
            headers: { 'X-GitHub-Api-Version': '2022-11-28' }
          })

关键修正点:

  • 移除手动引入@octokit/rest和实例化Octokit的代码,直接使用内置的github.rest调用API
  • 添加merge_method: 'squash'参数:GitHub API默认合并方式是merge,必须显式指定才能触发squash合并
  • 修正if条件中的语法错误:原代码多了一个左括号(
  • 用context.issue.number替代硬编码的github.event.number,更符合GitHub Action上下文的规范用法

2. 简化实现的替代方案

如果不想手写脚本,可以使用成熟的第三方Action来快速实现:

使用actions-ecosystem/action-merge-pr

这个Action专门用于合并PR,支持指定合并方式,配置更简洁:

merge-release:
  if: |
    ${{ github.repository == 'repo/to/avoid/execution/from/forks' }} &&
    ${{ github.triggering_actor == 'xyz' }} &&
    ${{ github.event.client_payload.slash_command.command == "approve-merge" }}
  runs-on: ubuntu-latest
  steps:
    - uses: actions-ecosystem/action-merge-pr@v2
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        merge_method: squash
        commit_title: 'Example'
        commit_message: 'Example.'

内容的提问来源于stack exchange,提问作者tftd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 14:57:55