2022-01-14 17:49:45 +02:00
|
|
|
package cnbutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/SAP/jenkins-library/pkg/piperenv"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TargetImage struct {
|
|
|
|
ContainerImageName string
|
|
|
|
ContainerImageTag string
|
|
|
|
ContainerRegistry *url.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetTargetImage(imageRegistry, imageName, imageTag, projectID, envRootPath string) (*TargetImage, error) {
|
|
|
|
if imageRegistry == "" || imageTag == "" {
|
|
|
|
return nil, errors.New("containerRegistryUrl and containerImageTag must be present")
|
|
|
|
}
|
|
|
|
|
|
|
|
targetImage := &TargetImage{
|
|
|
|
ContainerImageTag: strings.ReplaceAll(imageTag, "+", "-"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if matched, _ := regexp.MatchString("^(http|https)://.*", imageRegistry); !matched {
|
|
|
|
imageRegistry = fmt.Sprintf("https://%s", imageRegistry)
|
|
|
|
}
|
|
|
|
|
|
|
|
url, err := url.ParseRequestURI(imageRegistry)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "invalid registry url")
|
|
|
|
}
|
|
|
|
targetImage.ContainerRegistry = url
|
|
|
|
|
|
|
|
cpePath := filepath.Join(envRootPath, "commonPipelineEnvironment")
|
|
|
|
gitRepository := piperenv.GetResourceParameter(cpePath, "git", "repository")
|
2022-03-09 15:06:26 +02:00
|
|
|
githubRepository := piperenv.GetResourceParameter(cpePath, "github", "repository")
|
2022-01-14 17:49:45 +02:00
|
|
|
|
|
|
|
if imageName != "" {
|
|
|
|
targetImage.ContainerImageName = imageName
|
|
|
|
} else if projectID != "" {
|
|
|
|
name := strings.ReplaceAll(projectID, ".", "-")
|
|
|
|
targetImage.ContainerImageName = name
|
|
|
|
} else if gitRepository != "" {
|
|
|
|
targetImage.ContainerImageName = strings.ReplaceAll(gitRepository, ".", "-")
|
2022-03-09 15:06:26 +02:00
|
|
|
} else if githubRepository != "" {
|
|
|
|
targetImage.ContainerImageName = strings.ReplaceAll(githubRepository, ".", "-")
|
2022-01-14 17:49:45 +02:00
|
|
|
} else {
|
|
|
|
return nil, errors.New("failed to derive default for image name")
|
|
|
|
}
|
|
|
|
|
|
|
|
return targetImage, nil
|
|
|
|
}
|