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

GitHub Actions中用$GITHUB_OUTPUT替换::set-output报错求助

解决GitHub Action中Python脚本替换::set-output为GITHUB_OUTPUT的语法问题

核心问题分析

你尝试用Shell的>> $GITHUB_OUTPUT写法直接套在Python的print语句里,这是语法错误——Python不支持这种Shell重定向语法。而你后续的文件写入代码,存在循环内重复写入和格式不规范的问题,导致报错。

正确解决方案

关键修改点

  1. 不要在循环内重复输出,等所有仓库名称收集完成后,一次性写入GITHUB_OUTPUT
  2. 写入文件时必须添加换行符\n,确保每个输出项单独占一行(GitHub Action要求的格式)
  3. 修复Python3兼容问题(原代码用了Python2的urllib2,GitHub Action默认是Python3环境)

修改后的完整代码

name: "Get all repos"
description: "Get a full list of repositories in the organization"

inputs:
  organization:
    required: true
    type: string
  token:
    required: true
    type: string  
    
outputs:
  repos:
    description: Comma separated list of repositories
    value: ${{ steps.get-all-repos.outputs.allRepos }}
    
runs:
  using: "composite"
  steps:
    - id: get-all-repos
      name: Get all repositories
      shell: python
      run: |
        import os
        import urllib.request
        import json
        repoResultsPerPage=100
        repoPageNumber=1
        gitHubOrganization='${{ inputs.organization }}'
        token='${{ inputs.token }}'
        allRepos = []
        allRepoNames = []
        # 补全GitHub API的完整URL前缀
        url = 'https://api.github.com/orgs/%s/repos?page=%s&per_page=%s' % (gitHubOrganization, repoPageNumber, repoResultsPerPage)
        headers = {'content-type': 'application/json', 'Authorization': 'token %s' % (token)}
        req = urllib.request.Request(url, headers=headers)
        response = urllib.request.urlopen(req)
        # Python3中read()返回bytes,需解码为字符串
        result = response.read().decode('utf-8')
        
        while result != '[]':
          print('PageNumber: %s' % (repoPageNumber))
          json_data = json.loads(result)
          # 用extend替代+,提升列表拼接效率
          allRepos.extend(json_data)
          repoPageNumber += 1
          url = 'https://api.github.com/orgs/%s/repos?page=%s&per_page=%s' % (gitHubOrganization, repoPageNumber, repoResultsPerPage)
          req = urllib.request.Request(url, headers=headers)
          response = urllib.request.urlopen(req)
          result = response.read().decode('utf-8')
        
        # 收集所有仓库名称
        for repo in allRepos:
          allRepoNames.append(repo['name'])
        allRepoNamesList = ','.join(allRepoNames)
        
        # 写入GITHUB_OUTPUT
        output_file = os.getenv('GITHUB_OUTPUT')
        if output_file:
            with open(output_file, 'a') as myfile:
                # 必须添加换行符,保证输出格式符合GitHub要求
                myfile.write(f"allRepos={allRepoNamesList}\n")

额外说明

  • 原代码的URL缺少https://api.github.com前缀,会导致请求失败,必须补全
  • Python3移除了urllib2模块,需替换为urllib.request
  • 加入if output_file:判断,避免环境变量未设置时的异常
  • 循环内只做数据收集,最后统一输出,避免重复覆盖输出值

内容的提问来源于stack exchange,提问作者EDMAC Tecnologia en Software

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 06:11:26