`/bin/sh` 未在 docker 内的 Jenkins 中找到毒物

`/bin/sh` not found in tox within Jenkins inside docker

我正在尝试在 docker 容器中设置 运行s tox 的 Jenkins 管道。有一个已知问题,即 Jenkins 内部的 shebang 行变得很长,并且 two solutions are proposed。第一种是使用 --workdir 到 select 更短的路径。此选项原则上有效,但我从 Jenkins 中取消了每个项目的自动唯一路径名。因此,我更愿意使用第二个选项,TOX_LIMITED_SHEBANG。不幸的是,当应该安装被测包时,这似乎失败并出现以下错误:FileNotFoundError: [Errno 2] No such file or directory: "b'/bin/sh'": "b'/bin/sh'"。我已经验证 /bin/sh 实际上在 docker 容器中可用。 Jenkinsfile 如下所示:

node("docker") {
    // burnpanck/tox-base contains tox and many python versions
    docker.image('burnpanck/tox-base').inside {
        checkout scm
        stage('Matrix-test using Tox') {
            // verify that /bin/sh exists
            sh 'ls -al /bin'
            // the following does not work
            sh 'TOX_LIMITED_SHEBANG=1 tox -vv'
            // the following works 
            // sh 'tox --workdir=/var/jenkins_home/tox'
        }
    }
}

Tox 是版本 3.1.2 和 运行s 在 python 3.6 下(docker 图像是从这个 Dockerfile 生成的)。让我有点吃惊的是 "b'/bin/sh'" 来自 str-ing 一个 bytes 实例。难道 tox 实际上是在尝试 运行 路径 b'/bin 中名为 sh' 的程序?

使用中的tox.ini只是调用pytest:

[tox]
envlist = py36
[testenv]
recreate = True
commands =
    pytest

tox 的完整回溯(Jenkins 控制台输出)如下:

py36 create: /var/jenkins_home/workspace/_debug_jenkins-long-shebang-L7UHBNCVPSOBSTKZ7COPFJBJLWR5XZXFIAD7TBGC4WQLVDLZYVQQ/.tox/py36
py36 inst: /var/jenkins_home/workspace/_debug_jenkins-long-shebang-L7UHBNCVPSOBSTKZ7COPFJBJLWR5XZXFIAD7TBGC4WQLVDLZYVQQ/.tox/dist/test_model-0.dev20180717.zip
ERROR: invocation failed (errno 2), args: [b'/bin/sh', '/var/jenkins_home/workspace/_debug_jenkins-long-shebang-L7UHBNCVPSOBSTKZ7COPFJBJLWR5XZXFIAD7TBGC4WQLVDLZYVQQ/.tox/py36/bin/pip', 'install', '/var/jenkins_home/workspace/_debug_jenkins-long-shebang-L7UHBNCVPSOBSTKZ7COPFJBJLWR5XZXFIAD7TBGC4WQLVDLZYVQQ/.tox/dist/test_model-0.dev20180717.zip'], cwd: /var/jenkins_home/workspace/_debug_jenkins-long-shebang-L7UHBNCVPSOBSTKZ7COPFJBJLWR5XZXFIAD7TBGC4WQLVDLZYVQQ
Traceback (most recent call last):
  File "/.pyenv/versions/3.6.6/bin/tox", line 11, in <module>
    sys.exit(cmdline())
  File "/.pyenv/versions/3.6.6/lib/python3.6/site-packages/tox/session.py", line 39, in cmdline
    main(args)
  File "/.pyenv/versions/3.6.6/lib/python3.6/site-packages/tox/session.py", line 45, in main
    retcode = Session(config).runcommand()
  File "/.pyenv/versions/3.6.6/lib/python3.6/site-packages/tox/session.py", line 422, in runcommand
    return self.subcommand_test()
  File "/.pyenv/versions/3.6.6/lib/python3.6/site-packages/tox/session.py", line 620, in subcommand_test
    self.installpkg(venv, path)
  File "/.pyenv/versions/3.6.6/lib/python3.6/site-packages/tox/session.py", line 561, in installpkg
    venv.installpkg(path, action)
  File "/.pyenv/versions/3.6.6/lib/python3.6/site-packages/tox/venv.py", line 277, in installpkg
    self._install([sdistpath], extraopts=extraopts, action=action)
  File "/.pyenv/versions/3.6.6/lib/python3.6/site-packages/tox/venv.py", line 342, in _install
    self.run_install_command(packages=packages, options=options, action=action)
  File "/.pyenv/versions/3.6.6/lib/python3.6/site-packages/tox/venv.py", line 314, in run_install_command
    redirect=self.session.report.verbosity < 2,
  File "/.pyenv/versions/3.6.6/lib/python3.6/site-packages/tox/venv.py", line 427, in _pcall
    return action.popen(args, cwd=cwd, env=env, redirect=redirect, ignore_ret=ignore_ret)
  File "/.pyenv/versions/3.6.6/lib/python3.6/site-packages/tox/session.py", line 153, in popen
    popen = self._popen(args, cwd, env=env, stdout=stdout, stderr=subprocess.STDOUT)
  File "/.pyenv/versions/3.6.6/lib/python3.6/site-packages/tox/session.py", line 248, in _popen
    env=env,
  File "/.pyenv/versions/3.6.6/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/.pyenv/versions/3.6.6/lib/python3.6/subprocess.py", line 1344, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: "b'/bin/sh'": "b'/bin/sh'"

我们目前有 an open bug 个。当 tox 在 python3 下 运行 时 TOX_LIMITED_SHEBANG 被破坏。

根本原因是 .decode() 缺失,列表最终包含 bytesstr 个实例。

I fix it 之前的解决方法是将 tox 与 python2 解释器一起使用,或者不使用 TOX_LIMITED_SHEBANG