Execute a script file when comment on a PR that introduces the file to be executed #170792
-
Why are you starting this discussion?Question What GitHub Actions topic or product is this about?Workflow Configuration Discussion DetailsHello, |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
|
Hey there! Thanks for sharing your question and workflow file—I’m happy to help you figure out why your GitHub Actions workflow is failing with the "no such file or directory" error when trying to run script.py on a pull request (PR) comment saying "execute." Since you’re still practicing with GitHub Actions (same here, it’s a learning curve!), I’ll break down what’s likely causing the issue, suggest fixes, and explain how to improve your workflow based on my own experience and the GitHub Actions docs.What’s Causing the Issue?From your description and workflow file, the error "no such file or directory" when running python3 script.py suggests that the script.py file, which is being added or modified in the PR, isn’t available in the workflow’s file system when the workflow runs. This is a common issue with GitHub Actions when dealing with PRs, because the default actions/checkout@v4 step checks out the base branch of the PR (e.g., main), not the changes in the PR’s branch (where script.py exists). Let’s dive into fixing this and making sure your workflow runs as intended.Why the Workflow FailsYour workflow is triggered when a comment like "execute" is added to a PR, and it uses actions/checkout@v4 to fetch the repository. By default, this action checks out the commit at the head of the base branch (e.g., main), not the PR’s branch where script.py is added. So, when the run-script step tries to execute python3 script.py, the file isn’t found because it only exists in the PR’s branch, not in the base branch.How to Fix the WorkflowTo make sure script.py is available, you need to check out the PR’s branch (the head ref) instead of the base branch. Here’s how you can modify your workflow, along with some improvements to make it more robust:yaml name: run-file-on-comment Key Changes and ExplanationsCheckout the PR’s Branch:Added ref: ${{ github.event.pull_request.head.sha }} to the actions/checkout@v4 step. This tells the action to check out the specific commit from the PR’s branch (the head SHA), ensuring script.py is available in the workflow’s file system. Updated actions/setup-python to @v5:I upgraded to the latest version (@v5) for better compatibility and support. This ensures your Python setup is using the most recent features and bug fixes. Added contents: read Permission:Your workflow already includes pull-requests: write, but I added contents: read to ensure the workflow can read the repository’s files (like script.py). This follows GitHub’s best practices for least privilege. Kept Your Logic Intact:The if condition (github.event.issue.pull_request && github.event.comment.body == 'execute') is solid for triggering the workflow only on PR comments saying "execute." I didn’t change this since it’s working as intended. Additional Tips to Avoid IssuesVerify script.py Path: If script.py is in a subdirectory (e.g., src/script.py), update the run-script step to include the correct path:yaml run: python3 src/script.py Double-check the file’s location in the PR’s branch to avoid path-related errors. pip install -r requirements.txt || echo "No requirements.txt found" Test the Workflow: After updating the workflow, add a comment saying "execute" to your PR to trigger it. Check the Actions tab in your repo to see if it runs successfully. If it fails, the logs will show details (e.g., if another file is missing).
Place this before the run-script step to verify the file exists. My Thoughts as Someone Learning GitHub ActionsSince you mentioned you’re still practicing with GitHub Actions, I totally get how tricky it can be to figure out why something like this fails! I ran into a similar issue when I was trying to run a script in a PR, and it was confusing until I learned that actions/checkout doesn’t automatically grab the PR’s changes. Adding the ref field was a game-changer for me. Also, GitHub’s Actions documentation and community forums were super helpful for troubleshooting.If It Still Doesn’t WorkIf you apply these changes and still get errors, here are a couple of things to check:PR Context: Ensure the PR is open and the comment is exactly "execute" (case-sensitive). You can make it case-insensitive by tweaking the if condition:yaml if: ${{ github.event.issue.pull_request && contains(github.event.comment.body, 'execute') }} File Existence: If script.py is dynamically generated or in a weird path, the workflow might still fail. Share the full error log from the Actions tab, and I can help pinpoint the issue. I hope this helps get your workflow running smoothly! If you run into more errors or want me to review the updated workflow, just share the details, and I’ll dive in with you. Keep practicing—GitHub Actions gets easier with time! |
Beta Was this translation helpful? Give feedback.
-
|
Your workflow is checking out the default branch, but your Try this version: name: run-file-on-comment
on:
issue_comment:
types: [created]
jobs:
run-script:
# 1) Only run on PR comments
# 2) Only when the comment body is exactly "execute" (case-insensitive, trimmed)
# 3) Only when the commenter is a repo member/collaborator (security)
if: >
github.event.issue.pull_request &&
startsWith(trim(github.event.comment.body), 'execute') &&
(github.event.comment.author_association == 'MEMBER' ||
github.event.comment.author_association == 'OWNER' ||
github.event.comment.author_association == 'COLLABORATOR')
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
# ✅ Check out the PR branch (so new files like script.py exist)
- name: Checkout PR HEAD
uses: actions/checkout@v4
with:
# Use the PR number from the issue_comment payload
ref: refs/pull/${{ github.event.issue.number }}/head
fetch-depth: 1
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint (optional)
run: |
pip install flake8
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Run script
run: |
python3 script.pyWhy this works
Notes
Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
|
@Argh94 and @jihanurrahman33 thank you for the hints and the explanation! |
Beta Was this translation helpful? Give feedback.
Hi @damianomuzzolon2,
Your workflow is checking out the default branch, but your
script.pyonly exists on the PR branch. Forissue_comment-triggered jobs, you need to explicitly check out the PR’s ref.Try this version: