问题
我正在为Marketplace创建一个自定义GitHub Action,以便在其他项目中更轻松地使用。
该操作是使用TypeScript制作的,并使用了一些包,例如 typescript
,babel
(编辑:不再使用Babel),@actions/core
和 @actions/github
。
当我将Action添加到另一个项目的工作流程中时,他可以安装软件包并构建项目,甚至初始化项目,但是当执行开始时,他无法找到 @actions 模块和 @actions/core 的核心未定义(@actions/core 是第一个要使用的软件包,因此它会导致管道崩溃)。
node_modules
文件夹已正确创建,并且软件包已在其中,但在脚本内部,它们没有被加载。
可能的原因
当我尝试在我的计算机上运行构建版本(使用ncc和tsc之一构建的版本)时,发生了同样的错误:
TypeError: Cannot read property 'getInput' of undefined
编辑:问题在于错误导入了
@actions/core
包
附加信息
为了能够安装包和构建,我不得不在我的action.yml
文件中执行以下操作:
runs:
using: "composite"
steps:
- run: cd ${{ github.action_path }}
shell: bash
- run: yarn --cwd ${{ github.action_path }} --production=true
shell: bash
- run: yarn --cwd ${{ github.action_path }} build
shell: bash
- run: yarn --cwd ${{ github.action_path }} start
shell: bash
我的文件夹结构:
|-dist # THIS IS'NT BEING PUSHED TO THE REPO
|-src
|--index.ts # Where the @actions/core is required
|--... # More files that are imported by index.ts
|-node_modules # THIS IS'NT BEING PUSHED TO THE REPO
|--... # All the packages are here, this is right and ISN'T THE PROBLEM
|-action.yml
|-babel.config.js
|-package.json
|-tsconfig.json
|-yarn.lock
使用我上面发送的action.yml文件,会返回以下错误:
TypeError: Cannot read property 'getInput' of undefined
"getInput"是"@actions/core"的一个方法(已正确导入,并且不是问题的原因)。 编辑:这就是问题,哈哈。
如果我没有运行任何脚本来执行
yarn install
或者npm install
,就会出现以下错误:Error: Cannot find module '@actions/core'
在我看到的教程中,没有一个需要安装软件包,就像它们自动安装了一样。
我还尝试了使用ncc,并将编译后的代码推送到操作存储库中,但仍然无法工作。
我的action.yml文件:
runs:
using: "node12"
main: "index.js"
我的文件夹结构:
|-src
|--index.ts # Where the @actions/core is required
|--... # More files that are imported by index.ts
|-node_modules # THIS IS'NT BEING PUSHED TO THE REPO
|--... # All the packages are here, this is right and ISN'T THE PROBLEM
|-action.yml
|-index.js # The compiled code, that is being pushed to the repo
|-package.json
|-tsconfig.json
|-yarn.lock
使用上述配置,出现了相同的错误:
TypeError: Cannot read property 'getInput' of undefined
这是我在
src/index.ts
的第一行导入@actions/core
的方式:import core from "@actions/core";
- 我没有将node_modules推送到存储库中,只有dist文件夹(我尝试在操作执行期间进行构建但也不起作用)。
- 我还发现了this article,但它也不起作用。
问题
- 有关如何在GitHub Actions中使用TypeScript的任何想法吗?
- 为什么模块没有加载?
- 有关在Marketplace中使用TypeScript与GitHub Actions的任何类型的教程、文章、视频或内容吗?我找到了this template,但我无法弄清楚为什么这个可以而我的不行(我还没有测试过这个模板,也许它也不起作用)。
allowSyntheticDefaultImports
的TypeScript导入呢? - undefined