GitHub Actions - 如何使用TypeScript

5

问题

我正在为Marketplace创建一个自定义GitHub Action,以便在其他项目中更轻松地使用。

该操作是使用TypeScript制作的,并使用了一些包,例如 typescriptbabel (编辑:不再使用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,但我无法弄清楚为什么这个可以而我的不行(我还没有测试过这个模板,也许它也不起作用)。
2个回答

5

我终于发现了问题。

在 TypeScript 中使用 @actions/core,正确的导入方式如下:

import * as core from "@actions/core"

您需要将* as放置在其中。

很抱歉,在问题描述中我给出的信息不正确,我确信我以正确的方式导入它。

有关该项目的更多详细信息,请参见我的操作


为什么会出现*,如果我正在使用带有allowSyntheticDefaultImports的TypeScript导入呢? - undefined

0

你定义了一个组合运行步骤动作,而不是一个JavaScript动作

请参考动作类型的解释

如果你看一下这个TypeScript动作模板,它已经将dist文件夹检入并在其action.yaml文件的runs.main属性中引用它。

这意味着你应该在本地编译你的TypeScript动作,并检入dist文件夹。它还建议使用ncc将其编译为单个文件。 如此处所述这里

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'

我尝试了使用ncc(https://github.com/vercel/ncc),但它也没有起作用。我将编辑问题以添加更详细的信息。 - user13158171

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接