#!/bin/bash# 获取harbor某个项目的所有镜像名称和所有的标签# harbor版本v1.7.5-a8f6543a# 时间:20230407# 适合无密码配置的情况,有密码的情况可网上搜索修改。#仓库url;如:https://harbor.com:443/HARBOR='待补充'#目录workdir='.'#项目名称project='待补充'IMAGE_RANG=$(curl -k $HARBOR/api/search?q=${project}|grep"repository_name"|awk -F "\""'{print $4}')# Make sure the file that stores the image is newif[ -f $workdir/a.txt ];thenmv$workdir/a.txt $(date +%Y%m%d%H%M%S).a.txt.bak
fi# Get the latest tag and ID for all imagesforiin${IMAGE_RANG};do# Get the creation time of the latest image#NEW_TAG_TIME=$(curl $HARBOR/api/repositories/$i/tags|awk '/"created"/'|sort -k 2| tail -n 1|awk -F '"' '{print $4}')# Get the latest tag of the image# NEW_TAG=$(curl $HARBOR/api/repositories/$i/tags |grep -B 7 "${NEW_TAG_TIME}" |awk '/"name": /' | awk -F '"' '{print $4}')NEW_TAG=$(curl -k $HARBOR/api/repositories/$i/tags |grep -B 7"${NEW_TAG_TIME}"|awk'/"name": /'|awk -F '"''{print $4}')# Because there may be multiple tags for the same image, you need to use the for loopforhin${NEW_TAG};do#Image, Image name and tagIMAGE=$(echo $i:$h)# Get image ID, IMAGE_ID=$(curl -k $HARBOR/api/repositories/$i/tags/$h/manifest |grep -A 7 manifest |grep digest |awk -F :'{print $3}')# Intercepts the first 12 bits of the image IDIMAGE_ID_NEW=$(echo ${IMAGE_ID:0:12})if[${#IMAGE_ID} -eq 0];thencontinue;fi# Merge the image name and ID into one lineID_IMAGE=$(echo $IMAGE $IMAGE_ID_NEW)# Load the image name into a new fileecho${ID_IMAGE}>>a.txt
donedone## Format file contentIMAGE_FILE=`date +%Y%m%d%H%M%S.harbor_image.txt`awk'{printf "%-60s %70s\n",$1,$2}'$workdir/a.txt >${IMAGE_FILE}if[$? -eq 0];thenrm -rf $workdir/a.txt
fiecho"Please check the ${IMAGE_FILE} file."