chenyao
2025-02-18 b5c7b77cf659eb704d5af76808d9f1036c8414fc
Merge branch 'master' of http://139.196.74.78:10010/r/drone/pilot-h5
1 files added
29 ■■■■■ changed files
rmi_his_image.sh 29 ●●●●● patch | view | raw | blame | history
rmi_his_image.sh
New file
@@ -0,0 +1,29 @@
#!/bin/bash
# 删除历史版本镜像
# 打印本次更新的版本号(为jenkins更新的版本号),小于这个版本号的都清理掉
echo $BUILD_NUMBER
# 需要清理的镜像名名称,一般是项目模块名称
echo $IMAGE_NAME
# 要比较的版本号(仅比较数字部分,忽略 -SNAPSHOT)
TARGET_BUILD_NUMBER=$BUILD_NUMBER
# 列出所有相关镜像
images=$(docker images --filter=reference="$IMAGE_NAME:*" --format '{{.Repository}}:{{.Tag}}')
echo $images
for image in $images; do
    # 提取镜像标签中的构建号
    tag=$(echo $image | cut -d: -f2)
    build_number=$(echo $tag | sed -n 's/.*\([0-9]*\).*/\1/p')
    # 检查是否提取到有效的构建号
    if [[ -z $build_number ]]; then
        echo "Warning: Could not extract build number from tag '$tag'. Skipping image '$image'."
        continue
    fi
    # 将构建号转换为整数进行比较
    if (( build_number < TARGET_BUILD_NUMBER )); then
        # 构建号小于目标值,删除镜像
        echo "Deleting image: $image (build number $build_number is less than $TARGET_BUILD_NUMBER)"
        docker rmi $image
    fi
done