macOS上GitHub自托管Runner运行Python脚本权限不足问题咨询
问题:GitHub自托管Runner执行Python脚本时权限被拒
我部署了GitHub自托管Runner,使用Python自动化执行Unity测试,已通过.env文件配置本地变量以便后续将Runner和脚本部署到任意设备。本地终端手动运行脚本一切正常,但通过GitHub Actions触发脚本时出现权限问题,想请教是否有方法提升Runner权限,或是我遗漏了某个步骤?
错误信息
##[debug]Result: '$SELF_HOSTED_UNITY_TEST_SCRIPT {branch_name}' ##[debug]Loading env Run $SELF_HOSTED_UNITY_TEST_SCRIPT pythonmacos $SELF_HOSTED_UNITY_TEST_SCRIPT pythonmacos shell: /bin/bash -e {0} ##[debug]/bin/bash -e {user_path}/actions-runner/_work/_temp/129fb3d0-cb43-49b5-b558-9f36543c0193.sh {user_path}/actions-runner/_work/_temp/129fb3d0-cb43-49b5-b558-9f36543c0193.sh: line 1: {user_path}/dev-tools/test-runner/GithubActions/RunTests.py: Permission denied Error: Process completed with exit code 1. ##[debug]Finishing: Mac - Run testing script
相关代码
Python脚本(RunTests.py)开头部分
if __name__ == '__main__': load_dotenv(find_dotenv()) git_project_path = os.environ.get("GIT_PROJECT_PATH") parser = init_argparse() args = parser.parse_args() branch_name = args.branch_name[0] if not branch_name: exit(2)
GitHub Actions工作流脚本
name: Testing on Self Hosted Runner run-name: ${{ github.actor }} is running a test on a self hosted runner on branch ${{ github.head_ref }}. on: pull_request jobs: Create: runs-on: self-hosted steps: - name: Mac - Setup alias if: runner.os == 'macOS' run: | shopt -s expand_aliases - name: Mac - Run testing script if: runner.os == 'macOS' run: $SELF_HOSTED_UNITY_TEST_SCRIPT ${{ github.head_ref }}
RunTests.py导入内容
import argparse import os import subprocess from os.path import join, dirname from xml.etree import ElementTree import git from dotenv import load_dotenv, find_dotenv from termcolor import cprint
解决方案
给脚本添加可执行权限:在Runner所在的Mac设备上,执行命令:
chmod +x {user_path}/dev-tools/test-runner/GithubActions/RunTests.py也可以在工作流中添加一步自动设置权限:
- name: Grant execute permission to script if: runner.os == 'macOS' run: chmod +x {user_path}/dev-tools/test-runner/GithubActions/RunTests.py直接用Python解释器调用脚本:避免依赖别名和脚本可执行权限,修改工作流中的执行命令:
- name: Mac - Run testing script if: runner.os == 'macOS' run: python3 {user_path}/dev-tools/test-runner/GithubActions/RunTests.py ${{ github.head_ref }}检查Runner用户权限:确认运行GitHub Runner的用户对脚本文件及所在目录拥有读和执行权限。可以在工作流中添加步骤查看权限详情:
- name: Check script permissions if: runner.os == 'macOS' run: ls -l {user_path}/dev-tools/test-runner/GithubActions/RunTests.py添加shebang行:在RunTests.py的第一行添加以下内容,明确指定Python解释器路径:
#!/usr/bin/env python3配合前面的可执行权限设置,让系统能正确识别脚本的执行方式。
内容的提问来源于stack exchange,提问作者Maximilian Hung
相关产品推荐
相关产品推荐

