复合类型GitHub Action跨仓库运行失败:缺少动作文件
修复Composite GitHub Action在其他仓库运行失败的问题
你的Composite Action无法在其他仓库正常运行,核心问题集中在依赖文件未正确分发、路径处理不通用、依赖安装逻辑错误这几点,下面是具体的修复方案:
1. 确保Action仓库包含所有必要文件
你的Action仓库必须包含以下文件,否则其他仓库调用时会找不到关键资源:
action.yml(已存在)entry.sh(已存在)package.json(需要新增,用于管理mdanki依赖)
不要提交node_modules到仓库,依赖会在Action运行时动态安装。
2. 修正action.yml的配置问题
原配置中的硬编码路径和工作目录错误,导致在其他仓库无法定位文件。替换为以下配置:
name: 'anki-action' description: 'Build anki cards from .md files' branding: icon: alert-triangle color: orange runs: using: 'composite' steps: - name: Install Action dependencies run: npm install shell: bash working-directory: ${{ github.action_path }} - name: Generate Anki cards run: ./entry.sh ${{ inputs.path }} shell: bash working-directory: ${{ github.action_path }} - name: Upload generated Anki package uses: actions/upload-artifact@v4 with: name: anki-cards path: ${{ github.workspace }}/${{ inputs.path }}/cards.apkg inputs: path: description: 'The directory with .md files (relative to your repository root)' required: false default: 'anki-cards'
working-directory: ${{ github.action_path }}确保所有命令在Action的目录下执行,避免干扰用户仓库环境- 依赖安装步骤明确在Action目录执行,保证mdanki被安装到正确位置
- 上传artifact的路径改为基于用户工作区的相对路径,适配任何仓库的runner工作目录
- 升级
upload-artifact到v4版本,提升稳定性
3. 修复entry.sh脚本的逻辑错误
原脚本的目录切换和文件遍历逻辑存在问题,替换为以下代码:
set -e set -x # 获取输入的md文件目录(相对于用户仓库根目录) input_path="${1:-anki-cards}" # 拼接用户仓库中目标目录的绝对路径 target_dir="${GITHUB_WORKSPACE}/${input_path}" # 检查目标目录是否存在,不存在则报错退出 if [ ! -d "$target_dir" ]; then echo "Error: Directory $target_dir does not exist in your repository." exit 1 fi # 遍历目标目录下的所有.md文件(安全处理带空格的文件名) for filename in "$target_dir"/*.md do # 跳过目录下没有md文件的情况 [ -e "$filename" ] || continue # 使用Action目录下的mdanki生成卡片,输出到目标目录 ./node_modules/mdanki/src/index.js "$filename" "$target_dir/cards.apkg" done
- 直接使用
GITHUB_WORKSPACE拼接路径,避免不必要的目录切换 - 用
"$target_dir"/*.md遍历文件,比ls更可靠,支持带特殊字符的文件名 - 增加目录存在检查,提前暴露问题,方便调试
- 明确指定mdanki的路径,确保调用的是Action自身安装的依赖
4. 新增package.json管理依赖
在你的anki-action仓库根目录创建package.json,添加mdanki依赖:
{ "name": "anki-action", "version": "1.0.0", "dependencies": { "mdanki": "^1.0.0" } }
这个文件是npm安装mdanki的依据,必须存在。
5. 在其他仓库调用Action的正确方式
以leetcode-anki仓库为例,创建.github/workflows/generate-anki.yml,内容如下:
name: Generate LeetCode Anki Cards on: [push] jobs: build-cards: runs-on: ubuntu-latest steps: - name: Checkout repository code uses: actions/checkout@v4 - name: Run Anki Card Generator uses: your-username/anki-action@v1 # 替换为你的GitHub用户名和Action版本号 with: path: 'path/to/leetcode-md-files' # 替换为你仓库中md文件的实际目录
- 必须先执行
actions/checkout,让Action能访问到用户仓库中的md文件 - 指定Action的仓库地址和版本,确保调用的是最新修复后的版本
内容的提问来源于stack exchange,提问作者blablatdinov
相关产品推荐
相关产品推荐

