代码构建失败,执行命令时出错:npm install,原因:退出状态127

s6fujrry  于 11个月前  发布在  其他
关注(0)|答案(5)|浏览(185)

我用非常简单的代码创建了一个代码管道,并连接到codecommit。试图构建它,但它在codebuild步骤失败,说明错误执行npm install。我错过了什么吗?对不起,我是这个codebuild/ codepipeline的新手。
下面是代码构建失败的日志:

[Container] 2019/02/15 11:47:39 Waiting for agent ping 
[Container] 2019/02/15 11:47:40 Waiting for DOWNLOAD_SOURCE 
[Container] 2019/02/15 11:47:40 Phase is DOWNLOAD_SOURCE 
[Container] 2019/02/15 11:47:40 CODEBUILD_SRC_DIR=/codebuild/output/src501317273/src 
[Container] 2019/02/15 11:47:40 YAML location is /codebuild/output/src501317273/src/buildspec.yml 
[Container] 2019/02/15 11:47:40 Processing environment variables 
[Container] 2019/02/15 11:47:40 Moving to directory /codebuild/output/src501317273/src 
[Container] 2019/02/15 11:47:40 Registering with agent 
[Container] 2019/02/15 11:47:40 Phases found in YAML: 1 
[Container] 2019/02/15 11:47:40 BUILD: 2 commands 
[Container] 2019/02/15 11:47:40 Phase complete: DOWNLOAD_SOURCE Success: true 
[Container] 2019/02/15 11:47:40 Phase context status code: Message: 
[Container] 2019/02/15 11:47:40 Entering phase INSTALL 
[Container] 2019/02/15 11:47:40 Phase complete: INSTALL Success: true 
[Container] 2019/02/15 11:47:40 Phase context status code: Message: 
[Container] 2019/02/15 11:47:40 Entering phase PRE_BUILD 
[Container] 2019/02/15 11:47:40 Phase complete: PRE_BUILD Success: true 
[Container] 2019/02/15 11:47:40 Phase context status code: Message: 
[Container] 2019/02/15 11:47:41 Entering phase BUILD 
[Container] 2019/02/15 11:47:41 Running command npm install 
sh: 1: npm: not found 

[Container] 2019/02/15 11:47:41 Command did not exit successfully npm install exit status 127 
[Container] 2019/02/15 11:47:41 Phase complete: BUILD Success: false 
[Container] 2019/02/15 11:47:41 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: npm install. Reason: exit status 127 
[Container] 2019/02/15 11:47:41 Entering phase POST_BUILD 
[Container] 2019/02/15 11:47:41 Phase complete: POST_BUILD Success: true 
[Container] 2019/02/15 11:47:41 Phase context status code: Message: 
[Container] 2019/02/15 11:47:41 Expanding base directory path: . 
[Container] 2019/02/15 11:47:41 Assembling file list 
[Container] 2019/02/15 11:47:41 Expanding . 
[Container] 2019/02/15 11:47:41 Expanding artifact file paths for base directory . 
[Container] 2019/02/15 11:47:41 Assembling file list 
[Container] 2019/02/15 11:47:41 Expanding post-saml.yaml 
[Container] 2019/02/15 11:47:41 Skipping invalid artifact path post-saml.yaml 
[Container] 2019/02/15 11:47:41 Expanding beta.json 
[Container] 2019/02/15 11:47:41 Found 1 file(s) 
[Container] 2019/02/15 11:47:41 Phase complete: UPLOAD_ARTIFACTS Success: true 
[Container] 2019/02/15 11:47:41 Phase context status code: Message:

字符串
我的buildspec.yml文件看起来像这样:

version: 0.0
environment_variables:
    plaintext:
        "INPUT_FILE": "serverless.yml"
        "S3_BUCKET": ""
containers:
    LambdaFunctions:
        phases:
            during_build:
                commands:
                    - npm install
                    - aws cloudformation package --template $INPUT_FILE --s3-bucket $S3_BUCKET --output-template post-saml.yaml
        artifacts:
            files:
                - post-saml.yaml
                - beta.json

uinbv5nw

uinbv5nw1#

错误消息在日志下面的几行:sh: 1: npm: not found
这意味着npm命令在构建环境中不可用。您是否正确选择了nodejs构建?
当你创建一个构建环境时,你必须指定操作系统和运行时。在这里你不可能不指定nodejs作为运行时。
请参阅这里的分步指南:https://docs.aws.amazon.com/codebuild/latest/userguide/getting-started.html#getting-started-create-build-project
请参阅此处的文档:https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-available.html
下面是我的控制台屏幕截图:

sg2wtvxw

sg2wtvxw2#

只是把这个贴在这里,以防将来有人遇到它。如果更新到下面的BuildSpec Reference,OP buildspec.yaml应该可以工作。
更改日志:

  • 将buildspec更新为版本.2
  • 在安装阶段添加了nodejs依赖项
version: 0.2
env:
  variables:
    INPUT_FILE: "serverless.yml"
    S3_BUCKET: ""
phases:
  install:
    runtime-versions:
      nodejs: 10
    commands:
      - npm install
  build:
    commands:
      - aws cloudformation package --template $INPUT_FILE --s3-bucket $S3_BUCKET --output-template post-saml.yaml
artifacts:
    files:
        - post-saml.yaml
        - beta.json

字符串

cbjzeqam

cbjzeqam3#

我也遇到了同样的问题。要查看实际问题,请仔细检查构建日志。这不是红色显示的错误,而是上面的几行。
(1)与代码构建服务角色的权限有关。我临时分配了管理访问权限,它工作了,但由于不同的原因失败了。要检查它,请尝试只执行简单的cloud-formation命令,如'aws cloudformation list-stacks'(使用内联命令选项尝试它,或者只保留此命令,看看它是否工作。
(2)我的问题是一个sammetemplate文件(我用C#尝试),在你的情况下serverless. yml。它在一个位置创建一个构建文件,但我的sammetemplate引用了不同的位置。如果可能的话,在本地机器上从AWS CLI尝试相同的命令
您可能选择了错误的操作系统/映像等。检查https://docs.aws.amazon.com/codebuild/latest/userguide/getting-started-create-build-project-console.html

hk8txs48

hk8txs484#

添加,因为它可能会帮助别人.我收到一个错误,读:

/codebuild/output/tmp/script.sh: 4: ‘npm: not found

字符串
原来是因为引号的缘故。

yi0zb3m4

yi0zb3m45#

在我的例子中,我有以下buildspec.yml:

version: 0.2
phases:
  install:
    runtime-versions:
      nodejs: 18
    commands:
      - npm install
  build:
    commands:
      - npm run build
artifacts:
  files:
    - 'dist/**/*'
    - 'node_modules**/*'
    - 'package.json'
    - 'package-lock.json'

字符串
Amazon Linux镜像不支持节点18(截至2023年10月)。因此,我不得不将镜像更改为Linux Standard 7(您可以检查每个镜像here。每个镜像中都有一个runtimes.yml文件,说明该镜像中可用的每种语言的版本。
要更改图像,您必须转到CodeBuild -> Edit -> Environment

相关问题