diff --git a/cmd/artifactPrepareVersion.go b/cmd/artifactPrepareVersion.go index 7afaa8562..98dd7d409 100644 --- a/cmd/artifactPrepareVersion.go +++ b/cmd/artifactPrepareVersion.go @@ -152,10 +152,9 @@ func runArtifactPrepareVersion(config *artifactPrepareVersionOptions, telemetryD commonPipelineEnvironment.git.headCommitID = gitCommitID newVersion := version + now := time.Now() if config.VersioningType == "cloud" || config.VersioningType == "cloud_noTag" { - now := time.Now() - // make sure that versioning does not create tags (when set to "cloud") // for PR pipelines, optimized pipelines (= no build) provider, err := utils.NewOrchestratorSpecificConfigProvider() @@ -196,7 +195,7 @@ func runArtifactPrepareVersion(config *artifactPrepareVersionOptions, telemetryD // propagate version information to additional descriptors if len(config.AdditionalTargetTools) > 0 { - err = propagateVersion(config, utils, &artifactOpts, newVersion, gitCommitID, now) + err = propagateVersion(config, utils, &artifactOpts, version, gitCommitID, now) if err != nil { return err } @@ -212,6 +211,14 @@ func runArtifactPrepareVersion(config *artifactPrepareVersionOptions, telemetryD return errors.Wrapf(err, "failed to push changes for version '%v'", newVersion) } } + } else { + // propagate version information to additional descriptors + if len(config.AdditionalTargetTools) > 0 { + err = propagateVersion(config, utils, &artifactOpts, version, gitCommitID, now) + if err != nil { + return err + } + } } log.Entry().Infof("New version: '%v'", newVersion) @@ -480,7 +487,7 @@ func calculateCloudVersion(artifact versioning.Artifact, config *artifactPrepare return newVersion, nil } -func propagateVersion(config *artifactPrepareVersionOptions, utils artifactPrepareVersionUtils, artifactOpts *versioning.Options, newVersion, gitCommitID string, now time.Time) error { +func propagateVersion(config *artifactPrepareVersionOptions, utils artifactPrepareVersionUtils, artifactOpts *versioning.Options, version, gitCommitID string, now time.Time) error { var err error if len(config.AdditionalTargetDescriptors) > 0 && len(config.AdditionalTargetTools) != len(config.AdditionalTargetDescriptors) { @@ -518,9 +525,9 @@ func propagateVersion(config *artifactPrepareVersionOptions, utils artifactPrepa } // Make sure that version type fits to target artifact - var descriptorVersion string + descriptorVersion := version if config.VersioningType == "cloud" || config.VersioningType == "cloud_noTag" { - descriptorVersion, err = calculateCloudVersion(targetArtifact, config, newVersion, gitCommitID, now) + descriptorVersion, err = calculateCloudVersion(targetArtifact, config, version, gitCommitID, now) if err != nil { return err } diff --git a/cmd/artifactPrepareVersion_test.go b/cmd/artifactPrepareVersion_test.go index de541a6b7..91b9ee2c6 100644 --- a/cmd/artifactPrepareVersion_test.go +++ b/cmd/artifactPrepareVersion_test.go @@ -799,6 +799,7 @@ func TestPropagateVersion(t *testing.T) { VersioningType: "cloud", AdditionalTargetTools: []string{"helm"}, AdditionalTargetDescriptors: []string{"myChart/Chart.yaml"}, + IncludeCommitID: true, } chartMetadata := chart.Metadata{Version: "1.2.3"} @@ -811,6 +812,43 @@ func TestPropagateVersion(t *testing.T) { err = propagateVersion(&config, utils, &artifactOpts, "1.2.4", gitCommitID, testTime) assert.NoError(t, err) + + chartContent, err := utils.FileRead("myChart/Chart.yaml") + assert.NoError(t, err) + chartMeta := chart.Metadata{} + err = yaml.Unmarshal(chartContent, &chartMeta) + assert.NoError(t, err) + + assert.Equal(t, "1.2.4-20200101000000+theGitCommitId", chartMeta.AppVersion) + assert.Equal(t, "1.2.4-20200101000000+theGitCommitId", chartMeta.Version) + }) + + t.Run("success case - dedicated build descriptors / no cloud", func(t *testing.T) { + config := artifactPrepareVersionOptions{ + VersioningType: "library", + AdditionalTargetTools: []string{"helm"}, + AdditionalTargetDescriptors: []string{"myChart/Chart.yaml"}, + } + + chartMetadata := chart.Metadata{Version: "1.2.3"} + content, err := yaml.Marshal(chartMetadata) + assert.NoError(t, err) + + utils := newArtifactPrepareVersionMockUtils() + utils.AddFile("myChart/Chart.yaml", content) + artifactOpts := versioning.Options{} + + err = propagateVersion(&config, utils, &artifactOpts, "1.2.4", gitCommitID, testTime) + assert.NoError(t, err) + + chartContent, err := utils.FileRead("myChart/Chart.yaml") + assert.NoError(t, err) + chartMeta := chart.Metadata{} + err = yaml.Unmarshal(chartContent, &chartMeta) + assert.NoError(t, err) + + assert.Equal(t, "1.2.4", chartMeta.AppVersion) + assert.Equal(t, "1.2.4", chartMeta.Version) }) t.Run("success case - noop", func(t *testing.T) {