无法在 CodeBuild 中运行 bash 脚本

brn*_*o32 4 amazon-web-services aws-codebuild

我有以下内容buildspec.yml

version: 0.2

env:
  shell: bash
phases:
  install:
    runtime-versions:
      nodejs: 12
    commands:
      - source cicd/app_cicd.sh
      - npm_install
Run Code Online (Sandbox Code Playgroud)

哪里cicd/app_cicd.sh

#!/bin/bash

function npm_install() {
    npm install
}
Run Code Online (Sandbox Code Playgroud)

但 CodeBuild 输出显示

[Container] 2021/05/23 01:55:32 Phase complete: DOWNLOAD_SOURCE State: SUCCEEDED
[Container] 2021/05/23 01:55:32 Phase context status code:  Message: 
[Container] 2021/05/23 01:55:33 Entering phase INSTALL
[Container] 2021/05/23 01:55:33 Running command export CICD_ROOT=$(pwd)

[Container] 2021/05/23 01:55:33 Running command source cicd/app_cicd.sh

[Container] 2021/05/23 01:55:33 Running command npm_install
/codebuild/output/tmp/script.sh: line 4: npm_install: command not found
Run Code Online (Sandbox Code Playgroud)

鉴于我已指定在 中使用 bashbuildspec.yml并且我的 shell 脚本包含 shebang,我不确定出了什么问题。这当然是一个人为的例子

Mar*_*cin 5

npm_install必须与您的在同一行source,否则它们将完全独立执行。所以你的source命令不会延续到第二个命令。

version: 0.2

env:
  shell: bash
phases:
  install:
    runtime-versions:
      nodejs: 12
    commands:
      - source cicd/app_cicd.sh && npm_install
Run Code Online (Sandbox Code Playgroud)