【发布时间】:2019-05-17 09:14:58
【问题描述】:
我编写了一个小 Python 脚本来构建和测试我的那个项目的 docker 镜像。该脚本在本地运行良好,但在 check_output 期间远程失败。
构建.py
print(check_output(["which", "dgoss"]))
result = str(check_output(["dgoss", "run", image_tag], cwd=image_path, stderr=subprocess.DEVNULL))
.gitlab-ci.yml
before_script:
- curl -fsSL https://goss.rocks/install | sh
- ls -la /usr/local/bin/ | grep goss
script:
- which dgoss
- python3 ./infrastructure/build.py
输出
dgoss master has been installed to /usr/local/bin/dgoss
$ ls -la /usr/local/bin/ | grep goss
-rwxr-xr-x 1 root root 3803 May 17 08:58 dgoss
-rwxr-xr-x 1 root root 8524064 May 17 08:58 goss
$ which dgoss
/usr/local/bin/dgoss
$ python3 ./infrastructure/build.py
infrastructure/broker
b'/usr/local/bin/dgoss\n'
Traceback (most recent call last):
File "./infrastructure/build.py", line 54, in <module>
test()
File "./infrastructure/build.py", line 42, in test
result = str(check_output(["dgoss", "run", image_tag], cwd=image_path, stderr=subprocess.DEVNULL))
File "/usr/lib/python3.6/subprocess.py", line 356, in check_output
**kwargs).stdout
File "/usr/lib/python3.6/subprocess.py", line 423, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/lib/python3.6/subprocess.py", line 729, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.6/subprocess.py", line 1364, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'dgoss': 'dgoss'
文件存在且可执行。 python为什么会抛出异常?
【问题讨论】:
-
check_output做什么?明明找了一个文件名dgoss,找不到,这个文件是什么? -
您应该尝试使用完整路径调用
dgoss命令。像这样:result = str(check_output([str(check_output(["which", "dgoss"])).strip(), "run", image_tag],...当然,你可以为dgoss的路径创建一个分隔变量。 -
试过完整路径,也没用。
标签: python python-3.x subprocess gitlab-ci gitlab-ci-runner