Skip to content

Instantly share code, notes, and snippets.

@ThabetAmer
Created May 7, 2020 07:46
Show Gist options
  • Save ThabetAmer/37cf6420c42f99a79f0f1da8775e8b7a to your computer and use it in GitHub Desktop.
Save ThabetAmer/37cf6420c42f99a79f0f1da8775e8b7a to your computer and use it in GitHub Desktop.
Copies all tagged images with defined tag to a new one - using Harbor Retag feature.
#!/bin/bash
# Copies all tagged images with defined tag to a new one - using Harbor Retag feature.
# config vars
SRC_TAG="latest"
DST_TAG="v1"
HARBOR_API_URL="https://hostname/api"
HARBOR_USR="username"
HARBOR_PSW="password"
CURL_CMD="curl -Ssq -u $HARBOR_USR:$HARBOR_PSW"
REPOS=$($CURL_CMD -H "accept: application/json" -X GET "$HARBOR_API_URL/search?q=" | jq -r '.repository[] | .repository_name')
for REPO in $REPOS
do
#echo "INFO: processing repo $REPO..."
REPO_CODED=`echo $REPO | sed 's/\//\%2f/g'`
TAGS=$($CURL_CMD -H "accept: application/json" -X GET "$HARBOR_API_URL/repositories/$REPO_CODED/tags?detail=false" | jq -r '.[] | .name')
# validations
[[ -z "$TAGS" ]] && { echo "WARN: no tags found in $REPO" && continue; }
[[ "$TAGS" == *$SRC_TAG* ]] || { echo "WARN: no tag $SRC_TAG found in $REPO" && continue; }
[[ "$TAGS" == *$DST_TAG* ]] && { echo "WARN: tag $DST_TAG already exists in $REPO" && continue; }
# retag request
echo "INFO: retagging tag $SRC_TAG to $DST_TAG on $REPO..."
$CURL_CMD -X POST "$HARBOR_API_URL/repositories/$REPO_CODED/tags" \
-H "Content-Type: application/json" \
-d "{ \"override\": false, \"src_image\": \"$REPO:$SRC_TAG\", \"tag\": \"$DST_TAG\"}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment