Skip to content

Instantly share code, notes, and snippets.

@shahidv3
Last active February 7, 2021 07:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shahidv3/7f47e92e04af4e13c94729fad91a5125 to your computer and use it in GitHub Desktop.
Save shahidv3/7f47e92e04af4e13c94729fad91a5125 to your computer and use it in GitHub Desktop.
Shell script to remove docker container files from Harbor
#!/bin/bash
## ##############################################################################################################################
## Author : Sahid Shaik shahids89@gmail.com #
## Usage: ./tags_delete.sh #
## Harbor images deletion is 2 step process #
## 1. Soft delete image using this script #
## 2. Run garbage collector #
## ##############################################################################################################################
source config.ini
folder=$(date +"%m-%d-%Y_%H.%M.%S")
mkdir ${folder}
staging_tag=stage
prod_tag=prod
TagsDelete() {
for repo in `cat repos.txt`;
do
echo $repo;
curl -u $harbor_user:$harbor_pswd -X GET http://$harbor_host/api/repositories/myrepo/$repo/tags > $folder/$repo.json;
awk '/name/ {print $2}' $folder/$repo.json | tr -d '",' | sort > $folder/$repo.txt;
if [ $1 == "stage" ]
then
awk '/Staging/{print}' $folder/$repo.txt | sort -Vr | sed -n '3,$p' > $folder/$1_$repo.ini;
else
awk '/Production/{print}' $folder/$repo.txt | sort -Vr | sed -n '2,$p' > $folder/$1_$repo.ini;
fi
for buildnum in `cat $folder/$1_$repo.ini`;
do
echo $buildnum;
curl -v -u $harbor_user:$harbor_pswd -X DELETE http://$harbor_host/api/repositories/myrepo/$repo/tags/$buildnum;
done
done
}
TagsDelete $staging_tag
TagsDelete $prod_tag
@leventgenc
Copy link

First of all, thank you very much.

Could you give more information about the use of the harbor_tags_delete.sh script?

What format of repos should the repos.txt file look like?

Can you tell with an example structure?

@shahidv3
Copy link
Author

shahidv3 commented Feb 7, 2021

hi @leventgenc
sorry for responding late. I'm seeing it today. Better late than never.
hoping this would be useful to other folks

You can List up all Docker images and details of each image under the project example like 'library' like this

$ curl -i -k -X GET "https://harbor.mycompany.local/api/search?q=library/

redirect the above output to a file and extract the repo names using awk command.

Why the script is useful - I have captured in this blog link

https://medium.com/@shahids89/harbor-container-images-cleanup-automation-1de7cc7c5655

thanks , sahid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment