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

如何在GitHub Script脚本中指定@octokit/action的旧版本?

解决@octokit/action版本冲突问题的两种方法

你尝试的require("@octokit/action@v6.0.6")语法在github-script的环境里不生效,因为这个环境的require不是直接调用npm的包解析逻辑,而是依赖内置的预安装包。下面是两种可行的解决方案:

方法一:通过preinstall提前安装指定版本

利用github-script的preinstall参数,在执行脚本前安装你需要的@octokit/action旧版本,后续require会优先使用这个手动安装的版本:

- name: Send Trigger to Parent Repository
  uses: actions/github-script@v6.3.3
  with:
    preinstall: |
      npm install @octokit/action@6.0.6
    script: |
      const { Octokit } = require("@octokit/action");
      const octokit = new Octokit({
        auth: '${{ env.GITHUB_TOKEN }}'
      })
      await octokit.rest.repos.createDispatchEvent({
        owner: '${{ github.repository_owner }}',
        repo: '${{ matrix.parent_repo_name }}',
        event_type: '${{ env.EVENT_TYPE }}',
        client_payload: {
          submodule_name: '${{ github.event.repository.name }}',
          commit_message: `${{ github.event.head_commit.message }}`
        }
      });

方法二:直接使用github-script内置的Octokit实例

github-script已经内置了配置好认证的Octokit实例,不需要手动require和初始化,直接用github变量即可,还能彻底规避版本问题:

- name: Send Trigger to Parent Repository
  uses: actions/github-script@v6.3.3
  with:
    script: |
      await github.rest.repos.createDispatchEvent({
        owner: '${{ github.repository_owner }}',
        repo: '${{ matrix.parent_repo_name }}',
        event_type: '${{ env.EVENT_TYPE }}',
        client_payload: {
          submodule_name: '${{ github.event.repository.name }}',
          commit_message: `${{ github.event.head_commit.message }}`
        }
      });

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 13:55:12