如何在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
相关产品推荐
相关产品推荐

