Code Monkey home page Code Monkey logo

ecr-images-cleaner-action's Introduction

ECR images cleaner github action

A github action to help with the cleanup of old ECR images, supporting some parametrisation.

The action will call a bash script that performs the following:

First, it will retrieve all images declared in the replica set history for the kube-namespace input provided.
This will usually be production as these are the images that you would want to protect from being deleted.

So for example if you have a deployment with revisionHistoryLimit: 5 in their spec, this action will protect up to 6 image tags (the one in the currently active deployment plus the previous 5), no matter how old they are.

Then it will get the details of all images stored in the ECR, filtering out the above replica set images, any image that matches the regex additional-tags-regex and any image pushed less than days-to-keep-old-images ago.
The default for this variable is 30 days but can be configured.

Finally, we sort the resulting images by their imagePushedAt date, from older to newer, and proceed to apply a safety buffer max-old-images-to-keep that we will maintain, even knowing that there are old images.

So for example if the resulting set of images to delete is 75 after applying all the above filters, without the buffer we would delete all of them. With a buffer of 25 (this is the default value), we will only delete the oldest 50, and leave the most recent 25 images. This buffer can be configured too.

Setup

To set this up, you require an IAM user with access to the ECR, as well as credentials to authenticate to the kubernetes cluster and namespace of your interest, usually production.

As a pre-requisite, you will need the following secrets setup in your GitHub project:

  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY
  • KUBE_PROD_CERT
  • KUBE_PROD_TOKEN
  • KUBE_PROD_CLUSTER
  • KUBE_PROD_NAMESPACE

Your secrets may have other names, it doesn't matter, as you will pass them when using the action in your workflow.

Also, if instead of your production namespace, you prefer to use staging, or any other, the secrets must correspond to that namespace.

Inputs

The following inputs can be passed to the action. Some are mandatory, some are optional.

  • aws-access-key-id - Required - Access key for IAM User, needed to access the ECR

  • aws-secret-access-key - Required - Secret access key for IAM User, needed to access the ECR

  • kube-cert - Required - Credentials to authenticate to the kubernetes cluster

  • kube-token - Required - Credentials to authenticate to the kubernetes cluster

  • kube-cluster - Required - Kubernetes cluster

  • kube-namespace - Required - Namespace to retrieve the replica sets

  • ecr-repo-name - Required - Name of the ECR repository, usually is team-name/app-name

  • additional-tags-regex - Optional - Additional image tags that should not be deleted (regex). Default: .*main.*|.*latest.*

  • days-to-keep-old-images - Optional - Number of days to keep images, older will be purged (unless in use or in buffer). Default: 30.

  • max-old-images-to-keep - Optional - Number of images to keep even if they are older than the cutoff date. Default: 25.

Examples of use

  1. Omitting optional inputs. It will use the default values:
- name: Run ECR cleanup script
  uses: ministryofjustice/[email protected]
  with:
    aws-access-key-id: ${{ secrets.ECR_AWS_ACCESS_KEY_ID }}
    aws-secret-access-key: ${{ secrets.ECR_AWS_SECRET_ACCESS_KEY }}
    kube-cert: ${{ secrets.KUBE_PROD_CERT }}
    kube-token: ${{ secrets.KUBE_PROD_TOKEN }}
    kube-cluster: ${{ secrets.KUBE_PROD_CLUSTER }}
    kube-namespace: ${{ secrets.KUBE_PROD_NAMESPACE }}
    ecr-repo-name: family-justice/disclosure-checker
  1. Customising optional input values:
- name: Run ECR cleanup script
  uses: ministryofjustice/[email protected]
  with:
    aws-access-key-id: ${{ secrets.ECR_AWS_ACCESS_KEY_ID }}
    aws-secret-access-key: ${{ secrets.ECR_AWS_SECRET_ACCESS_KEY }}
    kube-cert: ${{ secrets.KUBE_PROD_CERT }}
    kube-token: ${{ secrets.KUBE_PROD_TOKEN }}
    kube-cluster: ${{ secrets.KUBE_PROD_CLUSTER }}
    kube-namespace: ${{ secrets.KUBE_PROD_NAMESPACE }}
    ecr-repo-name: family-justice/disclosure-checker
    additional-tags-regex: .*published.*
    days-to-keep-old-images: 15
    max-old-images-to-keep: 30

Then you can use this job on a workflow that runs on a schedule, or with workflow_dispatch to run it manually.

ecr-images-cleaner-action's People

Contributors

zheileman avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

ushkarev

ecr-images-cleaner-action's Issues

User access removed, access is now via a team

Hi there

This user had Direct Member access to this repository and access via a team.

Access is now only via a team.

If you have any questions, please post in #ask-operations-engineering on Slack.

This issue can be closed.

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.