This commit is contained in:
		
							
								
								
									
										59
									
								
								utils/parse_image.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								utils/parse_image.go
									
									
									
									
									
										Normal 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 | ||||
| } | ||||
		Reference in New Issue
	
	Block a user