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

使用GitHub Composite Action跨私有仓库运行Python正则扫描脚本遇文件缺失错误

解决Composite Action找不到main.py及扫描当前仓库文件的问题

问题核心是Composite Action不会自动拉取自身仓库的代码文件,同时要让脚本访问当前私有仓库的文件,需要在action.yml中明确添加两步checkout操作:拉取你的Composite Action仓库代码,以及拉取当前运行Action的目标仓库代码。

1. 修正composite-actions-repo中的action.yml配置

更新你的action.yml,添加以下关键步骤:

  • 拉取Composite Action自身仓库,确保main.py被下载到Runner环境
  • 拉取当前目标私有仓库(即调用该Action的仓库)
  • 配置Python环境并执行脚本

示例action.yml代码:

name: 'Regex File Scanner'
description: 'Scan files in repo for regex patterns'
runs:
  using: 'composite'
  steps:
    # 拉取Composite Action仓库代码(包含main.py)
    - name: Checkout composite action repo
      uses: actions/checkout@v4
      with:
        repository: your-username/composite-actions-repo
        ref: main # 替换为你的分支名
        path: composite-action # 将代码放到指定目录,避免和目标仓库冲突

    # 拉取当前目标私有仓库代码
    - name: Checkout target repo
      uses: actions/checkout@v4
      with:
        path: target-repo # 目标仓库代码存放目录,后续脚本可从这里扫描文件

    # 设置Python环境
    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: '3.10' # 匹配你的脚本依赖版本

    # 执行扫描脚本,指定目标仓库路径作为参数
    - name: Run regex scanner
      shell: bash
      run: |
        python composite-action/main.py --scan-path target-repo

2. 调整main.py脚本

修改你的main.py,支持接收命令行参数指定扫描路径,这样就能直接读取目标仓库的文件:

import os
import re
import argparse

def scan_files(path, pattern):
    # 你的扫描逻辑,遍历path下的文件匹配pattern
    for root, dirs, files in os.walk(path):
        for file in files:
            file_path = os.path.join(root, file)
            try:
                with open(file_path, 'r', encoding='utf-8') as f:
                    content = f.read()
                    if re.search(pattern, content):
                        print(f"Match found in: {file_path}")
            except Exception as e:
                print(f"Error reading {file_path}: {e}")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Scan files for regex patterns')
    parser.add_argument('--scan-path', required=True, help='Path to the directory to scan')
    parser.add_argument('--pattern', required=True, help='Regex pattern to match')
    args = parser.parse_args()
    
    scan_files(args.scan_path, args.pattern)

3. 私有仓库权限配置

在调用该Action的私有仓库(如a-private-repo)的Workflow文件中,确保GITHUB_TOKEN有足够权限拉取Composite Action仓库:

name: Run Regex Scanner
on: [push, pull_request]

permissions:
  contents: read # 确保能拉取私有仓库代码

jobs:
  scan:
    runs-on: ubuntu-latest
    steps:
      - name: Run regex scanner action
        uses: your-username/composite-actions-repo@main # 替换为你的Action仓库和分支
        with:
          pattern: 'your-regex-pattern-here' # 传递正则参数给脚本

如果Composite Action仓库也是私有仓库,可能需要使用Personal Access Token (PAT) 替代默认的GITHUB_TOKEN,在调用Action时通过secrets传入:

- name: Run regex scanner action
  uses: your-username/composite-actions-repo@main
  with:
    pattern: 'your-regex-pattern-here'
  env:
    GITHUB_TOKEN: ${{ secrets.PAT_WITH_REPO_ACCESS }}

注意:PAT需要拥有repo权限,才能访问私有仓库。

关键说明

  • 通过path参数将两个仓库的代码分开存放,避免文件覆盖或路径混淆
  • 脚本通过命令行参数指定扫描路径,确保能访问目标仓库的文件
  • 私有仓库间的访问需要配置正确的权限,否则会出现拉取失败的问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 10:17:13