#!/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