所以,让我首先在这里给出整个背景:
-> 我们有 cypress UI end2end 测试套件,我们希望每晚运行它,作为 Jenkins 每晚构建的一部分。我们正在无头模式下运行 cypress 测试,但已经通过了
--record
标签,以便我们将视频录制保存到仪表板中。
-> 作为 jenkins 管道步骤的一部分,我们正在 docker 镜像中运行 cypress 测试。
这是
cypress_run.sh
文件,用作在 jenkins 中运行测试的起点:
# Update package list
apt-get update
apt-get install apache2
# Install NVM (Node Version Manager)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
# Load NVM
export NVM_DIR="$HOME/.nvm"
. "$NVM_DIR/nvm.sh"
# Use NVM to install the specified version of Node.js
nvm install 20
# Use npm to install the specified version of npm
npm install -g npm@9
# Display versions for verification
echo "NVM version: \$(nvm --version), Node version: \$(node --version), npm version: \$(npm --version)"
apt-get update
apt-get install libgtk2.0-0 libgtk-3-0 libgbm-dev libnotify-dev libnss3 libxss1 libasound2 libxtst6 xauth xvfb
echo "Running Cypress Command"
npm install --legacy-peer-deps
# Install Xvfb
apt-get install -y xvfb
# Start Xvfb
Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
# Set the display environment variable
export DISPLAY=:99
echo "Installed NPM Packages!"
export CYPRESS_BASE_URL="$UNIFIED_URL"
export CYPRESS_USERNAME="admin"
export CYPRESS_PASSWORD="password"
echo "Cypress base Url: ${CYPRESS_BASE_URL}"
npx cypress run --record --key <KEY> --browser chrome --headless --config-file cypress.config.js --config baseUrl="$CYPRESS_BASE_URL" username="$CYPRESS_USERNAME" password="$CYPRESS_PASSWORD"
现在,这段代码的整个设置在大多数情况下都运行良好,但偶尔它会开始在 jenkins 上抛出以下错误,然后我无法解决这个问题:
[2024-04-01T18:58:54.001Z] Cypress base Url: https://mondaytestingcypress.jfrogdev.org
[2024-04-01T18:58:54.001Z] [STARTED] Task without title.
[2024-04-01T18:58:54.559Z] [FAILED] Cypress failed to start.
[2024-04-01T18:58:54.559Z] [FAILED]
[2024-04-01T18:58:54.559Z] [FAILED] This may be due to a missing library or dependency. https://on.cypress.io/required-dependencies
[2024-04-01T18:58:54.559Z] [FAILED]
[2024-04-01T18:58:54.559Z] [FAILED] Please refer to the error below for more details.
[2024-04-01T18:58:54.559Z] [FAILED]
[2024-04-01T18:58:54.559Z] [FAILED] ----------
[2024-04-01T18:58:54.559Z] [FAILED]
[2024-04-01T18:58:54.559Z] [FAILED] [1606:0401/185854.356170:ERROR:ozone_platform_x11.cc(240)] Missing X server or $DISPLAY
[2024-04-01T18:58:54.560Z] [FAILED] [1606:0401/185854.356223:ERROR:env.cc(255)] The platform failed to initialize. Exiting.
[2024-04-01T18:58:54.560Z] [FAILED]
[2024-04-01T18:58:54.560Z] [FAILED] ----------
[2024-04-01T18:58:54.560Z] [FAILED]
[2024-04-01T18:58:54.560Z] [FAILED] Platform: linux-x64 (Debian - 11.8)
[2024-04-01T18:58:54.560Z] [FAILED] Cypress Version: 12.17.4
[2024-04-01T18:58:54.560Z] Cypress failed to start.
[2024-04-01T18:58:54.560Z]
[2024-04-01T18:58:54.560Z] This may be due to a missing library or dependency. https://on.cypress.io/required-dependencies
[2024-04-01T18:58:54.560Z]
[2024-04-01T18:58:54.560Z] Please refer to the error below for more details.
[2024-04-01T18:58:54.560Z]
[2024-04-01T18:58:54.560Z] ----------
[2024-04-01T18:58:54.560Z]
[2024-04-01T18:58:54.560Z] [1606:0401/185854.356170:ERROR:ozone_platform_x11.cc(240)] Missing X server or $DISPLAY
[2024-04-01T18:58:54.560Z] [1606:0401/185854.356223:ERROR:env.cc(255)] The platform failed to initialize. Exiting.
[2024-04-01T18:58:54.560Z]
[2024-04-01T18:58:54.560Z] ----------
[2024-04-01T18:58:54.560Z]
[2024-04-01T18:58:54.560Z] Platform: linux-x64 (Debian - 11.8)
[2024-04-01T18:58:54.560Z] Cypress Version: 12.17.4
[2024-04-01T18:58:54.560Z] ./cypress_run.sh: line 44: 1582 Aborted (core dumped) Xvfb :"$RANDOM_DISPLAY_PORT_NUMBER" -screen 0 1024x768x24 > /dev/null 2>&1
我尝试了多种方法,暂时解决了问题,但是当它再次出现时,后续所有的jenkins构建都失败了。
-> 我手动硬编码了一个不同的显示端口号,比如说 100,这在有限的时间内解决了问题,但又开始使构建失败。
Xvfb :100 -screen 0 1024x768x24 > /dev/null 2>&1 &
# Set the display environment variable
export DISPLAY=:100
因此,我认为端口号可能是问题所在。因此,在每次构建时,我都会生成给定范围内的随机数,并将端口分配给该数字。
RANDOM_DISPLAY_PORT_NUMBER=$(shuf -i 10-98 -n 1)
# Start Xvfb
Xvfb :"$RANDOM_DISPLAY_PORT_NUMBER" -screen 0 1024x768x24 > /dev/null 2>&1 &
# Set the display environment variable
export DISPLAY=":$RANDOM_DISPLAY_PORT_NUMBER"
echo "DISPLAY PORT NUMBER SELECTED IS: $RANDOM_DISPLAY_PORT_NUMBER"
这解决了很长一段时间的问题,然后我一周都没有遇到这个问题,但构建再次开始失败。现在,无论我手动触发构建多少次,我每次都会收到相同的错误消息。
Cypress 将忽略您的
DISPLAY
变量(以及任何其他环境变量),除非您在其前面添加 CYPRESS_
- 请参阅 cypress 环境变量文档,其中相关部分指出:
在命令行或 CI 提供程序中设置的任何以
或CYPRESS_
开头的导出环境变量都将被 Cypress 自动解析。cypress_
此语句也可以通过逆向推理来理解:任何不带有CYPRESS_
前缀的变量将不会被Cypress解析。您还可以使用任何其他
方法通过 Cypress 设置变量。