1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-18 05:18:24 +02:00

artifactPrepareVersion: define versioning scheme (#1457)

This commit is contained in:
Oliver Nocon 2020-04-24 20:52:16 +02:00 committed by GitHub
parent 8e871e5c38
commit a728416e12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 33 deletions

View File

@ -75,8 +75,9 @@ func runArtifactPrepareVersion(config *artifactPrepareVersionOptions, telemetryD
GlobalSettingsFile: config.GlobalSettingsFile,
M2Path: config.M2Path,
ProjectSettingsFile: config.ProjectSettingsFile,
VersionField: config.CustomversionField,
VersionField: config.CustomVersionField,
VersionSection: config.CustomVersionSection,
VersioningScheme: config.CustomVersioningScheme,
}
var err error

View File

@ -16,21 +16,22 @@ import (
)
type artifactPrepareVersionOptions struct {
BuildTool string `json:"buildTool,omitempty"`
CommitUserName string `json:"commitUserName,omitempty"`
CustomversionField string `json:"customversionField,omitempty"`
CustomVersionSection string `json:"customVersionSection,omitempty"`
DockerVersionSource string `json:"dockerVersionSource,omitempty"`
FilePath string `json:"filePath,omitempty"`
GlobalSettingsFile string `json:"globalSettingsFile,omitempty"`
IncludeCommitID bool `json:"includeCommitId,omitempty"`
M2Path string `json:"m2Path,omitempty"`
Password string `json:"password,omitempty"`
ProjectSettingsFile string `json:"projectSettingsFile,omitempty"`
TagPrefix string `json:"tagPrefix,omitempty"`
Username string `json:"username,omitempty"`
VersioningTemplate string `json:"versioningTemplate,omitempty"`
VersioningType string `json:"versioningType,omitempty"`
BuildTool string `json:"buildTool,omitempty"`
CommitUserName string `json:"commitUserName,omitempty"`
CustomVersionField string `json:"customVersionField,omitempty"`
CustomVersionSection string `json:"customVersionSection,omitempty"`
CustomVersioningScheme string `json:"customVersioningScheme,omitempty"`
DockerVersionSource string `json:"dockerVersionSource,omitempty"`
FilePath string `json:"filePath,omitempty"`
GlobalSettingsFile string `json:"globalSettingsFile,omitempty"`
IncludeCommitID bool `json:"includeCommitId,omitempty"`
M2Path string `json:"m2Path,omitempty"`
Password string `json:"password,omitempty"`
ProjectSettingsFile string `json:"projectSettingsFile,omitempty"`
TagPrefix string `json:"tagPrefix,omitempty"`
Username string `json:"username,omitempty"`
VersioningTemplate string `json:"versioningTemplate,omitempty"`
VersioningType string `json:"versioningType,omitempty"`
}
type artifactPrepareVersionCommonPipelineEnvironment struct {
@ -171,8 +172,9 @@ Define ` + "`" + `buildTool: custom` + "`" + `, ` + "`" + `filePath: <path to yo
func addArtifactPrepareVersionFlags(cmd *cobra.Command, stepConfig *artifactPrepareVersionOptions) {
cmd.Flags().StringVar(&stepConfig.BuildTool, "buildTool", os.Getenv("PIPER_buildTool"), "Defines the tool which is used for building the artifact. Supports `custom`, `dub`, `golang`, `maven`, `mta`, `npm`, `pip`, `sbt`.")
cmd.Flags().StringVar(&stepConfig.CommitUserName, "commitUserName", "Project Piper", "Defines the user name which appears in version control for the versioning update (in case `versioningType: cloud`).")
cmd.Flags().StringVar(&stepConfig.CustomversionField, "customversionField", os.Getenv("PIPER_customversionField"), "For `buildTool: custom`: Defines the field which contains the version in the descriptor file.")
cmd.Flags().StringVar(&stepConfig.CustomVersionField, "customVersionField", os.Getenv("PIPER_customVersionField"), "For `buildTool: custom`: Defines the field which contains the version in the descriptor file.")
cmd.Flags().StringVar(&stepConfig.CustomVersionSection, "customVersionSection", os.Getenv("PIPER_customVersionSection"), "For `buildTool: custom`: Defines the section for version retrieval in vase a *.ini/*.cfg file is used.")
cmd.Flags().StringVar(&stepConfig.CustomVersioningScheme, "customVersioningScheme", os.Getenv("PIPER_customVersioningScheme"), "For `buildTool: custom`: Defines the versioning scheme to be used (possible options `pep440`, `maven`, `semver2`).")
cmd.Flags().StringVar(&stepConfig.DockerVersionSource, "dockerVersionSource", os.Getenv("PIPER_dockerVersionSource"), "For `buildTool: docker`: Defines the source of the version. Can be `FROM`, any supported _buildTool_ or an environment variable name.")
cmd.Flags().StringVar(&stepConfig.FilePath, "filePath", os.Getenv("PIPER_filePath"), "Defines a custom path to the descriptor file. Build tool specific defaults are used (e.g. `maven: pom.xml`, `npm: package.json`, `mta: mta.yaml`).")
cmd.Flags().StringVar(&stepConfig.GlobalSettingsFile, "globalSettingsFile", os.Getenv("PIPER_globalSettingsFile"), "Maven only - Path to the mvn settings file that should be used as global settings file.")
@ -215,7 +217,7 @@ func artifactPrepareVersionMetadata() config.StepData {
Aliases: []config.Alias{{Name: "gitUserName"}},
},
{
Name: "customversionField",
Name: "customVersionField",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
@ -230,6 +232,14 @@ func artifactPrepareVersionMetadata() config.StepData {
Mandatory: false,
Aliases: []config.Alias{},
},
{
Name: "customVersioningScheme",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: false,
Aliases: []config.Alias{},
},
{
Name: "dockerVersionSource",
ResourceRef: []config.ResourceReference{},

View File

@ -12,12 +12,13 @@ import (
// INIfile defines an artifact using a json file for versioning
type INIfile struct {
path string
content *ini.File
versionSection string
versionField string
readFile func(string) ([]byte, error)
writeFile func(string, []byte, os.FileMode) error
path string
content *ini.File
versioningScheme string
versionSection string
versionField string
readFile func(string) ([]byte, error)
writeFile func(string, []byte, os.FileMode) error
}
func (i *INIfile) init() error {
@ -45,7 +46,10 @@ func (i *INIfile) init() error {
// VersioningScheme returns the relevant versioning scheme
func (i *INIfile) VersioningScheme() string {
return "semver2"
if len(i.versioningScheme) == 0 {
return "semver2"
}
return i.versioningScheme
}
// GetVersion returns the current version of the artifact with a ini-file-based build descriptor

View File

@ -24,6 +24,7 @@ type Options struct {
VersionSource string
VersionSection string
VersionField string
VersioningScheme string
}
type mvnRunner struct{}
@ -46,7 +47,7 @@ func GetArtifact(buildTool, buildDescriptorFilePath string, opts *Options, execR
switch buildTool {
case "custom":
var err error
artifact, err = customArtifact(buildDescriptorFilePath, opts.VersionField, opts.VersionSection)
artifact, err = customArtifact(buildDescriptorFilePath, opts.VersionField, opts.VersionSection, opts.VersioningScheme)
if err != nil {
return artifact, err
}
@ -146,13 +147,14 @@ func searchDescriptor(supported []string, existsFunc func(string) (bool, error))
return descriptor, nil
}
func customArtifact(buildDescriptorFilePath, field, section string) (Artifact, error) {
func customArtifact(buildDescriptorFilePath, field, section, scheme string) (Artifact, error) {
switch filepath.Ext(buildDescriptorFilePath) {
case ".cfg", ".ini":
return &INIfile{
path: buildDescriptorFilePath,
versionField: field,
versionSection: section,
path: buildDescriptorFilePath,
versionField: field,
versionSection: section,
versioningScheme: scheme,
}, nil
case ".json":
return &JSONfile{
@ -166,7 +168,8 @@ func customArtifact(buildDescriptorFilePath, field, section string) (Artifact, e
}, nil
case ".txt", "":
return &Versionfile{
path: buildDescriptorFilePath,
path: buildDescriptorFilePath,
versioningScheme: scheme,
}, nil
default:
return nil, fmt.Errorf("file type not supported: '%v'", buildDescriptorFilePath)

View File

@ -148,21 +148,24 @@ func TestCustomArtifact(t *testing.T) {
file string
field string
section string
scheme string
expected Artifact
expectedErr string
}{
{file: "not.supported", expectedErr: "file type not supported: 'not.supported'"},
{file: "test.cfg", field: "testField", section: "testSection", expected: &INIfile{path: "test.cfg", versionField: "testField", versionSection: "testSection"}},
{file: "test.ini", field: "testField", section: "testSection", expected: &INIfile{path: "test.ini", versionField: "testField", versionSection: "testSection"}},
{file: "test.ini", field: "testField", section: "testSection", scheme: "maven", expected: &INIfile{path: "test.ini", versionField: "testField", versionSection: "testSection", versioningScheme: "maven"}},
{file: "test.json", field: "testField", expected: &JSONfile{path: "test.json", versionField: "testField"}},
{file: "test.yaml", field: "testField", expected: &YAMLfile{path: "test.yaml", versionField: "testField"}},
{file: "test.yml", field: "testField", expected: &YAMLfile{path: "test.yml", versionField: "testField"}},
{file: "test.txt", expected: &Versionfile{path: "test.txt"}},
{file: "test", expected: &Versionfile{path: "test"}},
{file: "test", scheme: "maven", expected: &Versionfile{path: "test", versioningScheme: "maven"}},
}
for _, test := range tt {
res, err := customArtifact(test.file, test.field, test.section)
res, err := customArtifact(test.file, test.field, test.section, test.scheme)
if len(test.expectedErr) == 0 {
assert.NoError(t, err)

View File

@ -91,7 +91,7 @@ spec:
- STAGES
- STEPS
default: Project Piper
- name: customversionField
- name: customVersionField
type: string
description: "For `buildTool: custom`: Defines the field which contains the version in the descriptor file."
scope:
@ -105,6 +105,13 @@ spec:
- PARAMETERS
- STAGES
- STEPS
- name: customVersioningScheme
type: string
description: "For `buildTool: custom`: Defines the versioning scheme to be used (possible options `pep440`, `maven`, `semver2`)."
scope:
- PARAMETERS
- STAGES
- STEPS
- name: dockerVersionSource
type: string
description: "For `buildTool: docker`: Defines the source of the version. Can be `FROM`, any supported _buildTool_ or an environment variable name."