-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelete-harbor-image.sh
38 lines (31 loc) · 1.41 KB
/
delete-harbor-image.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/bin/bash
# Simple script to delete the latest images in Harbor before uploading a new image.
# Run this in your CI/CD pipeline
case "${{parameters.environment}}" in
"dev")
STATUS_CODE=$(curl -u "$HARBOR_USER:$HARBOR_PASS" --location --request GET "https://$HOST/api/v2.0/projects/$PROJECT/repositories/$REPO_NAME/artifacts/${{parameters.tag}}" -k --write-out "%{http_code}\n" --silent --output /dev/null)
case "$STATUS_CODE" in
404)
echo "There's no such tag or image in this repository. Status code: $STATUS_CODE"
exit 0
;;
200)
STATUS_CODE=$(curl -u "$HARBOR_USER:$HARBOR_PASS" --location --request DELETE "https://$HARBOR_HOST/api/v2.0/projects/$PROJECT/repositories/$REPO_NAME/artifacts/${{parameters.tag}}" -k --write-out "%{http_code}\n" --silent --output /dev/null)
if [ "$STATUS_CODE" = 200 ]; then
echo "Image deleted in Harbor. Status code: $STATUS_CODE"
exit 0
else
echo "Error when trying to delete the image. Status Code: $STATUS_CODE"
exit 1
fi
;;
*)
;;
esac
;;
"tst" | "uat" | "prd")
echo "${{parameters.environment^^}} environment. Actually, there is no need to delete an image."
;;
*)
;;
esac