1.需求背景
1.1 容器发布ci 成功以后 我们希望CD时候可以选择相关镜像
1.2 统计指定仓库镜像有多少个版本
2.shell代码
#!/bin/bash
#harbor 关键环境变量
HARBOR_URL=""
USERNAME=""
PASSWORD=""
PAGE_SIZE=50
imges_head=$(echo "$HARBOR_URL"|awk -F // '{print $2}' )
projects_endpoint="$HARBOR_URL/api/v2.0/projects?page_size=$PAGE_SIZE"
out_file="./images_tag_list.txt"
checkSysEnv() {
if [ -z "$HARBOR_URL" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]
then
echo -e "\033[31m缺少harbor登录关联环境变量,请检查并填写正确配置....\033[0m"
exit 1
fi
}
getProjectList(){
checkSysEnv
projects=$(curl -u $USERNAME:$PASSWORD -skX 'GET' $projects_endpoint |jq -r '.[].name')
if [ -z "$projects" ]
then
echo -e "\033[31m 获取harbor项目仓库出现异常境,请检查....\033[0m"
exit 1
fi
for project in $projects
do
echo "$project"
done
}
getRepoCountPages(){
checkSysEnv
project_name=$1
if [ -z "$project_name" ]
then
projects=`getProjectList`
for project in $projects
do
repo_count_endpoint="$HARBOR_URL/api/v2.0/projects?name=$project"
repo_count=$(curl -u $USERNAME:$PASSWORD -skX 'GET' $repo_count_endpoint |jq -r '.[].repo_count')
pages=$(expr $repo_count / 10 + 1)
echo "$pages"
done
else
repo_count_endpoint="$HARBOR_URL/api/v2.0/projects?name=$project_name"
repo_count=$(curl -u $USERNAME:$PASSWORD -skX 'GET' $repo_count_endpoint |jq -r '.[].repo_count')
pages=$(expr $repo_count / 10 + 1)
echo "$pages"
fi
}
get_appoint_repo_tags(){
echo "" > ./images_tag_list.txt
project=$2
repo_count=`getRepoCountPages $project`
if [ -z "$repo_count" ]; then
continue
fi
pages=$(expr "$repo_count" / 10 + 1)
for ((i=1; i<=$pages; i++)); do
images_endpoint="$HARBOR_URL/api/v2.0/projects/$project/repositories?page_size=10&page=$i"
repos_json=$(curl -u "$USERNAME:$PASSWORD" -skX 'GET' "$images_endpoint")
if [ -z "$repos_json" ] || [ "$repos_json" == "null" ]; then
continue
fi
repos=$(echo "$repos_json" | jq -r '.[].name' | awk -F / '{print $2}')
for repo in $repos; do
tag_count_url="$HARBOR_URL/api/v2.0/projects/$project/repositories/$repo"
tag_count=$(curl -u "$USERNAME:$PASSWORD" -skX 'GET' "$tag_count_url" | jq '.artifact_count')
if [ -z "$tag_count" ] || [ "$tag_count" == "null" ]; then
continue
fi
pages1=$(expr "$tag_count" / 10 + 1)
if [ -z "$pages1" ]; then
continue
fi
for ((j=1; j<=$pages1; j++)); do
tags_endpoint="$HARBOR_URL/api/v2.0/projects/$project/repositories/$repo/artifacts?page_size=10&page=$j"
tags_json=$(curl -u "$USERNAME:$PASSWORD" -skX 'GET' "$tags_endpoint")
if [ -z "$tags_json" ] || [ "$tags_json" == "null" ] ; then
continue
fi
tags=$(echo "$tags_json" |jq -r '.[].tags[].name')
if [ -z "$tags" ] || [ "$tags" == "null" ]; then
continue
fi
for tag in $tags; do
if [ -z "$tag" ] || [ "$tag" == "null" ]; then
continue
fi
printf "$HARBOR_URL/$project/$repo:$tag \n" >> images_tag_list.txt 2>&1
if [ ! -f "$out_file" ]; then
echo "文件$out_file 不存在"
exit 1
fi
while IFS= read -r line; do
echo "$line"
done < "$out_file"
done
done
done
done
}
case $1 in
"get_project_list")
getProjectList
;;
"get_repo_count_one_page")
getRepoCountPages $*
;;
"get_appoint_repo_tags")
checkSysEnv
get_appoint_repo_tags $*
;;
*)
echo -e "\033[32m 参数如下: \033[0m"
echo -e "\033[32m get_project_list \033[0m 获取harbor项目列表 默认查询最大50个 其他修改 PAGE_SIZE 参数"
echo -e "\033[32m get_repo_count_one_page \033[0m 获取项目列表有多少页数据 后面指定harbor项目名称,不指定查询全部"
echo -e "\033[32m get_appoint_repo_tags \033[0m get_appoint_repo_tags 指定harbor仓库名称 后面跟 harbor存在的仓库名称可以通过get_project_list查询"
;;
esac