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

feat(commonPipelineEnvironment): add artifact coordinates (#2867)

* add coordinates to CPE

* revert go.mod changes
This commit is contained in:
Oliver Nocon 2021-06-02 15:41:05 +02:00 committed by GitHub
parent dbb1740f27
commit d8a8a73184
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 116 additions and 1 deletions

View File

@ -206,6 +206,17 @@ func runArtifactPrepareVersion(config *artifactPrepareVersionOptions, telemetryD
commonPipelineEnvironment.originalArtifactVersion = version
commonPipelineEnvironment.git.commitMessage = gitCommitMessage
// we may replace GetVersion() above with GetCoordinates() at some point ...
if config.FetchCoordinates {
coordinates, err := artifact.GetCoordinates()
if err != nil {
return fmt.Errorf("failed to get coordinates: %w", err)
}
commonPipelineEnvironment.artifactID = coordinates.ArtifactID
commonPipelineEnvironment.groupID = coordinates.GroupID
commonPipelineEnvironment.packaging = coordinates.Packaging
}
return nil
}

View File

@ -23,6 +23,7 @@ type artifactPrepareVersionOptions struct {
CustomVersionSection string `json:"customVersionSection,omitempty"`
CustomVersioningScheme string `json:"customVersioningScheme,omitempty"`
DockerVersionSource string `json:"dockerVersionSource,omitempty"`
FetchCoordinates bool `json:"fetchCoordinates,omitempty"`
FilePath string `json:"filePath,omitempty"`
GlobalSettingsFile string `json:"globalSettingsFile,omitempty"`
IncludeCommitID bool `json:"includeCommitId,omitempty"`
@ -40,6 +41,9 @@ type artifactPrepareVersionOptions struct {
type artifactPrepareVersionCommonPipelineEnvironment struct {
artifactVersion string
originalArtifactVersion string
artifactID string
groupID string
packaging string
git struct {
commitID string
headCommitID string
@ -55,6 +59,9 @@ func (p *artifactPrepareVersionCommonPipelineEnvironment) persist(path, resource
}{
{category: "", name: "artifactVersion", value: p.artifactVersion},
{category: "", name: "originalArtifactVersion", value: p.originalArtifactVersion},
{category: "", name: "artifactId", value: p.artifactID},
{category: "", name: "groupId", value: p.groupID},
{category: "", name: "packaging", value: p.packaging},
{category: "git", name: "commitId", value: p.git.commitID},
{category: "git", name: "headCommitId", value: p.git.headCommitID},
{category: "git", name: "commitMessage", value: p.git.commitMessage},
@ -218,6 +225,7 @@ func addArtifactPrepareVersionFlags(cmd *cobra.Command, stepConfig *artifactPrep
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().BoolVar(&stepConfig.FetchCoordinates, "fetchCoordinates", false, "If set to `true` the step will retreive artifact coordinates and store them in the common pipeline environment.")
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.")
cmd.Flags().BoolVar(&stepConfig.IncludeCommitID, "includeCommitId", true, "Defines if the automatically generated version (`versioningType: cloud`) should include the commit id hash.")
@ -293,6 +301,14 @@ func artifactPrepareVersionMetadata() config.StepData {
Mandatory: false,
Aliases: []config.Alias{},
},
{
Name: "fetchCoordinates",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "bool",
Mandatory: false,
Aliases: []config.Alias{},
},
{
Name: "filePath",
ResourceRef: []config.ResourceReference{},
@ -426,6 +442,9 @@ func artifactPrepareVersionMetadata() config.StepData {
Parameters: []map[string]interface{}{
{"Name": "artifactVersion"},
{"Name": "originalArtifactVersion"},
{"Name": "artifactId"},
{"Name": "groupId"},
{"Name": "packaging"},
{"Name": "git/commitId"},
{"Name": "git/headCommitId"},
{"Name": "git/commitMessage"},

View File

@ -25,6 +25,8 @@ type artifactVersioningMock struct {
setVersionError string
initCalled bool
versioningScheme string
coordinates versioning.Coordinates
coordinatesError error
}
func (a *artifactVersioningMock) VersioningScheme() string {
@ -47,7 +49,10 @@ func (a *artifactVersioningMock) SetVersion(version string) error {
}
func (a *artifactVersioningMock) GetCoordinates() (versioning.Coordinates, error) {
return versioning.Coordinates{}, fmt.Errorf("not implemented")
if a.coordinatesError != nil {
return versioning.Coordinates{}, a.coordinatesError
}
return a.coordinates, nil
}
type gitRepositoryMock struct {
@ -303,6 +308,36 @@ func TestRunArtifactPrepareVersion(t *testing.T) {
assert.Equal(t, repo.revisionHash.String(), cpe.git.commitID)
})
t.Run("success case - coordinates", func(t *testing.T) {
config := artifactPrepareVersionOptions{
BuildTool: "maven",
VersioningType: "library",
FetchCoordinates: true,
}
cpe := artifactPrepareVersionCommonPipelineEnvironment{}
versioningMock := artifactVersioningMock{
originalVersion: "1.2.3",
versioningScheme: "maven",
coordinates: versioning.Coordinates{GroupID: "my.testGroup", ArtifactID: "testArtifact", Packaging: "testPackaging"},
}
worktree := gitWorktreeMock{
commitHash: plumbing.ComputeHash(plumbing.CommitObject, []byte{2, 3, 4}),
}
repo := gitRepositoryMock{
revisionHash: plumbing.ComputeHash(plumbing.CommitObject, []byte{1, 2, 3}),
}
err := runArtifactPrepareVersion(&config, &telemetry.CustomData{}, &cpe, &versioningMock, nil, &repo, func(r gitRepository) (gitWorktree, error) { return &worktree, nil })
assert.NoError(t, err)
assert.Equal(t, "testArtifact", cpe.artifactID)
assert.Equal(t, "my.testGroup", cpe.groupID)
assert.Equal(t, "testPackaging", cpe.packaging)
})
t.Run("error - failed to retrieve version", func(t *testing.T) {
config := artifactPrepareVersionOptions{}
@ -412,6 +447,33 @@ func TestRunArtifactPrepareVersion(t *testing.T) {
err := runArtifactPrepareVersion(&config, &telemetry.CustomData{}, &artifactPrepareVersionCommonPipelineEnvironment{}, &versioningMock, nil, &repo, func(r gitRepository) (gitWorktree, error) { return &worktree, nil })
assert.Contains(t, fmt.Sprint(err), "failed to push changes for version '1.2.3")
})
t.Run("error - failed to get coordinates", func(t *testing.T) {
config := artifactPrepareVersionOptions{
BuildTool: "maven",
VersioningType: "library",
FetchCoordinates: true,
}
cpe := artifactPrepareVersionCommonPipelineEnvironment{}
versioningMock := artifactVersioningMock{
originalVersion: "1.2.3",
versioningScheme: "maven",
coordinatesError: fmt.Errorf("coordinatesError"),
}
worktree := gitWorktreeMock{
commitHash: plumbing.ComputeHash(plumbing.CommitObject, []byte{2, 3, 4}),
}
repo := gitRepositoryMock{
revisionHash: plumbing.ComputeHash(plumbing.CommitObject, []byte{1, 2, 3}),
}
err := runArtifactPrepareVersion(&config, &telemetry.CustomData{}, &cpe, &versioningMock, nil, &repo, func(r gitRepository) (gitWorktree, error) { return &worktree, nil })
assert.EqualError(t, err, "failed to get coordinates: coordinatesError")
})
}
func TestVersioningTemplate(t *testing.T) {

1
go.sum
View File

@ -1713,6 +1713,7 @@ golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

View File

@ -139,6 +139,13 @@ spec:
- PARAMETERS
- STAGES
- STEPS
- name: fetchCoordinates
type: bool
description: If set to `true` the step will retreive artifact coordinates and store them in the common pipeline environment.
scope:
- PARAMETERS
- STAGES
- STEPS
- name: filePath
type: string
description: "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`)."
@ -269,6 +276,9 @@ spec:
params:
- name: artifactVersion
- name: originalArtifactVersion
- name: artifactId
- name: groupId
- name: packaging
- name: git/commitId
- name: git/headCommitId
- name: git/commitMessage

View File

@ -13,6 +13,11 @@ class commonPipelineEnvironment implements Serializable {
def artifactVersion
def originalArtifactVersion
// stores additional artifact coordinates
def artifactId
def groupId
def packaging
//stores the build tools if it inferred automatically, e.g. in the SAP Cloud SDK pipeline
String buildTool
@ -69,6 +74,10 @@ class commonPipelineEnvironment implements Serializable {
artifactVersion = null
originalArtifactVersion = null
artifactId = null
groupId = null
packaging = null
buildTool = null
configuration = [:]
@ -180,6 +189,9 @@ class commonPipelineEnvironment implements Serializable {
def files = [
[filename: '.pipeline/commonPipelineEnvironment/artifactVersion', property: 'artifactVersion'],
[filename: '.pipeline/commonPipelineEnvironment/artifactId', property: 'artifactId'],
[filename: '.pipeline/commonPipelineEnvironment/groupId', property: 'groupId'],
[filename: '.pipeline/commonPipelineEnvironment/packaging', property: 'packaging'],
[filename: '.pipeline/commonPipelineEnvironment/buildTool', property: 'buildTool'],
[filename: '.pipeline/commonPipelineEnvironment/originalArtifactVersion', property: 'originalArtifactVersion'],
[filename: '.pipeline/commonPipelineEnvironment/github/owner', property: 'githubOrg'],