Created
December 13, 2022 14:27
-
-
Save Robogeek95/d87c6763e47c78976dae1440831ba1c6 to your computer and use it in GitHub Desktop.
clean up old jfrog artifacts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#replace <token> with your artifactory token | |
#replace <domain> with your artifactory domain name | |
# pass to the script the $START_TIME for minimum creation time in milliseconds of artifacts (example 1325376000000 for January 1st 2012) | |
# pass to the script the $END_TIME for maximum creation time in milliseconds of artifacts (example 1388534400000 for January 1st 2014) | |
# pass to the script the $REPO for the repository where artifacts are located (example: releases) | |
domain="" | |
START_TIME="1670626800000" | |
END_TIME="1670886000000" | |
REPO="" | |
token="" | |
# find all artifacts created in <START_TIME> till <START_TIME>, time are expressed in milliseconds | |
# grep look for uri attribute in JSON results returned then awk take the corresponding value of the uri attribute. The first 2 sed commands remove the last character and the last sed command remove the first character, ie stripping off the comma and double quotes from the URL in awk | |
# RESULTS=`curl -H "Authorization: Bearer $token" -s -X GET "https://${domain}.artifactoryonline.com/${domain}/api/search/creation?from=$START_TIME&to=$END_TIME&repos=$REPO" | grep uri | awk '{print $3}' | sed s'/.$//' | sed s'/.$//' | sed -r 's/^.{1}//'` | |
RESULTS=`curl -H "Authorization: Bearer $token" -s -X GET "https://${domain}.jfrog.io/artifactory/api/search/creation?from=$START_TIME&to=$END_TIME&repos=$REPO" | grep uri | awk '{print $3}' | sed s'/.$//' | sed s'/.$//' | sed -r 's/^.{1}//'` | |
echo RESULTS:$RESULTS | |
for RESULT in $RESULTS ; do | |
echo "fetching path from $RESULT" | |
# from the URL we fetch the download uri to remove | |
# grep look for downloadUri attribute in JSON results returned then awk take the corresponding value of the downloadUri attribute. The first 2 sed commands remove the last character and the last sed command remove the first character, ie stripping off the comma and double quotes from the URL in awk | |
PATH_TO_FILE=`curl -H "Authorization: Bearer $token" -s -X GET $RESULT | grep downloadUri | awk '{print $3}' | sed s'/.$//' | sed s'/.$//' | sed -r 's/^.{1}//'` | |
echo "deleting path $PATH_TO_FILE" | |
# deleting the corresponding artifact at last | |
curl -H "Authorization: Bearer $token" -X DELETE $PATH_TO_FILE | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment