AWS CodeBuild buildspec.yml get all files and subfolders recursively
TL;DR You probably don't need to use "discard-paths: yes". So, drop "discard-paths: yes" and instead use "base-directory" with "**/*" for glob pattern. 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:
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 - **/*. 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.
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
:
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
.Then in
post_build
I move everything out of folderbuild-output
.Delete files pulled from my GitHub's repo which aren't needed to host the static website in the
S3 bucket
Then I get the result I want: all files inside
public
in the root of theS3 bucket
with the right folder tree.
Update:
- You can also use my
buildspec.yml
example file here. Since the example is out of the scope of this question, I don't paste the code here. - I explain in details here.