惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
J
Java Code Geeks
博客园 - 聂微东
量子位
C
Check Point Blog
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
B
Blog
罗磊的独立博客
腾讯CDC
GbyAI
GbyAI
博客园 - 【当耐特】
A
About on SuperTechFans
M
MIT News - Artificial intelligence
U
Unit 42
D
Docker
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Y
Y Combinator Blog
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队

lvbibir's Blog

shell | 磁盘空间分析脚本 Linux cat 和 tee 命令写入文件 python | 使用 uv 管理你的 python 环境 Claude Code 完整配置指南 windows | mihomo 内核独立部署指南 linux | 磁盘扩容 wsl | 释放长久运行占用的磁盘空间 mysql | 线程上限问题处理 麒麟 7.6 安装谷歌 OTP 认证模块 suse 12sp5 升级 openssh 及 openssl suse 12sp5 部署 mysql 5.7 《谁的青春不迷茫》 docker | centos7 部署 docker vim | 基础配置和使用 windows | rime 输入法 & 雾凇方案 shell | sshpass 批量传输文件及执行命令 wsl | 原生 linux 方式安装 docker nodejs | fnm + pnpm 开发环境配置 wsl | 安装配置 miniconda 虚拟环境 wsl | 自动更新系统代理 wsl | bashrc 环境变量不正确加载的处理方法 wsl | win10 安装 wsl2 troubleshooting | ssh 成功但是 scp 失败 linux | 常用命令总结 docker 部署 piclist shell | 检测网站存活并自动钉钉告警 Zabbix | 监控端口连通性并自动追踪 TCP 路由 windows | 自定义开机快速启动项 windows | miniconda 配置 python 虚拟环境 Zabbix | 监控主机到指定 ip 的流量大小
shell | 将本地镜像批量推送到 harbor
lvbibir · 2022-10-12 · via lvbibir's Blog

流程图

代码示例

使用前需要登录 harbor

确保镜像的项目名在 harbor 中已存在

格式三类型的镜像会推送到 harbor 的 library 项目中

#!/bin/bash
# author: Amadeus Liu
# date: 2022-10-11 17:02:13
# version: 1.0

harbor_url="local.harbor.com"
log_file="/var/log/push-harbor.log"
image_id=$(docker images -q | sort -u)

ls ${log_file} || touch ${log_file}
echo "############# $(date "+%Y-%m-%d %H:%M:%S") #############" >> ${log_file}

get_image_tags () {
  docker inspect $1 --format='{{.RepoTags}}' | sed 's/\[//g' | sed 's/\]//g'
}

image_tag_and_push () {
  docker tag $1 $2 && echo "docker tag  $1 $2" >> ${log_file}
  docker push $2 && echo "docker pull $1 $2" >> ${log_file}
}

for i in ${image_id}; do
  # 判断镜像是否有harbor仓库的标签,有则视为harbor仓库中已有
  if [[  $(get_image_tags $i) =~ ${harbor_url} ]]; then
      echo "已有${harbor_url}仓库标签-----$(get_image_tags $i)"
    else
      # 镜像的第一个完整标签
      image_tag_first=$(echo $(get_image_tags $i) | awk -F' ' '{print $1}')
      # 镜像的第一个完整标签并去除版本
      image_tag_first_delete_ver=$(echo ${image_tag_first} | awk -F':' '{print $1}')
      
      # 判断标签属于哪种格式
      if [[ ${image_tag_first_delete_ver} =~ "/" ]]; then
        # 镜像的第一个完整标签的第一部分('/'分割后的$1)
        image_tag_first_repo=$(echo ${image_tag_first_delete_ver}| awk -F'/' '{print $1}')
        if [[ "${image_tag_first_repo}" =~ "." ]]; then
            # 格式一
            image_tag_harbor="${harbor_url}/$(echo ${image_tag_first} | awk -F'/' '{print $2}')/$(echo ${image_tag_first} | awk -F'/' '{print $3}')"
            echo "${image_tag_first} >>>>>tag to>>>>> ${image_tag_harbor}"
            image_tag_and_push $i ${image_tag_harbor}
          else
            # 格式二
            image_tag_harbor="${harbor_url}/${image_tag_first}"
            echo "${image_tag_first} >>>>>tag to>>>>> ${image_tag_harbor}"
            image_tag_and_push $i ${image_tag_harbor}
        fi
      else
        # 格式三
        image_tag_harbor="${harbor_url}/library/${image_tag_first}"
        echo "${image_tag_first} >>>>>tag to>>>>> ${image_tag_harbor}"
        image_tag_and_push $i ${image_tag_harbor}
      fi
  fi
done

腾讯云搬迁声明

我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=3ielzwnut2qsg

以上

wechat_pay

微信

alipay

支付宝