将警告视为错误,因为 process.env.CI = true。编译失败

问题描述 投票:0回答:14

我正在为自学目的开发一个反应天气应用程序。 在 gh-pages 中部署相同。
网址
https://davisraimon.github.io/react-weather/
回购
https://github.com/davisraimon/react-weather

当尝试将我的应用程序与 Travis Ci 集成时,出现如下错误。 它说我必须更改一些名为 Process.env.CI.

的环境变量
$ git clone --depth=50 --branch=master https://github.com/davisraimon/react-weather.git davisraimon/react-weather
nvm.install
4.18s$ nvm install stable
cache.1
Setting up build cache
cache.npm
$ node --version
v14.4.0
$ npm --version
6.14.5
$ nvm --version
0.35.3
install.npm
13.21s$ npm ci 
7.45s$ npm run build
> [email protected] build /home/travis/build/davisraimon/react-weather
> react-scripts build
Creating an optimized production build...
Treating warnings as errors because process.env.CI = true.
Most CI servers set it automatically.
Failed to compile.
./src/components/form.component.js
  Line 1:17:  'Component' is defined but never used  no-unused-vars
./src/App.js
  Line 2:8:    'logo' is defined but never used              no-unused-vars
  Line 8:7:    'API_key' is assigned a value but never used  no-unused-vars
  Line 37:5:   Expected a default case                       default-case
  Line 53:14:  Expected '===' and instead saw '=='           eqeqeq
  Line 69:20:  Expected '===' and instead saw '=='           eqeqeq
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build: `react-scripts build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     /home/travis/.npm/_logs/2020-06-30T17_45_07_887Z-debug.log
The command "npm run build" exited with 1.
cache.2
store build cache

我在 .travis.yml 文件中添加了 env 变量。

env:
    process.env.CI : false

仍然显示相同的错误。

谁能帮我摆脱这种情况...

reactjs deployment continuous-integration travis-ci github-pages
14个回答
75
投票
  "scripts": {
    "start": "react-scripts start",
    "build": "CI=false && react-scripts build",  // Add CI=False here
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

28
投票

对我来说,解决方案是:

env: 
  CI: false

14
投票

如果您必须从 GitLab CICD 管道中继续构建和部署,您可以像这样包含 CI=false:

CI=false npm run build

unset CI
npm run build

这里是一个完整的 gitlab_ci 作业示例:

build-dev:
  environment: DEV
  image: node:16
  stage: build
  script:
    - npm install
    - CI=false npm run build
  only:
    - /^devrelease-.*$/
  tags:
    - docker

build-dev:
  environment: DEV
  image: node:16
  stage: build
  script:
    - unset CI
    - npm install
    - npm run build
  only:
    - /^devrelease-.*$/
  tags:
    - docker
  

9
投票

对我来说用

npm run build
替换
CI='' npm run build
是有效的。


9
投票

在我的具体案例中,我使用的是 Netlify,所以我所做的就是在 Netlify 的面板中设置 ENV 变量:

  • 选择站点设置选项卡
  • 选择构建和部署
  • 向下滚动到环境变量然后按编辑变量
  • 填写 Key = CI 和 Value = false
  • 按保存并触发新的部署


2
投票

按照以下步骤,您的问题将得到解决:

  • 选择站点设置选项卡
  • 选择构建和部署
  • 向下滚动到环境变量选择编辑变量
  • 填写 Key = CI 和 Value = false
  • 按清除缓存并重新部署

...玩得开心。


2
投票

发生这种情况是由于您的 CI 服务器设置(在我的例子中是 netlify)。按照以下步骤操作,您的问题将得到解决:

选择-->

Site Settings
选项卡

选择-->

Build and Deploy

向下滚动到环境变量选择

Edit Variables

填写

Key = CI
Value = false

clear cache and redeploy


1
投票

CI=true 如果您想在部署过程中处理测试,但可以通过 package.json 设置标志轻松存档(如果您的 dockerfile 已经设置 ENV CI=true,您可以将其删除)

 "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "CI=true react-scripts test"
  }

1
投票

经过数小时的谷歌搜索,对我来说,以下内容适用于Azure Devops

在你的 package.json 中

"scripts": {
  ...
  "build": "set \"CI=false\" && react-scripts build
}

注意:转义引号很重要!


0
投票

就我而言,我试图通过 GitHub 存储库将 React 应用程序部署到 Azure 静态网页 这 环境: CI:假

很有魅力。 只是一个简短的说明。这适用于在 GitHub 操作中定义 CI 流程的 yaml。 env 元素没有缩进(与 name 处于同一级别:)


0
投票

在 yml 文件中的 Gitlab CI 作业中,在我的作业中添加以下脚本对我有用。

script:
- CI=false yarn run build

0
投票

你必须修复你在反应中的警告,这就是为什么它不允许你部署


0
投票

如果您想部署到windows服务器或使用powershell,您可以像这样设置环境变量:

script:
  - $env:CI="false"
  - npm run build

或:

$env:CI="false"; npm run build

0
投票

我正在使用 gitlab ci & yarn,这对我有用:

  script: 
    - CI=false yarn build

© www.soinside.com 2019 - 2024. All rights reserved.