2

I'm trying out github actions because I need to make the same changes to a lot of .mei files (xml-language) in my repository. Even after some (bad) attempts at fixing and looking through the documentation, I can't get it to work. The python script uses regex and walks on my own computer, so I think my problem lies within the .yml file.

I get the following error on git commit:

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.

There are files that the regex would apply to which didn't change.

My Python file:

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()

My .yml file:

# 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 }}

the requirements.txt file is empty, since os and re are in the standard distribution.

Any help is appreciated, thank you.

Because I'm still testing I commented out the on section. Is there a way to only run the action on certain commit messages (in this case: created mei)?

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
brathenne
  • 29
  • 5
  • 1
    Since your script is located at `.github/workflows/meichange.py` -- your code will walk the directory `.github/`. Is that the intended behavior? My guess is that you're not walking the directory you are expecting and therefore no changes are occurring. Try printing the `dir_path` variable to be sure it is what you expect. – sytech Apr 19 '23 at 06:01
  • @sytech Thank you, that helped. Now I run into other issues though. This repo is a private fork, so I should have full access and when I created my Github token, I checked the repo checkbox, so it should grant full control. Still I get this error: Run git config --global user.name "GitHub Actions" [main b9e7b04] Update files 9 files changed, 11 insertions(+), 20 deletions(-) remote: Write access to repository not granted. fatal: unable to access 'repo-adress': The requested URL returned error: 403 Error: Process completed with exit code 128. – brathenne Apr 19 '23 at 07:29
  • 1
    It doesn't look like you're telling git to use your `GITHUB_TOKEN` anywhere. You probably need to configure git credentials. See also: [Push to origin from GitHub action](https://stackoverflow.com/a/57932145/5747944) – sytech Apr 19 '23 at 07:49

1 Answers1

1

In your code, your dir_path is being set relative to the script file:

dir_path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))

Your script is located at .github/workflows/meichange.py meaning you are walking the .github/ directory, which is probably not what you meant to do and why you are not seeing any changes being added by your script.

If you wanted to walk the root of the repo, you want to move up another level (e.g., "../..").

Expressed another way, using pathlib:

import pathlib
#... 
# the root of the git repo
dir_path = pathlib.Path(__file__).parent.parent.parent

Additionally, even though you have set the GITHUB_TOKEN environment variable, you are not configuring your git credentials in any way and therefore you will be given an access denied if you attempt to push changes as this is currently written. See answers of this question for how to configure action to push successfully: Push to origin from GitHub action.

sytech
  • 29,298
  • 3
  • 45
  • 86
  • First of all, thank you! I tried to implement the git remote set-url --push origin https://Username:$GITHUB_TOKEN@github.com/my/repo line after both git configure lines but the error didn't change. Could it be the problem that this is actually just a fork of a private repo where I'm not the admin (but have write access)? Or could my token be the problem? I renewed it several times but that didn't help... – brathenne Apr 19 '23 at 10:22
  • i also tried using checkoutv3 too but the error remains "GitHub Actions" [main b9e7b04] Update files 9 files changed, 11 insertions(+), 20 deletions(-) remote: Write access to repository not granted. fatal: unable to access 'repo-adress': The requested URL returned error: 403 Error: Process completed with exit code 128 – brathenne Apr 19 '23 at 11:19