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?
Asked
Active
Viewed 2,576 times
6
-
You could setup a lifecycle policy on the s3 bucket that is hosting these published assets. https://docs.aws.amazon.com/AmazonS3/latest/user-guide/create-lifecycle.html – Subin Mathew Dec 27 '19 at 16:20
3 Answers
10
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.

George Rushby
- 1,305
- 8
- 13
0
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.

shariqmaws
- 8,152
- 1
- 16
- 35
0
- aws s3 rm s3://BUCKET_NAME --recursive. (Empty bucket's current data)
- cd into the folder which you want to upload.
- aws s3 sync . s3://BUCKET_NAME. ( upload all the files into the given bucket)

varun vashishtha
- 167
- 1
- 4