travis: sh: 0: 无法打开 /etc/init.d/xvfb

travis: sh: 0: Can't open /etc/init.d/xvfb

我的 travis CI 使用 Ubuntu 14.04 和 Node.js 8。我的 .travis.yml 看起来像:

language: node_js
node_js:
  - 8
sudo: required
addons:
    chrome: stable
before_script:
  - export DISPLAY=:99.0
  - sh -e /etc/init.d/xvfb start
install:
  - npm set progress=false
  - npm install
script:
  - ng lint
  - npm run test
  - npm run e2e
  - npm run build

我尝试将其更新为使用 Ubuntu 16.04 和 Node.js 10,方法是将其更改为:

language: node_js
node_js:
  - '10'
dist: xenial
sudo: required
addons:
    chrome: stable
before_script:
  - export DISPLAY=:99.0
  - sh -e /etc/init.d/xvfb start
install:
  - npm set progress=false
  - npm install
script:
  - ng lint
  - npm run test
  - npm run e2e
  - npm run build

但是现在我在尝试启动时遇到错误 xvfb:

0.00s$ sh -e /etc/init.d/xvfb start

sh: 0: Can't open /etc/init.d/xvfb

The command "sh -e /etc/init.d/xvfb start" failed and exited with 127 during .

解决方案是从 before_script 数组中删除 sh -e /etc/init.d/xvfb start 并在 services 数组中简单地引入 xvfb

所以我的 .travis.yml 现在看起来像这样:

language: node_js
node_js:
  - '10'
dist: xenial
sudo: required
services:
  - xvfb
addons:
    chrome: stable
before_script:
  - export DISPLAY=:99.0
install:
  - npm set progress=false
  - npm install
script:
  - ng lint
  - npm run test
  - npm run e2e
  - npm run build

除了已接受的答案外,我认为根据 travis 文档 Using xvfb to Run Tests That Require a GUI,结果配置应该更清晰一些。您不需要设置 DISPLAY,因此 before_script 部分变得过多:

This only works on Ubuntu 16.04 (Xenial) and later on releases i.e. with dist: xenial or dist: bionic
The following will start xvfb and set the right values for the DISPLAY environment variable...

此外,您无需在 2019 年为 node_js 指定 dist,因为 xenial 现在是 node_js 语言 (blogpost). Previously it was trusty, so another possible solution might be in specifying dist as trusty (see the doc) 的默认图像。但说到默认,讨论的配置可能看起来像

language: node_js
node_js:
  - '10'
sudo: required
services:
  - xvfb
addons:
    chrome: stable
install:
  - npm set progress=false
  - npm install
script:
  - ng lint
  - npm run test
  - npm run e2e
  - npm run build