feat(cnbBuild) read target image name from github cpe (#3620)

Co-authored-by: Johannes Dillmann <j.dillmann@sap.com>
This commit is contained in:
Johannes Dillmann
2022-03-09 14:06:26 +01:00
committed by GitHub
co-authored by Johannes Dillmann
parent 64a00c540a
commit 3708f274cc
4 changed files with 24 additions and 1 deletions
+1 -1
View File
@@ -164,7 +164,7 @@ func CnbBuildCommand() *cobra.Command {
}
func addCnbBuildFlags(cmd *cobra.Command, stepConfig *cnbBuildOptions) {
cmd.Flags().StringVar(&stepConfig.ContainerImageName, "containerImageName", os.Getenv("PIPER_containerImageName"), "Name of the container which will be built\n`cnbBuild` step will try to identify a containerImageName using the following precedence:\n 1. `containerImageName` parameter.\n 2. `project.id` field of a `project.toml` file.\n 3. `git/repository` parameter of the `commonPipelineEnvironment`.\nIf none of the above was found - an error will be raised.\n")
cmd.Flags().StringVar(&stepConfig.ContainerImageName, "containerImageName", os.Getenv("PIPER_containerImageName"), "Name of the container which will be built\n`cnbBuild` step will try to identify a containerImageName using the following precedence:\n 1. `containerImageName` parameter.\n 2. `project.id` field of a `project.toml` file.\n 3. `git/repository` parameter of the `commonPipelineEnvironment`.\n 4. `github/repository` parameter of the `commonPipelineEnvironment`.\nIf none of the above was found - an error will be raised.\n")
cmd.Flags().StringVar(&stepConfig.ContainerImageTag, "containerImageTag", os.Getenv("PIPER_containerImageTag"), "Tag of the container which will be built")
cmd.Flags().StringVar(&stepConfig.ContainerRegistryURL, "containerRegistryUrl", os.Getenv("PIPER_containerRegistryUrl"), "Container registry where the image should be pushed to")
cmd.Flags().StringSliceVar(&stepConfig.Buildpacks, "buildpacks", []string{}, "List of custom buildpacks to use in the form of '$HOSTNAME/$REPO[:$TAG]'.")
+3
View File
@@ -38,6 +38,7 @@ func GetTargetImage(imageRegistry, imageName, imageTag, projectID, envRootPath s
cpePath := filepath.Join(envRootPath, "commonPipelineEnvironment")
gitRepository := piperenv.GetResourceParameter(cpePath, "git", "repository")
githubRepository := piperenv.GetResourceParameter(cpePath, "github", "repository")
if imageName != "" {
targetImage.ContainerImageName = imageName
@@ -46,6 +47,8 @@ func GetTargetImage(imageRegistry, imageName, imageTag, projectID, envRootPath s
targetImage.ContainerImageName = name
} else if gitRepository != "" {
targetImage.ContainerImageName = strings.ReplaceAll(gitRepository, ".", "-")
} else if githubRepository != "" {
targetImage.ContainerImageName = strings.ReplaceAll(githubRepository, ".", "-")
} else {
return nil, errors.New("failed to derive default for image name")
}
+19
View File
@@ -72,6 +72,25 @@ func TestGetImageName(t *testing.T) {
assert.Equal(t, "tag", targetImage.ContainerImageTag)
})
t.Run("Image name is taken from github repo", func(t *testing.T) {
t.Parallel()
tmpdir, err := ioutil.TempDir("", "cpe")
assert.NoError(t, err)
defer os.RemoveAll(tmpdir)
err = os.MkdirAll(filepath.Join(tmpdir, "commonPipelineEnvironment", "github"), os.ModePerm)
assert.NoError(t, err)
err = ioutil.WriteFile(filepath.Join(tmpdir, "commonPipelineEnvironment", "github", "repository"), []byte("repo-name"), os.ModePerm)
assert.NoError(t, err)
targetImage, err := cnbutils.GetTargetImage("http://registry", "", "tag", "", tmpdir)
assert.NoError(t, err)
assert.Equal(t, "repo-name", targetImage.ContainerImageName)
assert.Equal(t, "tag", targetImage.ContainerImageTag)
})
t.Run("throws an error if unable to find image name", func(t *testing.T) {
t.Parallel()
+1
View File
@@ -44,6 +44,7 @@ spec:
1. `containerImageName` parameter.
2. `project.id` field of a `project.toml` file.
3. `git/repository` parameter of the `commonPipelineEnvironment`.
4. `github/repository` parameter of the `commonPipelineEnvironment`.
If none of the above was found - an error will be raised.
scope:
- GENERAL