AWS Codebuild puting all artifacts in root of S3 bucket
Asked Answered
M

4

22

I'm building a project that puts all of it's files in a 'dist' folder, and running it through CodeBuild. I'm trying to get it to put all of the files and folders in 'dist' into the root of the s3 bucket, but I'm having trouble figuring out how to make that work.

My 'dist' folder looks something like this:

- index.html - somecssfiles.css - fonts/ - - some fonts or w/e - js/ - - some javascript files

I've tried a lot of different stuff, but can't seem to get it to just drop 'dist/*' into the root of the s3 bucket. Here's the current iteration of my artifacts property in the buildspec.yml file:

artifacts: files: - '*' discard-paths: yes base-directory: 'dist'

I thought that would probably work, but it ignores the folders. Any help is appreciated, thanks for reading.

Molasses answered 29/3, 2018 at 16:2 Comment(0)
I
11

I only recently figured this out myself, so experts please forgive me if I am incorrect, but:

The "discard-paths: yes" is what throws away your directory structure. It essentially flattens your artifact set. If (like me) you only want to throw away paths up to your artifact folder, put that full path in the "base-directory" field.

Again, this part I'm not sure about, but it seemed to me that you can't tell CodeBuild not to apply server side encryption to your artifacts, meaning if its a website you intend to allow people to browse to, that's not going to work, at least out of the box. What I ended up doing was instead of using the artifact section of codebuild at all, I added the following post_build command:

commands: - aws s3 sync my/artifact/path/ s3://my-bucket-name/

edit: I am building using a docker image I created and added to Amazon's ECR, I had to install the AWS CLI in my image to be able to run that command.

Irksome answered 30/3, 2018 at 13:31 Comment(1)
Beware running that sync command in the POST_BUILD step, this will run regardless if the BUILD step has passed or not unless your doing some sort of safety check. Better to run it in the BUILD command chained after your build. eg: npm build && aws s3 sync ....Sama
D
41

I think you're missing a piece - it's not clear but you need to specify the path in your Codebuild template with the following artifacts:

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

And then ensure that in the "Artifacts" section of your codebuild project, you specify the S3 bucket normally, but add the "optional" name parameter to be /

This sets the output to the root of the S3 directory - see https://docs.aws.amazon.com/codebuild/latest/APIReference/API_ProjectArtifacts.html#CodeBuild-Type-ProjectArtifacts-name

If type is set to S3, this is the name of the output artifact object. If you set the name to be a forward slash ("/"), the artifact is stored in the root of the output bucket.

Diffusivity answered 28/2, 2019 at 12:37 Comment(3)
This should be correct answer. You save my day !! Thanks a lot.Wortham
Additionally, if you use this buildspec command template, you can leave the "Path" field blank in the "Artifacts" section of CodeBuild.Tenner
For anyone reading this: don't be a dummy like me and get "Name" get mixed up with "Path (optional)". You want to set Name to "/"Shattuck
I
11

I only recently figured this out myself, so experts please forgive me if I am incorrect, but:

The "discard-paths: yes" is what throws away your directory structure. It essentially flattens your artifact set. If (like me) you only want to throw away paths up to your artifact folder, put that full path in the "base-directory" field.

Again, this part I'm not sure about, but it seemed to me that you can't tell CodeBuild not to apply server side encryption to your artifacts, meaning if its a website you intend to allow people to browse to, that's not going to work, at least out of the box. What I ended up doing was instead of using the artifact section of codebuild at all, I added the following post_build command:

commands: - aws s3 sync my/artifact/path/ s3://my-bucket-name/

edit: I am building using a docker image I created and added to Amazon's ECR, I had to install the AWS CLI in my image to be able to run that command.

Irksome answered 30/3, 2018 at 13:31 Comment(1)
Beware running that sync command in the POST_BUILD step, this will run regardless if the BUILD step has passed or not unless your doing some sort of safety check. Better to run it in the BUILD command chained after your build. eg: npm build && aws s3 sync ....Sama
C
4

Adhering to the paradigma of CodePipeline, I'd suggest you produce an artifact containing only the folder using code build, and then deploy that artifact to whereever, e.g. to S3 using AWS Code Deploy.

But if if you really want to use Code Build then the following is a solution:

In the Codebuild template use:

artifacts:
    files:
        - "dist/**/*"

Then in the AWS Console edit the "Artifacts" section of your codebuild project, use the following inputs:

  • "Name": use the forward slash ("/")
  • "Path - optional": leave empty
  • "Namespace type - optional": choose "None"

Other Examples taken from aws documentation:

  • If path is set to MyArtifacts, namespaceType is set to BUILD_ID, and name is set to MyArtifact.zip, then the output artifact is stored in MyArtifacts//MyArtifact.zip.

  • If path is empty, namespaceType is set to NONE, and name is set to "/", the output artifact is stored in the root of the output bucket.

  • If path is set to MyArtifacts, namespaceType is set to BUILD_ID, and name is set to "/", the output artifact is stored in MyArtifacts/.

Contuse answered 15/1, 2021 at 11:19 Comment(0)
W
0

faced the same issue and here is what worked for me:

CodeBuild Artifacts Config

1

name: just a "/" no quotes.

path: "./" again no quotes.

nameSpace: None

All my files are now in root of s3 with no parent folders.

Woden answered 16/8, 2022 at 22:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.