This post is an update from my previous “Remove unused images from your git project” post.
With a functional bash script that I could re-use for any git repo, I wanted to make the process of cleaning up my repo as painless and automated as possible. The initial goal was to make the script available on DockerHub (which it is), but that soon evolved to being able to run the script with a GitHub Action.
The code for this post can be found in this repo: https://github.com/stevemar/image-deleter
Creating a Docker Image
Being able to run our script with Docker was pretty easy, we needed to package it up in a
Dockerfile and modify it a bit so it’s looking at a mounted volume (hardcoded to
/source). Here’s the
Dockerfile that was used.
FROM bash:4.4 RUN apk add --no-cache git grep COPY rm-images.sh . ENTRYPOINT [ "bash", "/rm-images.sh", "/source" ]
Once I published the first version I was able to run it with just one command:
docker run -v `pwd`:/source stevemar/image-deleter:latest
This would mount the current path (determined with
/source) and then call our image on DockerHub. The file would be marked as deleted and we saved some time by not having to carry our script around with us to every new repo.
Creating a GitHub Action
Going one step further, where we don’t have to run anything, but rather we integrate that clean up into our repo would be the best case scenario. So why not try creating a GitHub Action?
name: 'Stale Image Remover' description: 'Remove stale images from your repo' runs: using: 'docker' image: 'Dockerfile' branding: icon: 'git-pull-request' color: 'blue'
his effectively allows our Action to be consumed by others, so long as they include this block of text in as a job their Action
- name: Remove the images uses: email@example.com
Once a new version was released I immediately made it available on the Marketplace: Here’s the “Stale Image Remover” on the Marketplace.
Testing it all out
I created a repo to test the action, here’s the repo. The action makes use of the “Create Pull Request” action to create a pull request against the repo. An example action can be found here: https://github.com/stevemar/testing-images/blob/master/.github/workflows/main.yml
on: push: branches: - master jobs: rm_old_images: runs-on: ubuntu-latest name: A job to remove images steps: - name: Checking out our code uses: actions/checkout@master - name: Remove the images uses: firstname.lastname@example.org - name: Create Pull Request uses: peter-evans/create-pull-request@v2 with: token: $ commit-message: Remove unused images title: '[Automated PR] Remove unused images' body: | Found a few images that can be removed : https://github.com/stevemar/image-deleter - name: Check outputs run: | echo "Pull Request Number - $" echo "Pull Request Number - $"
Most of this action can be copy and pasted directly to your repo and it’ll start working right away. You just need to create a Secret called GH_TOKEN, which is based on your API key. You can tweak the commit message, title, and body to whatever you like. Shout out to Peter Evans for the fantastic GitHub Action he created.
Once the job is published you can check out the logs…
And then see a Pull Request come in automatically…
That’s it, hopefully I’ll make a video of this soon!