You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

创建将多Markdown转为单JSON的GitHub Action失败,求解决

排查GitHub Action未生成JSON文件的问题

以下是几个核心排查方向和解决方法:

1. 路径匹配/写入路径错误

GitHub Actions的运行工作区是仓库根目录(通过$GITHUB_WORKSPACE环境变量获取),如果你的Node脚本使用相对路径,可能因为脚本所在目录和工作目录不一致,导致找不到源文件或写入到错误位置。

  • 修复脚本中的路径:用path.join结合process.env.GITHUB_WORKSPACE构建绝对路径,示例:
const path = require('path');
// 匹配仓库根目录下的所有md文件
const mdFilePattern = path.join(process.env.GITHUB_WORKSPACE, '**/*.md');
// 输出到仓库根目录的output.json
const outputPath = path.join(process.env.GITHUB_WORKSPACE, 'output.json');

2. 脚本未捕获错误,掩盖了实际问题

如果脚本只输出预设日志,没有错误捕获逻辑,就算读取文件、写入文件失败也不会显示错误信息,导致你误以为脚本正常运行。

  • 给脚本添加完整的错误捕获:
const fs = require('fs').promises;
const path = require('path');
const glob = require('glob-promise');

async function convertMdToJson() {
  try {
    console.log('Starting MD to JSON conversion');
    const mdFiles = await glob(path.join(process.env.GITHUB_WORKSPACE, '**/*.md'));
    
    if (mdFiles.length === 0) {
      throw new Error('No Markdown files found in the workspace');
    }

    const jsonContent = await Promise.all(
      mdFiles.map(async file => ({
        filename: path.basename(file),
        content: await fs.readFile(file, 'utf8')
      }))
    );

    const outputPath = path.join(process.env.GITHUB_WORKSPACE, 'output.json');
    await fs.writeFile(outputPath, JSON.stringify(jsonContent, null, 2));
    console.log(`JSON file written to ${outputPath}`);
  } catch (err) {
    console.error('Conversion failed:', err.message);
    process.exit(1); // 让Action标记为失败,便于排查
  }
}

convertMdToJson();

重新运行Action后,查看日志中的错误信息,就能定位具体问题(比如依赖未安装、文件不存在等)。

3. 工作流未保留生成的文件

GitHub Actions的运行环境是临时的,脚本生成的文件如果没有通过上传artifact或提交到仓库的步骤保存,Action结束后文件会被销毁,你看不到生成的结果。

  • 在工作流中添加上传artifact步骤,以便查看生成的文件:
jobs:
  convert:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
      - name: Install dependencies
        run: npm install glob-promise # 如果用了glob类依赖
      - name: Run conversion script
        run: node buildJson.js
      - name: List workspace files
        run: ls -la $GITHUB_WORKSPACE # 确认文件是否生成
      - name: Upload JSON artifact
        uses: actions/upload-artifact@v4
        with:
          name: generated-json
          path: output.json

运行后,在Action的Artifacts标签下可以下载生成的JSON文件,确认是否成功生成。

如果需要将文件提交到仓库,添加git提交步骤:

- name: Commit generated JSON
        run: |
          git config --global user.name "GitHub Actions Bot"
          git config --global user.email "actions@github.com"
          git add output.json
          git commit -m "chore: update generated JSON file" || echo "No changes to commit"
          git push

4. 依赖未正确安装

如果脚本使用了第三方库(比如glob),工作流中没有安装依赖的步骤,会导致脚本运行失败但只输出预设日志。

  • 确保工作流中有npm install步骤,或者如果项目有package.json,直接运行npm ci来安装依赖。

内容的提问来源于stack exchange,提问作者desert_dweller

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 23:01:09