部署阶段出错在 AWS 代码管道的日志中找不到错误

问题描述

我的 bitbucket 中有 reactjs 项目。我正在尝试在我的 EC2 服务器中部署它,但在部署阶段遇到错误。并且在检查错误时......它没有按照figute所示进行部署......我正在与你分享文件错误......你们能帮我吗?

buildspec.yml

    # Do not change version. This is the version of aws buildspec,not the version of your buldspec file.
version: 0.2
phases:
  pre_build:
    commands:
      #installs dependencies into the node_modules/ directory
      - npm install
  build:
    commands:
      - echo Build started on `date`
      - echo Compiling
      - npm run build
  post_build:
    commands:
      - echo Build completed on `date`
# Include only the files required for your application to run.
artifacts:
  files:
    - public/**/*
    - src/**/*
    - package.json
    - appspec.yml
    - scripts/**/*

appspec.yml

    version: 0.0
os: linux

files:
  - source: /
    destination: /home/ec2-user/server

permissions:
  - object: /
    pattern: "**"
    owner: ec2-user
    group: ec2-user 

hooks:

  BeforeInstall:
    - location: scripts/before_install.sh
      timeout: 300
      runas: root
  
  AfterInstall:
    - location: scripts/after_install.sh
      timeout: 300
      runas: root

  ApplicationStart:
    - location: scripts/app_start.sh
      timeout: 300
      runas: root

before_install.sh

   #!/bin/bash
cd /home/ec2-user/server
curl -sL https://rpm.nodesource.com/setup_14.x | sudo -E bash -
yum -y install nodejs npm

after_install.sh

#!/bin/bash
cd /home/ec2-user/server
npm install
npm install --save react react-dom react-scripts react-particles-js
npm install pm2 -g

app_start.sh

  #!/bin/bash
cd /home/ec2-user/server/src
npm start
pm2 start npm --name "econote-dashboard" -- start
pm2 startup
pm2 save
pm2 restart all

错误 指定位置的脚本:scripts/app_start.sh 未能在 300 秒内完成

Logs
[stdout] Line 41:12: Effect callbacks are synchronous to prevent race conditions. Put the async function inside:
[stdout]
[stdout]useEffect(() => {
[stdout] async function fetchData() {
[stdout] // You can await here
[stdout] const response = await MyAPI.getData(someId);
[stdout] // ...
[stdout] }
[stdout] fetchData();
[stdout]},[someId]); // Or [] if effect doesn't need props or state
[stdout]
[stdout]Learn more about data fetching with Hooks: https://reactjs.org/link/hooks-data-fetching react-hooks/exhaustive-deps
[stdout]
[stdout]src/components/Cards.js
[stdout] Line 4:9: 'imageClick' is assigned a value but never used no-unused-vars
[stdout] Line 7:3: Anchors must have content and the content must be accessible by a screen reader jsx-a11y/anchor-has-content
[stdout] Line 13:6: Using target="_blank" without rel="noreferrer" is a security risk: see https://html.spec.whatwg.org/multipage/links.html#link-type-noopener react/jsx-no-target-blank
[stdout]
[stdout]src/components/Dashboard.js
[stdout] Line 2:23: 'EpubView' is defined but never used no-unused-vars
[stdout]
[stdout]src/components/login.js
[stdout] Line 30:24: The href attribute requires a valid value to be accessible. Provide a valid,navigable address as the href value. If you cannot provide a valid href,but still need the element to resemble a link,use a button and change it with appropriate styles. Learn more: https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/anchor-is-valid.md jsx-a11y/anchor-is-valid
[stdout]
[stdout]Search for the keywords to learn more about each warning.
[stdout]To ignore,add // eslint-disable-next-line to the line before.
[stdout]
[stderr]Terminated

解决方法

您可以从 EC2 本身查看部署日志。

部署日志位于:

/opt/codedeploy-agent/deployment-root/deployment-logs/codedeploy-agent-deployments.log

可以从以下位置找到单独的部署日志。将 deployment-group-IDdeployment-ID 替换为您的实际值。

/opt/codedeploy-agent/deployment-root/deployment-group-ID/deployment-ID/logs/scripts.log

您可以从 here 阅读有关 CodeDeploy 日志管理的更多信息。还有一个选项可以使用 CloudWatch 日志代理从 CloudWatch 控制台查看部署日志。有关详细信息,请参阅 this AWS 博客

,

编辑:在使用调试后我们看到脚本的更多输出后(见下文)

从输出看来,您的应用程序无法编译,因为 eslint 发现错误。如果我没记错的话,这是使用 create-react-app 创建的应用程序的默认行为。

我建议您先尝试在开发环境中修复这些错误。只需通过运行 app_start 脚本启动应用程序即可。如果您遇到需要帮助的错误,请尝试在 SO 上提出新的、具体的问题。

(可选地,如果您不想修复 lint 错误,您可以通过将 // eslint-disable-next-line 添加到之前的行来告诉 eslint 忽略一行,就像脚本提到的那样)。

遵循调试提示

不是直接回答您的问题,但您可以考虑将其添加到您的 bash 脚本中以查看更多输出:

#!/bin/bash
set -ex

Set -e 会在出现错误时停止您的脚本,-x 会在运行之前打印出每个命令,以便您获得一些堆栈跟踪。

另请参阅:https://stackoverflow.com/a/19622300/904465https://stackoverflow.com/a/36273740/904465,了解有关 set -eset -x 的更多信息。