GitHub Actions - 如何使用 TypeScript

5 github typescript github-actions building-github-actions

问题

我正在为 Marketplace 做一个自定义 GitHub Action,以使其更容易在其他项目中使用。

该 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
Run Code Online (Sandbox Code Playgroud)

编辑:问题是包的导入不正确@actions/core

附加信息

为了能够安装软件包并构建,我必须在我的action.yml文件中执行以下操作:

TypeError: Cannot read property 'getInput' of undefined
Run Code Online (Sandbox Code Playgroud)

我的文件夹结构:

|-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
Run Code Online (Sandbox Code Playgroud)

使用我上面发送的action.yml,返回以下错误:

TypeError: Cannot read property 'getInput' of undefined
Run Code Online (Sandbox Code Playgroud)

getInput是一种方法@actions/core(正在正确导入并且不是问题

编辑:这就是问题哈哈哈。


如果我不运行yarn installnpm install使用某些脚本,则会发生以下错误:

Error: Cannot find module '@actions/core'
Run Code Online (Sandbox Code Playgroud)

在我看到的教程中,他们都不需要安装软件包,就像它们是自动安装的一样。


我还尝试使用ncc并将编译后的代码推送到操作存储库,但它也不起作用。

我的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
Run Code Online (Sandbox Code Playgroud)

我的文件夹结构:

|-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
Run Code Online (Sandbox Code Playgroud)

使用上面的配置,会发生同样的错误:

TypeError: Cannot read property 'getInput' of undefined
Run Code Online (Sandbox Code Playgroud)

这就是我@actions/core在第一行中导入的方式src/index.ts

import core from "@actions/core";
Run Code Online (Sandbox Code Playgroud)
  • 我没有将 node_modules 推送到存储库,只是将 dist 文件夹推送到存储库(我也尝试过不使用该文件夹,在操作执行期间构建,但它也不起作用。)
  • 我也找到了这篇文章,但也没有成功。

问题

  • 关于如何在 GitHub Actions 中使用 typescript 有什么想法吗?
  • 为什么模块没有加载?
  • 是否有关于使用 TypeScript 与 GitHub Actions for Marketplace 合作的任何教程、文章、视频或任何类型的内容?我找到了这个模板,但我无法找出为什么它有效而我的却无效(我没有测试过该模板,也许它也不起作用)。

小智 5

我终于发现了问题所在。

要在 TypeScript 中使用@actions/core,这是导入它的正确方法:

import * as core from "@actions/core"
Run Code Online (Sandbox Code Playgroud)

你需要放一个* as.

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

有关该项目的更多详细信息,这是我的行动