简体   繁体   English

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

[英]AWS CodeBuild buildspec.yml get all files and subfolders recursively

I'm trying to use AWS CodeBuild to get all files and subfolders inside a nested public<\/code> folder and deploy to an S3 bucket using CodePipeline.我正在尝试使用 AWS CodeBuild 获取嵌套public<\/code>中的所有文件和子文件夹,并使用 CodePipeline 部署到 S3 存储桶。 I was able to hook them all together but struggling to configure the buildspec.yml<\/code> file to get the output I want.我能够将它们全部连接在一起,但努力配置buildspec.yml<\/code>文件以获得我想要的输出。

My folder structure:我的文件夹结构:

<path>/public/

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

TL;DR You probably don't need to use " discard-paths: yes ". TL; DR你可能并不需要使用“丢弃路径:是的”。 So, drop " discard-paths: yes " and instead use " base-directory " with " **/* " for glob pattern.所以,去掉“ discard-paths: yes ”,而是使用“ base-directory ”和“ **/* ”作为glob模式。 Read on for more reasoning why.继续阅读以了解更多原因。

So yeah, as it usually happens, the culprit was pretty trivial.所以是的,正如通常发生的那样,罪魁祸首非常微不足道。 In my case the directory structure looked like this:就我而言,目录结构如下所示:

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

So in order for the artifact to contain everything from the " dist " directory whilst preserving the nested directories structure, the buildspec had to look like this:因此,为了让工件包含“ dist ”目录中的所有内容,同时保留嵌套目录结构,构建规范必须如下所示:

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

Essentially " base-directory " parameter behaves as unix's "cd" command, so at first CodeBuild jumps to that directory and only after that resolves the glob pattern, in this case - **/* .本质上,“ base-directory ”参数的行为类似于unix的“ cd”命令,因此首先CodeBuild跳转到该目录,然后才解析glob模式,在本例中为 - **/* If you also happen to use " discard-paths " then what happens is that, presumably, CodeBuild will change current directory to " dist ", resolve the files structure correctly but then remove all path prefixes from the resolved paths, thus the result will be a plain directory structure with no nesting.如果你也碰巧使用“丢弃路径”然后发生的事情是,据推测,CodeBuild将当前目录切换到“DIST”,正确地解析文件结构,但随后从解决路径删除所有路径前缀,这样的结果将是没有嵌套的普通目录结构。

Hope that helps!希望有帮助!

I've figured out a way to work around it.我想出了一个办法来解决它。 I don't think it's the best way but it works.我不认为这是最好的方法,但它有效。 I hope you can find this solution useful buildspec.yml :我希望你能找到这个解决方案有用的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:
    - '**/*'

In words:用一句话来说:

  • I copy everything inside the nested <path>/public folder out to a folder called ./build-output .我将嵌套的<path>/public文件夹中的所有内容复制到名为./build-output的文件夹中。

  • Then in post_build I move everything out of folder build-output .然后在post_build我将所有内容移出文件夹build-output

  • Delete files pulled from my GitHub's repo which aren't needed to host the static website in the S3 bucket删除从我的 GitHub 存储库中提取的文件,这些文件不需要在S3 bucket托管静态网站

  • Then I get the result I want: all files inside public in the root of the S3 bucket with the right folder tree.然后我得到了我想要的结果: S3 bucket根目录中public中的所有文件以及正确的文件夹树。

Update:更新:

  • You can also use my buildspec.yml example file here .您还可以在此处使用我的buildspec.yml示例文件。 Since the example is out of the scope of this question, I don't paste the code here.由于示例超出了本问题的范围,因此我不将代码粘贴到此处。
  • I explain in details here .我在这里详细解释。

I was running into a similar issue and after many permutations below syntax worked for me.我遇到了类似的问题,经过以下语法的许多排列对我有用。

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

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

This worked for me.这对我有用。

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

in buildspec.yml在 buildspec.yml 中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将环境变量传递给 AWS 代码构建的 buildspec.yml - How to pass environment variable to the buildspec.yml for AWS codebuild AWS CodeBuild:如何强制 buildspec.yml 在出错时退出 - AWS CodeBuild: How to force buildspec.yml quit on error AWS CodeBuild - BuildSpec.yml - 导出变量未解析值 - AWS CodeBuild - BuildSpec.yml - exported-variables are not resolving values CodeBuild,获取文件夹列表,里面的文件发生了改变buildspec.yml - CodeBuild, get the list of folders that have changed files inside them buildspec.yml AWS Copilot 管道失败,为什么 CodeBuild 找不到 buildspec.yml? - AWS Copilot pipeline fails, why cant CodeBuild find buildspec.yml? 为什么 AWS CodeBuild buildspec.yml 支持多个构建阶段? - Why do AWS CodeBuild buildspec.yml support multiple build phases? 有没有办法在 AWS CodeBuild 的 buildspec.yml 中指定手动批准阶段? - Is there a way to specify a manual approval stage within the buildspec.yml for AWS CodeBuild? 如何将环境变量添加到CodeBuild buildspec.yml? - How Do You Add Environment Variables to CodeBuild buildspec.yml? 我们如何通过 Boto3 API 从每个代码构建项目中获取 buildspec.yml 文件的内容 - how can we get contents of a buildspec.yml file from each codebuild project through Boto3 API 术语“ docker”未被识别为cmdlet AWS buildspec.yml的名称 - The term 'docker' is not recognized as the name of a cmdlet AWS buildspec.yml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM