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

使用GitHub Action执行Python脚本修改MEI文件遇提交错误及触发咨询

问题与解决方案

问题概述

本地可正常运行的Python批量修改MEI文件脚本,在GitHub Action中执行时,git commit步骤提示「工作树无变更」;同时需要实现仅当提交信息包含“created mei”时触发Action的规则。

报错信息

Run git config --global user.name "GitHub Actions"
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean
Error: Process completed with exit code 1.

现有代码

Python修改脚本(meichange.py)

import re
import os

def main():
    dir_path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
    for root, dirs, files in os.walk(dir_path):
        for file in files:
            if file.endswith(".mei"):
                filechange(os.path.join(root, file))

def filechange(adress):
    with open(adress) as f:
        mei=f.read()
    mei = re.sub(r"<score[^>]*>\s*(<scoreDef[^>]*)(mnum\.visible=\"\w\" )?([^>]*>)",r'\1 mnum.visible="false"\3',mei)
    mei = re.sub(r"<meterSig[^>]*>",r'',mei)
    mei = re.sub(r"(<slur[^>]*)endid=\"#\d+\"([^>]*>)",r'\1tstamp2="0m+1"\2',mei)
    with open(adress,"w") as f:
        f.write(mei)
        
if __name__ == "__main__":
    main()

GitHub Action配置(.github/workflows/mei-modify.yml)

# This is a basic workflow to help you get started with Actions

name: Change MEI via Python

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the "main" branch
  #push:
   # branches: [ "main" ]
  #pull_request:
    #branches: [ "main" ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: '3.11'
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r .github/workflows/requirements.txt
    - name: Change files
      run: python .github/workflows/meichange.py
    - name: Commit changes
      run: |
        git config --global user.name "GitHub Actions"
        git config --global user.email "actions@github.com"
        git add .
        git commit -m "Update files"
        git push
      env:
        GITHUB_TOKEN: ${{ secrets.SECRET_GITHUB_TOKEN }}

排查与修复方案

1. 修复脚本路径问题(解决“无变更”报错)

脚本中计算根目录的逻辑在GitHub Action环境中出错:

  • 脚本位于.github/workflows目录,os.path.join(os.path.dirname(__file__), "..")会指向.github目录,而非仓库根目录,导致无法遍历到目标MEI文件。
  • 替换为直接使用仓库根目录(Action checkout后默认工作目录就是仓库根):

修改Python脚本的main函数:

def main():
    dir_path = os.getcwd()  # 直接使用当前工作目录(仓库根)
    for root, dirs, files in os.walk(dir_path):
        for file in files:
            if file.endswith(".mei"):
                filechange(os.path.join(root, file))

2. 避免无变更时commit报错

当前commit步骤在无文件变更时会直接报错退出,需添加判断逻辑,仅当有变更时才执行提交:

修改Commit步骤的run命令:

git config --global user.name "GitHub Actions"
git config --global user.email "actions@github.com"
git add .
# 检查是否有变更,有则提交
git diff --quiet || git commit -m "Update MEI files via Action"
# 仅当存在待推送的变更时执行push
git diff --quiet origin/main || git push

3. 配置提交信息触发规则

在Action的on字段中添加push触发条件,仅当提交信息包含“created mei”且推送到main分支时触发:

修改YML的on部分:

on:
  push:
    branches: [ "main" ]
    # 忽略工作流文件变更触发,避免循环执行
    paths-ignore:
      - ".github/workflows/**"
    # 仅当提交信息包含指定字符串时触发
    if: contains(github.event.head_commit.message, 'created mei')
  workflow_dispatch: # 保留手动触发选项

最终完整YML配置

name: Change MEI via Python

on:
  push:
    branches: [ "main" ]
    paths-ignore:
      - ".github/workflows/**"
    if: contains(github.event.head_commit.message, 'created mei')
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: '3.11'
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        # 空requirements.txt可省略此行,或保留
        pip install -r .github/workflows/requirements.txt
    - name: Change files
      run: python .github/workflows/meichange.py
    - name: Commit and push changes
      run: |
        git config --global user.name "GitHub Actions"
        git config --global user.email "actions@github.com"
        git add .
        git diff --quiet || git commit -m "Update MEI files via Action"
        git diff --quiet origin/main || git push
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # 默认GITHUB_TOKEN已具备推送权限,无需自定义密钥

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 09:07:15