1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00

fix(versioning): correct propagated version (#3778)

This commit is contained in:
Oliver Nocon 2022-05-16 10:02:25 +02:00 committed by GitHub
parent e6724d7f05
commit 57347702d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 6 deletions

View File

@ -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
}

View File

@ -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) {