AWS CodeBuild buildspec.yml 递归获取所有文件和子文件夹

AWS CodeBuild buildspec.yml get all files and subfolders recursively

我正在尝试使用 AWS CodeBuild 获取嵌套 public 文件夹中的所有文件和子文件夹,并使用 CodePipeline 部署到 S3 存储桶。我能够将它们全部连接在一起,但努力配置 buildspec.yml 文件以获得我想要的输出。

我的文件夹结构:

<path>/public/

├── 404.html
├── css
│   ├── ...
├── fonts
│   ├── bootstrap
│   │   ├── ...
│   ├── icomoon
│   │   ├── icomoon
│   │   │   ├── ...
│   └── simple-line-icons
│       ├── ...
├── images
│   ├── ...
├── index.html
├── index.xml
├── js
│   ├── ...
└── tags
    └── index.xml

我需要将 public 文件夹中的所有内容(包括子文件夹)放入 S3 存储桶的根目录中。

到目前为止,我已经尝试按照文档 here, here and here 进行操作。我试过使用:

我找到了解决该问题的方法。我认为这不是最好的方法,但它确实有效。我希望你能发现这个解决方案有用 buildspec.yml:

version: 0.2

phases:
  build:
    commands:
      - mkdir build-output
      - cp -R <path>/public/ build-output
  post_build:
    commands:
      - mv build-output/**/* ./
      - mv build-output/* ./
      - rm -R build-output *.yml LICENSE README* .git*
artifacts:
  files:
    - '**/*'

换言之:

  • 我将嵌套 <path>/public 文件夹中的所有内容复制到名为 ./build-output 的文件夹中。

  • 然后在 post_build 中,我将所有内容移出文件夹 build-output

  • 删除从我的 GitHub 的存储库中提取的文件,这些文件不需要在 S3 bucket

  • 中托管静态网站
  • 然后我得到了我想要的结果:public 中的所有文件都位于 S3 bucket 的根目录中,具有正确的文件夹树。

Update:

  • 您也可以使用我的 buildspec.yml 示例文件 here。由于这个例子超出了这个问题的范围,我不在这里粘贴代码。
  • 我详细解释here.

我 运行 遇到了类似的问题,经过多次排列后语法对我有用。

artifacts:
  files:
    - './**/*'
  base-directory: public
  name: websitename

这对我有用。

    /
    buildspec.yml
    /public
        index.html
        /js
        /img
        appspec.yml
    /src

在buildspec.yml

    artifacts:
    files:
      - 'public/*'
    discard-paths: yes

TL;DR你可能不需要使用“discard-paths: yes”。因此,删除“discard-paths: yes”并使用“base-directory”和“**/*" 用于 glob 模式。继续阅读以了解更多原因。

所以是的,正如通常发生的那样,罪魁祸首非常微不足道。在我的例子中,目录结构如下所示:

dist/
|-- favicon.ico
|-- index.html
|-- js
|   `-- app.js
`-- revision.txt

因此,为了使工件包含“dist”目录中的所有内容,同时保留嵌套目录结构,构建规范必须如下所示:

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

本质上“base-directory”参数的行为与 unix 的 "cd" 命令相同,因此首先 CodeBuild 跳转到该目录,然后才解析 glob 模式,在这种情况 - **/*。如果您也碰巧使用了“discard-paths”,那么 CodeBuild 可能会将当前目录更改为“dist”,解析文件结构正确 但是 然后从解析的路径中删除所有路径前缀,因此结果将是一个没有嵌套的普通目录结构。

希望对您有所帮助!

对于那些在 CodePipline 中使用 CodeBuid 时遇到此问题并尝试对 buildspec.yml 文件进行正确配置的人,对我来说,罪魁祸首是在部署阶段,我将源代码设置为输入工件工件而不是构建工件。