重新解析image地址
All checks were successful
continuous-integration/drone/tag Build is passing

This commit is contained in:
谢作欢
2023-11-03 15:57:34 +08:00
parent d78b079290
commit 57a38b9d2b
3 changed files with 63 additions and 31 deletions

59
utils/parse_image.go Normal file
View File

@@ -0,0 +1,59 @@
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
}