通过 aws codebuild 为 nodejs lambda 创建 zip 文件的问题

issue creating zip file through aws codebuild for nodejs lambda

我想通过 aws codebuild 过程为我的 nodejs lambda 创建一个 zip 工件 - 这样 lambda 函数就可以使用这个 zip 文件作为 S3 中的源,我们有一个部署 "proof" 用于管理 git 代码构建中的提交 ID

我在 github-repo 中的文件结构是

folder1
   - myfile.js
   - otherfile.js
folder2
   - otherfiles.js
package.json

现在对于 nodejs lambda 项目我想要没有 zip 文件夹的 zip 文件(我们需要 lambda 中的 nodejs 项目)所以 zip 应该直接包含以下文件

- myfile.js
- node_module ==> folder from codebuild via npm install command 

问题:

1) S3 中的输出 zip 包含在文件夹中,即 .zip->rootfolder->myfile.js 而不是我们需要的 .zip->myfiles.js 对于 nodejs,lambda 不能使用它,它应该在根 zip 中有文件,而不是在它们里面(文件夹内没有相对路径)

2) 路径 - 如您所见 myfile.js 在文件夹内 我想省略相对路径 - 我尝试丢弃路径但问题是所有 node_module 文件也是在文件夹中而不是在文件夹中,因为丢弃路径适用于它们两者 - 我可以只为 myfile.js 而不是 node_module 文件夹设置丢弃路径吗? 我当前的 yaml 文件:

artifacts:
  files:
    - folder/myfile.js
    - node_modules/**/*
  discard-paths: yes 

如果有人能提供解决方案就太好了?

如果解决方案不包含更改 github-repo 文件夹结构,那就太好了,我想在该 repo 中对其他文件重复此操作以创建其他 lambda 函数。

编辑:

我使用了下面的 yaml 文件,在@awsnitin 回答后一切正常

version: 0.2

phases:
  build:
    commands:
      - echo Build started on `date`
      - npm install
  post_build:
    commands:
      - echo Running post_build commands
      - mkdir build-output
      - cp -R folder1/myfile.js build-output
      - mkdir -p build-output/node_modules
      - cp -R node_modules/* build-output/node_modules
      - cd build-output/
      - zip -qr build-output.zip ./*
      - mv build-output.zip ../
      - echo Build completed on `date`
artifacts:
  files:
    - build-output.zip

不幸的是,丢弃路径在这种情况下不起作用。最好的选择是将必要的文件复制到新文件夹作为构建逻辑的一部分 (buildspec.yml),并在工件部分指定该文件夹。这是一个示例构建规范文件

post_build:
    commands:
      - mkdir build-output
      - cp -R folder/myfile.js node_modules/ build-output
artifacts:
  files:
    - build-output/**/*

我刚刚在 Python 上遇到了这个问题,但我认为我的解决方案适用于 Node.js,因为两者都依赖于 .zip 文件。以下是我的 buildspec.yml:

artifacts:
  files:
    - '**/*'
  base-directory: target

上面的代码将文件压缩到目标目录下(我的 .py 和包是 pip 安装的,或者在 Node.js 的情况下,所有的 .js 和 node_modules)和将其保存到 S3 存储桶中。之前不需要压缩任何东西,也不需要将其列为工件文件。我在 CodeFile 中使用了 CodeBuild,所以 https://s3bucket/.../random-part 实际上是从 buildspec.yml/artifacts/files/base-directory 声明生成的 zip 文件。