drone-kubernetes/utils/parse_image.go

60 lines
1.1 KiB
Go
Raw Permalink Normal View History

2023-11-03 15:57:34 +08:00
package utils
import (
"fmt"
"os"
"strings"
)
// ParseImage
// @description: 解析镜像地址
// @param repo
// @return string
func ParseImage(repo string) string {
if repo == "" {
return ""
}
// 判断镜像地址是否包含tag
if strings.Contains(repo, ":") {
// 整个镜像地址
image := strings.Split(repo, ":")[0]
tag := strings.Split(repo, ":")[1]
if strings.Contains(tag, "v") {
tag = GetRealImageTag()
}
repo = fmt.Sprintf("%s:%s", image, tag)
} else {
// 不包含判断是tag还是镜像地址
if strings.Contains(repo, "/") {
// 镜像地址默认tag为latest
repo = fmt.Sprintf("%s:latest", repo)
} else {
// tag只是tag判断是否带有v
if strings.Contains(repo, "v") {
repo = GetRealImageTag()
}
}
}
return repo
}
// GetRealImageTag
// @description: 获取真实的镜像tag
// @return repo
func GetRealImageTag() (tag string) {
// 这才是真是的最前面的那一块
realImageShort := os.Getenv("DRONE_SEMVER_SHORT")
imageAlpha := os.Getenv("DRONE_SEMVER_PRERELEASE")
tag = fmt.Sprintf("%s-%s", realImageShort, imageAlpha)
return
}