Get CodeBuild artifact files up to the root folder while keeping the subfolders tree

Viewed 585

Here is my current buildspec file:

version: 0.2

phases:
  build:
    commands:
    - pip install mkdocs
    - mkdocs build
artifacts:
  files:
    - site/**/*

I want to get all the files and folders in the site folder.

With this file, the generated artifacts have this structure:

site
  ├── folder-1
  │   └── file-1.html
  └── folder-2
      ├── file-2.html
      └── subfolder
          └── file-3.html

I want to get rid of the site/ top folder and have all the subfolders and files going up at the "root" of the artifact folder, as it contains a static website I want to upload to an S3 bucket.

Therefore, I can't use the discard-paths argument as everything is going to be flattened, when I want to keep the subfolder hierarchy.

2 Answers

You can use aws s3 sync site/ s3://<bucket_name> in the post build

eg.

version: 0.2

phases:
  build:
    commands:
    - pip install mkdocs
    - mkdocs build
  post_build:
    commands: aws s3 sync site/ s3://<bucket_name>

It's better than pushing the artifact files. I tried it and it just adds the build artifacts into the S3. Using the sync command copies missing or outdated files, you can use the --delete option as well to remove files if there are not present in your new build.

View more info here: https://docs.aws.amazon.com/cli/latest/userguide/cli-services-s3-commands.html#using-s3-commands-managing-objects-sync

p.s. make sure to add a policy for codebuild to allow codebuild to interact with the S3 bucket

It sounds like you are after the base-directory option, which strips leading paths off the artifact files. In your case base-directory: site would seem to be what you want.

If the artifact handing isn't working for you, you can add another build step that zips up the files however you prefer, and then just add that .zip you created as the artifact instead.

This way you will end up with the same .zip output, only you'll have created it just the way you want instead of getting CodeBuild to do it for you.

Related