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

Python脚本调用GitHub Actions workflow命令无法生成格式化警告

GitHub Actions中Python脚本输出警告不生效的解决方法

问题

在GitHub Actions工作流中运行Python脚本时,通过sys.stdout.write输出符合规范的::warning命令,结果被识别为普通文本,无法显示为GitHub Actions内置的警告样式。

相关代码

Python脚本(ValidateLangFiles.py)

#!/usr/bin/python
# Requires Python v3 Interpreter

import os
import sys
import glob
import configparser

ConfigStructure = {
    "file_menu": [ "file", "new", "open" ],
    "help_menu": [ "help", "about", "github" ],
    "open_file_popup": [ "open_file" ],
    "new_document_popup": [
        "title", "width_input", "height_input",
        "ok_button", "cancel_button"
    ],
    "about_csprite_popup": [
        "title", "contributors_header", "contributors_paragraph",
        "contributors_link", "os_projects_header", "os_projects_text",
        "close_button"
    ],
    "unicode_range": [ "range" ]
}

Files = []
ExitCode = 0

for f in glob.glob("./*.ini"):
    if os.path.isfile(f):
        Files.append(f)

for f in Files:
    try:
        config=configparser.ConfigParser()
        config.read(f)

        for Section in ConfigStructure:
            if not Section in config:
                sys.stdout.write("::warning file=\"" + f + "\",title=\"Section '" + Section + "' Not Found\"\n")
                break

            for SubSection in ConfigStructure[Section]:
                if not SubSection in config[Section]:
                    sys.stdout.write("::warning file=\"" + f + "\",title=\"Sub-Section '" + SubSection + "' of '" + Section + "' Not Found\"\n")

    except Exception as e:
        sys.stdout.write(f"::error file='{f}',title='Unhandled Error Occurred'::'{str(e)}'\n")
        ExitCode = 1

sys.exit(ExitCode)

GitHub Actions工作流配置

name: Lint
on:
  push:
    branches: [ master ]

jobs:
  lint:
    runs-on: ubuntu-22.04
    steps:
    - uses: actions/checkout@v4
      with:
        submodules: recursive

    - name: Install Python
      run: |
        sudo apt-get update -y
        sudo apt-get install python3 -y

    - name: Lint Languages Files
      run: |
        python3 ValidateLangFiles.py

问题原因

  1. 命令格式不完整:GitHub Actions的::warning命令需要遵循完整格式:::warning file=<文件名>,title=<标题>::<警告内容>,原脚本中缺少最后的::<警告内容>部分,导致无法被正确识别。
  2. 字符串转义问题:手动拼接字符串时使用了转义双引号,容易出现格式错误,且可读性差。
  3. 输出缓冲问题:sys.stdout.write可能存在缓冲,导致命令没有立即被GitHub Actions捕获。

解决方法

修改Python脚本,调整命令格式并优化输出方式:

#!/usr/bin/python
# Requires Python v3 Interpreter

import os
import sys
import glob
import configparser

ConfigStructure = {
    "file_menu": [ "file", "new", "open" ],
    "help_menu": [ "help", "about", "github" ],
    "open_file_popup": [ "open_file" ],
    "new_document_popup": [
        "title", "width_input", "height_input",
        "ok_button", "cancel_button"
    ],
    "about_csprite_popup": [
        "title", "contributors_header", "contributors_paragraph",
        "contributors_link", "os_projects_header", "os_projects_text",
        "close_button"
    ],
    "unicode_range": [ "range" ]
}

Files = []
ExitCode = 0

for f in glob.glob("./*.ini"):
    if os.path.isfile(f):
        Files.append(f)

for f in Files:
    try:
        config=configparser.ConfigParser()
        config.read(f)

        for Section in ConfigStructure:
            if not Section in config:
                # 使用f-string简化格式,补充完整的警告命令结构
                warning_msg = f"::warning file='{f}',title='Section \"{Section}\" Not Found'::配置文件缺少必要的Section: {Section}"
                print(warning_msg)  # 使用print自动换行并刷新缓冲区
                break

            for SubSection in ConfigStructure[Section]:
                if not SubSection in config[Section]:
                    warning_msg = f"::warning file='{f}',title='Sub-Section \"{SubSection}\" of \"{Section}\" Not Found'::配置文件缺少Section {Section}下的Sub-Section: {SubSection}"
                    print(warning_msg)

    except Exception as e:
        error_msg = f"::error file='{f}',title='Unhandled Error Occurred'::{str(e)}"
        print(error_msg)
        ExitCode = 1

sys.exit(ExitCode)

关键修改点

  1. 补全命令格式:为每个::warning命令添加::<警告内容>部分,符合GitHub Actions的命令规范。
  2. 使用f-string:替代手动字符串拼接,避免转义错误,提升代码可读性。
  3. 改用print函数:print会自动添加换行符并刷新输出缓冲区,确保命令被GitHub Actions即时捕获。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 16:15:05