GitHub 动作 - 如何使用 TypeScript
GitHub Actions - How To Work With TypeScript
问题
我正在为 Marketplace 执行自定义 GitHub 操作,以使其更易于在其他项目中使用。
Action 使用 TypeScript 制作,并使用了一些包,如 typescript
、babel
(编辑: 不再使用 babel), @actions/core
和 @actions/github
.
当我将Action添加到另一个项目的工作流中时,他可以安装包并构建项目,甚至可以初始化它,但是当开始执行时,他找不到@actions模块和core of @actions/core is undefined(@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'
在我看到的教程中,none个需要安装包,就像自动安装一样。
我也尝试过 ncc 并将编译后的代码推送到 action repo,但它也没有用。
我的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 操作中使用打字稿有什么想法吗?
- 为什么没有加载模块?
- 是否有关于使用 TypeScript 和 GitHub Actions for Marketplace 的任何教程、文章、视频或任何类型的内容?我找到了 this template,但我无法找出为什么这行得通而我的行不通(我还没有测试模板,也许它也行不通)。
您定义了 Composite run steps action
而不是 JavaScript action
。
如果您查看 this TypeScript action template,它会签入 dist
文件夹并在 属性 runs.main
的 action.yaml
中引用它。
这意味着您应该在本地编译您的 TypeScript 操作并检查 dist
文件夹。它还说使用 ncc
编译为单个文件。
如所述 here.
name: 'Your name here'
description: 'Provide a description here'
author: 'Your name or organization here'
inputs:
milliseconds: # change this
required: true
description: 'input description here'
default: 'default value if applicable'
runs:
using: 'node12'
main: 'dist/index.js'
终于发现问题了
要在 TypeScript 中使用 @actions/core
,这是导入它的正确方法:
import * as core from "@actions/core"
你需要放一个* as
.
很抱歉我在问题描述中提供的信息不正确,我确信我是以正确的方式导入它。
项目详情,this is my action
问题
我正在为 Marketplace 执行自定义 GitHub 操作,以使其更易于在其他项目中使用。
Action 使用 TypeScript 制作,并使用了一些包,如 typescript
、babel
(编辑: 不再使用 babel), @actions/core
和 @actions/github
.
当我将Action添加到另一个项目的工作流中时,他可以安装包并构建项目,甚至可以初始化它,但是当开始执行时,他找不到@actions模块和core of @actions/core is undefined(@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'
在我看到的教程中,none个需要安装包,就像自动安装一样。
我也尝试过 ncc 并将编译后的代码推送到 action repo,但它也没有用。
我的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 操作中使用打字稿有什么想法吗?
- 为什么没有加载模块?
- 是否有关于使用 TypeScript 和 GitHub Actions for Marketplace 的任何教程、文章、视频或任何类型的内容?我找到了 this template,但我无法找出为什么这行得通而我的行不通(我还没有测试模板,也许它也行不通)。
您定义了 Composite run steps action
而不是 JavaScript action
。
如果您查看 this TypeScript action template,它会签入 dist
文件夹并在 属性 runs.main
的 action.yaml
中引用它。
这意味着您应该在本地编译您的 TypeScript 操作并检查 dist
文件夹。它还说使用 ncc
编译为单个文件。
如所述 here.
name: 'Your name here'
description: 'Provide a description here'
author: 'Your name or organization here'
inputs:
milliseconds: # change this
required: true
description: 'input description here'
default: 'default value if applicable'
runs:
using: 'node12'
main: 'dist/index.js'
终于发现问题了
要在 TypeScript 中使用 @actions/core
,这是导入它的正确方法:
import * as core from "@actions/core"
你需要放一个* as
.
很抱歉我在问题描述中提供的信息不正确,我确信我是以正确的方式导入它。
项目详情,this is my action