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

如何在GitHub Action中不使用预定义环境参数实现文件定位

解答

首先可以明确:不需要依赖预定义环境参数,完全可以在Action脚本中遍历目录匹配目标文件,再把路径传入你的Python脚本,具体实现方案如下:


完整示例 workflow 配置

name: 合并文本文件
on: [push] # 可自行修改触发条件

jobs:
  merge-files:
    runs-on: ubuntu-latest
    steps:
      - name: 检出仓库代码
        uses: actions/checkout@v4

      - name: 配置Python环境
        uses: actions/setup-python@v5
        with:
          python-version: "3.x"

      - name: 遍历文件并执行合并脚本
        run: |
          # 匹配规则可按需修改,示例为匹配仓库内所有后缀为.txt的普通文件
          TARGET_FILES=$(find . -name "*.txt" -type f)
          # 可按需加排序规则,比如按文件名升序排列:
          # TARGET_FILES=$(find . -name "*.txt" -type f | sort)
          # 传入参数给Python脚本
          python3 your_merge_script.py $TARGET_FILES

常见调整说明

  • 如果需要缩小查找范围,把find命令的.改成指定目录即可,比如只找./articles目录下的txt:find ./articles -name "*.txt" -type f
  • 如果需要排除特定目录,比如排除.github和test目录:find . -path "./.github" -prune -o -path "./test" -prune -o -name "*.txt" -type f -print
  • 匹配结果可提前打印校验,在执行Python脚本前加一行echo "匹配到的文件:$TARGET_FILES"即可确认路径是否符合预期

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 01:06:03