在Docker中执行gh pr create遇EOF错误,求解决方案
问题分析与解决办法
1. 修复引号嵌套导致的EOF错误
你的命令中sh -c外层使用双引号,内部--title/--body的双引号与外层冲突,导致shell解析时提前截断命令,触发EOF异常。两种修复方式:
- 将外层双引号替换为单引号,内部保留双引号:
nsenter --mount=/host/proc/1/ns/mnt sh -c 'cd /home/..USER../Desktop/..PROJECT.. && export https_proxy=..PROXY../ && gh pr create --title "test" --body "test" --base cicd_test' - 对内部双引号进行转义:
nsenter --mount=/host/proc/1/ns/mnt sh -c "cd /home/..USER../Desktop/..PROJECT..&&export https_proxy=..PROXY../&&gh pr create --title \"test\" --body \"test\" --base cicd_test"
2. 确保gh命令在非交互式环境正常运行
CI/CD容器属于非交互式环境,gh pr create默认可能等待用户输入,需补充必要参数并完成认证:
- 提前配置GitHub认证令牌(令牌需拥有repo权限):
在执行gh命令前添加:export GH_TOKEN="your-github-personal-access-token" - 补充
--head参数指定分支,并添加--no-maintainer-edit禁用交互提示:gh pr create --title "test" --body "test" --base cicd_test --head your-branch-name --no-maintainer-edit
3. 替代方案:容器内直接安装gh执行
避免nsenter的环境依赖问题,直接在CI/CD容器内部署GitHub CLI:
- 安装gh(以Ubuntu容器为例):
apt update && apt install -y gh - 完成认证:
echo "$GH_TOKEN" | gh auth login --with-token - 执行PR创建命令:
cd /path/to/your/project export https_proxy=..PROXY../ gh pr create --title "test" --body "test" --base cicd_test --head your-branch-name --no-maintainer-edit
4. 额外检查项
- 验证nsenter执行用户是否有权限访问主机的
/host/proc/1/ns/mnt及项目目录; - 测试代理有效性:执行
curl -x https://..PROXY../ https://api.github.com,确认能正常访问GitHub API。
内容的提问来源于stack exchange,提问作者熊浩茗
相关产品推荐
相关产品推荐

