Coder Social home page Coder Social logo

euan-forrester / vue-cli-plugin-s3-deploy-cleanup Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 0.0 68 KB

A vue-cli plugin that helps cleanup old build artifacts from S3 by tagging them for later deletion

License: MIT License

JavaScript 100.00%
vue vuejs vue-cli-plugin s3 deploy deployment aws aws-s3

vue-cli-plugin-s3-deploy-cleanup's Introduction

vue-cli-plugin-s3-deploy-cleanup

A vue-cli plugin that helps cleanup old build artifacts from S3 by tagging them for later deletion

Description

Each time you change a file and then build your project, files with similar -- but different -- names are created. These build artifacts have a hash appended to their filenames to differentiate them. The new index.html file generated then points to these new files. When you deploy them by copying them to S3 the files with the old hashes remain. Over time you may build up lots of old files like this in your bucket.

This plugin helps to clean up these files by tagging all the non-current files with a special value that S3 can then identify and delete at a later time. It's important to not delete them right away because a user may have some, but not all, of them cached in their web browser. If they have your old index.html file cached on their local machine, their web browser might go looking for the old hashes that it references, and if they're not there the user will see a 404.

Thus an S3 lifecycle rule can be used to delete these tagged files after a certain number of days when you're sure that no one will have them cached.

Instructions

  1. Add the vue-cli-plugin-s3-deploy plugin to your project: https://github.com/multiplegeorges/vue-cli-plugin-s3-deploy

  2. Add this plugin to your project: vue add vue-cli-plugin-s3-deploy-cleanup

An S3 tag has a Key and a Value, so you will be prompted for both during the installation phase. Or you can accept the default Key = "DeployLifecycle" and Value = "DeleteMe".

You'll also get a deploy:cleanup npm script added that invokes this plugin.

  1. Change your S3 bucket definition to include a lifecycle rule that will delete all items with the tag above in N days.

Terraform example:

resource "aws_s3_bucket" "frontend" {

  [...]
  
  versioning {
    enabled = true
  }

  lifecycle_rule {
    id      = "expire-old-versions-after-N-days"
    enabled = true

    noncurrent_version_expiration {
      days = var.days_to_keep_old_versions
    }
  }

  lifecycle_rule {
    id      = "expire-tagged-files-after-N-days"
    enabled = true

    tags = {
      "DeployLifecycle" = "DeleteMe"
    }

    expiration {
      days = var.days_to_keep_old_versions
    }
  }
}

Note that we have 2 lifecycle rules: one to delete old versions of the same file, and one to delete files with our new tag. This is because some files may be deployed with the same name (e.g. index.html) and so will have old versions kept by S3 if versioning is enabled on the bucket.

  1. Deploy your project: yarn deploy && yarn deploy:cleanup

You'll see a list of files that the plugin found in S3 and in the dist directory on your local machine. Any files in S3 that are not on your local machine are assumed to be old build artifacts and will be tagged with the tag your specified. They will also be copied in place so that their last modified date is updated (the lifecycle rule calculates time based on the object's last modified date, and tagging the object doesn't update this date). Their metadata will be replaced during this copy operation. Once this is done the lifecycle rule will delete them after the amount of time you specified.

Environments

Note that you can override the cleanup tag on a per-environment basis by adding the values:

VUE_APP_S3D_CLEANUP_TAG_KEY = <something>
VUE_APP_S3D_CLEANUP_TAG_VALUE = <something>

To your .env.<environment> file, and then invoking the deploy with yarn deploy --mode=<environment> && yarn deploy:cleanup --mode=<environment>

This is the same file that holds any overrides for the vue-cli-plugin-s3-deploy plugin.

vue-cli-plugin-s3-deploy-cleanup's People

Contributors

euan-forrester avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

vue-cli-plugin-s3-deploy-cleanup's Issues

node 18

Hi
Any chance of a version bump to allow node v18+?
thanks!

AccessDenied

When I try to execute the cleanup I get it. It would be possible because the default aws profile it's not the same that the bucket?
I've fullaccess to s3.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.