Issue
I'm trying to write an action to automatically generate markdown summaries for a repository by running a Python script:
import os
if __name__ == "__main__":
# Walk through all directories and files
for root, dirs, files in os.walk('.'):
# Ignore hidden directories
if root.startswith('.\\.'):
continue
# Filepath to summary file
summary_file_path = f'{root}/Summary.md'
with open(summary_file_path, "w") as summary_file:
# Add title
summary_file.write("# Summary\n\n")
# Add directories to summary
for dir in dirs:
# Ignore hidden directories
if not dir.startswith('.'):
# Add to summary
summary_file.write(f"- [{dir}]({dir}/Summary.md)\n")
# Add files to summary
for file in files:
# Ignore specific files
if not (file == 'Summary.md' or file == 'Script.py'):
# Remove extension
filename = os.path.splitext(file)[0]
# Replace underscores with spaces
filename = filename.replace('_', ' ')
# Add to summary
summary_file.write(f"- [{filename}]({file})\n")
And to run the script, I wrote the action:
# This name will appear in the Actions
name: Generate Summaries
on:
# Allows to manually run the job(s) at any time
workflow_dispatch:
# Run on every push on the master branch
push:
branches:
- main
# Jobs list
jobs:
# Job name
generate-summaries:
# Where the workflow will run
runs-on: ubuntu-latest
# Steps list
steps:
# Checkout the repository content
- name: Checkout repository
uses: actions/checkout@v4
# Setup python
- name: Setup python
uses: actions/setup-python@v4
with:
python-version: '3.10'
# Generate/Update summaries
- name: Generate summaries
run: python Script.py
# Commit all changes
- name: Commit changes
run: |
git config user.name 'github-actions[bot]'
git config user.email 'github-actions[bot]@users.noreply.github.com'
git add .
git commit -m "Updated summaries" || exit 0
git push origin main
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
The script should create a Summary.md file in each directory (excluding hidden ones), listing the contents of each one. In other words, considering this folder structure:
.
├── .github/
│ └── workflows/
│ └── main.yaml
├── ExampleFolder1/
│ ├── ExampleFolder1.md
│ └── ExampleFolder11/
│ └── ExampleFolder11.md
├── ExampleFolder2/
│ └── ExampleFolder2.md
└── Script.py
The result should be:
.
├── .github/
│ └── workflows/
│ └── main.yaml
├── ExampleFolder1/
│ ├── ExampleFolder1.md
│ ├── Summary.md
│ └── ExampleFolder11/
│ ├── ExampleFolder11.md
│ └── Summary.md
├── ExampleFolder2/
│ ├── ExampleFolder2.md
│ └── Summary.md
├── Script.py
└── Summary.md
And it works properly when I run it locally, but it throws an error when it runs in the workflow:
Run git config user.name 'github-actions[bot]'
git config user.name 'github-actions[bot]'
git config user.email 'github-actions[bot]@users.noreply.github.com'
git add .
git commit -m "Updated summaries" || exit 0
git push origin main
shell: /usr/bin/bash -e {0}
env:
pythonLocation: /opt/hostedtoolcache/Python/3.10.13/x64
PKG_CONFIG_PATH: /opt/hostedtoolcache/Python/3.10.13/x64/lib/pkgconfig
Python_ROOT_DIR: /opt/hostedtoolcache/Python/3.10.13/x64
Python2_ROOT_DIR: /opt/hostedtoolcache/Python/3.10.13/x64
Python3_ROOT_DIR: /opt/hostedtoolcache/Python/3.10.13/x64
LD_LIBRARY_PATH: /opt/hostedtoolcache/Python/3.10.13/x64/lib
GITHUB_TOKEN: ***
To https://github.com/diegoborbadev/summariestest
[main ec81c99] Updated summaries
! [remote rejected] main -> main (refusing to allow a GitHub App to create or update workflow `.github/workflows/Summary.md` without `workflows` permission)
error: failed to push some refs to 'https://github.com/diegoborbadev/summariestest'
4 files changed, 12 insertions(+)
create mode 100644 .github/Summary.md
create mode 100644 .github/workflows/Summary.md
create mode 100644 FrontEnd/Summary.md
create mode 100644 Summary.md
Error: Process completed with exit code 1.
The error occurs due to permissions to modify the workflow, I understand, but the script shouldn't be modifying that folder. It has filters to prevent this from happening, and locally, it doesn't occur. So, why does it exhibit this behavior when executed in the workflow?
Solution
if root.startswith('.\\.'):
is platform-specific, as it uses backslashes to check for directory paths.
However, backslashes are directory separators on Windows, whereas the GitHub Actions runner is set to use Ubuntu, where the directory separator is a forward slash (/
).
Also, os.walk
will return the relative file paths starting from the directory where you run the script, which might not include the leading ./
.
So, when the script runs on Ubuntu, it sees directories starting with "./"
instead of ".\\"
, and thus the condition does not filter out the .github
hidden directory as you intended.
Answered By - Random Davis
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.