Axiaz
Axiaz

Reputation: 117

Cleaning Up Old Files in AWS CodePipeline

I have CodePipeline set up to build and deploy a static Vue site from my Github repo to an S3 bucket. But since the built files have hashed names (e.g. app.2c71f2bb.js), after each deploy, the old files still remain in the bucket. I'm wondering what's a common way of dealing with this issue? And how would I go about doing it?

Upvotes: 6

Views: 3391

Answers (3)

varun vashishtha
varun vashishtha

Reputation: 187

  1. aws s3 rm s3://BUCKET_NAME --recursive. (Empty bucket's current data)
  2. cd into the folder which you want to upload.
  3. aws s3 sync . s3://BUCKET_NAME. ( upload all the files into the given bucket)

Upvotes: 0

George Rushby
George Rushby

Reputation: 1355

Without knowing the stages in your pipeline I am going to assume that you have a CodeBuild step already defined because you mentioned a build.

Checkout > Build > Deploy (S3)

Remove the Deploy step and add this to CodeBuild,

            post_build:
              commands:
              - aws s3 sync ${LOCAL_FILES} s3://${S3_BUCKET_NAME} --delete

When doing this you will need to add the relevant permissions to your CodeBuild role, not the CodePipeline role.

Upvotes: 11

shariqmaws
shariqmaws

Reputation: 8890

Throw in a CodeBuild action in CodePipeline to run some custom aws cli commands to remove the S3 objects. Make sure your CodeBuild service role has permissions to do the relevant AWS API actions.

Upvotes: 0

Related Questions