drone-kubernetes/utils/parse_image.go
谢作欢 57a38b9d2b
All checks were successful
continuous-integration/drone/tag Build is passing
重新解析image地址
2023-11-03 15:57:34 +08:00

60 lines
1.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}