diff --git a/cmd/gradleExecuteBuild.go b/cmd/gradleExecuteBuild.go index 8a635adf2b..6fe9361624 100644 --- a/cmd/gradleExecuteBuild.go +++ b/cmd/gradleExecuteBuild.go @@ -9,6 +9,7 @@ import ( "strings" "text/template" + "github.com/SAP/jenkins-library/pkg/buildsettings" "github.com/SAP/jenkins-library/pkg/command" "github.com/SAP/jenkins-library/pkg/gradle" "github.com/SAP/jenkins-library/pkg/log" @@ -18,7 +19,8 @@ import ( ) const ( - gradleBomFilename = "bom-gradle" + gradleBomFilename = "bom-gradle" + stepNameForBuildSettings = "gradleExecuteBuild" ) var ( @@ -189,6 +191,25 @@ func runGradleExecuteBuild(config *gradleExecuteBuildOptions, telemetryData *tel return err } + log.Entry().Debugf("creating build settings information...") + + dockerImage, err := GetDockerImageValue(stepNameForBuildSettings) + if err != nil { + return fmt.Errorf("failed to retrieve dockerImage configuration: %w", err) + } + + gradleConfig := buildsettings.BuildOptions{ + CreateBOM: config.CreateBOM, + Publish: config.Publish, + BuildSettingsInfo: config.BuildSettingsInfo, + DockerImage: dockerImage, + } + buildSettingsInfo, err := buildsettings.CreateBuildSettingsInfo(&gradleConfig, stepNameForBuildSettings) + if err != nil { + log.Entry().Warnf("failed to create build settings info: %v", err) + } + pipelineEnv.custom.buildSettingsInfo = buildSettingsInfo + log.Entry().Info("Publishing of artifacts to staging repository...") if config.Publish { if err := publishArtifacts(config, utils, pipelineEnv); err != nil { diff --git a/cmd/gradleExecuteBuild_generated.go b/cmd/gradleExecuteBuild_generated.go index 585fb16420..7f60984f84 100644 --- a/cmd/gradleExecuteBuild_generated.go +++ b/cmd/gradleExecuteBuild_generated.go @@ -38,6 +38,7 @@ type gradleExecuteBuildOptions struct { ExcludeCreateBOMForProjects []string `json:"excludeCreateBOMForProjects,omitempty"` ExcludePublishingForProjects []string `json:"excludePublishingForProjects,omitempty"` BuildFlags []string `json:"buildFlags,omitempty"` + BuildSettingsInfo string `json:"buildSettingsInfo,omitempty"` } type gradleExecuteBuildReports struct { @@ -78,7 +79,8 @@ func (p *gradleExecuteBuildReports) persist(stepConfig gradleExecuteBuildOptions type gradleExecuteBuildCommonPipelineEnvironment struct { custom struct { - artifacts piperenv.Artifacts + artifacts piperenv.Artifacts + buildSettingsInfo string } } @@ -89,6 +91,7 @@ func (p *gradleExecuteBuildCommonPipelineEnvironment) persist(path, resourceName value interface{} }{ {category: "custom", name: "artifacts", value: p.custom.artifacts}, + {category: "custom", name: "buildSettingsInfo", value: p.custom.buildSettingsInfo}, } errCount := 0 @@ -242,6 +245,7 @@ func addGradleExecuteBuildFlags(cmd *cobra.Command, stepConfig *gradleExecuteBui cmd.Flags().StringSliceVar(&stepConfig.ExcludeCreateBOMForProjects, "excludeCreateBOMForProjects", []string{}, "Defines which projects/subprojects will be ignored during bom creation. Only if applyCreateBOMForAllProjects is set to true") cmd.Flags().StringSliceVar(&stepConfig.ExcludePublishingForProjects, "excludePublishingForProjects", []string{}, "Defines which projects/subprojects will be ignored during publishing. Only if applyCreateBOMForAllProjects is set to true") cmd.Flags().StringSliceVar(&stepConfig.BuildFlags, "buildFlags", []string{}, "Defines a list of tasks and/or arguments to be provided for gradle in the respective order to be executed. This list takes precedence if specified over 'task' parameter") + cmd.Flags().StringVar(&stepConfig.BuildSettingsInfo, "buildSettingsInfo", os.Getenv("PIPER_buildSettingsInfo"), "build settings info is typically filled by the step automatically to create information about the build settings that were used during the gradle build. This information is typically used for compliance related processes.") } @@ -421,6 +425,20 @@ func gradleExecuteBuildMetadata() config.StepData { Aliases: []config.Alias{}, Default: []string{}, }, + { + Name: "buildSettingsInfo", + ResourceRef: []config.ResourceReference{ + { + Name: "commonPipelineEnvironment", + Param: "custom/buildSettingsInfo", + }, + }, + Scope: []string{"STEPS", "STAGES", "PARAMETERS"}, + Type: "string", + Mandatory: false, + Aliases: []config.Alias{}, + Default: os.Getenv("PIPER_buildSettingsInfo"), + }, }, }, Containers: []config.Container{ @@ -440,6 +458,7 @@ func gradleExecuteBuildMetadata() config.StepData { Type: "piperEnvironment", Parameters: []map[string]interface{}{ {"name": "custom/artifacts", "type": "piperenv.Artifacts"}, + {"name": "custom/buildSettingsInfo"}, }, }, }, diff --git a/pkg/buildsettings/buildSettings.go b/pkg/buildsettings/buildSettings.go index 6b66dbc347..20594eb9d1 100644 --- a/pkg/buildsettings/buildSettings.go +++ b/pkg/buildsettings/buildSettings.go @@ -9,15 +9,15 @@ import ( ) type BuildSettings struct { - GolangBuild []BuildOptions `json:"golangBuild,omitempty"` - GradleBuild []BuildOptions `json:"gradleBuild,omitempty"` - HelmExecute []BuildOptions `json:"helmExecute,omitempty"` - KanikoExecute []BuildOptions `json:"kanikoExecute,omitempty"` - MavenBuild []BuildOptions `json:"mavenBuild,omitempty"` - MtaBuild []BuildOptions `json:"mtaBuild,omitempty"` - PythonBuild []BuildOptions `json:"pythonBuild,omitempty"` - NpmExecuteScripts []BuildOptions `json:"npmExecuteScripts,omitempty"` - CnbBuild []BuildOptions `json:"cnbBuild,omitempty"` + GolangBuild []BuildOptions `json:"golangBuild,omitempty"` + GradleExecuteBuild []BuildOptions `json:"gradleExecuteBuild,omitempty"` + HelmExecute []BuildOptions `json:"helmExecute,omitempty"` + KanikoExecute []BuildOptions `json:"kanikoExecute,omitempty"` + MavenBuild []BuildOptions `json:"mavenBuild,omitempty"` + MtaBuild []BuildOptions `json:"mtaBuild,omitempty"` + PythonBuild []BuildOptions `json:"pythonBuild,omitempty"` + NpmExecuteScripts []BuildOptions `json:"npmExecuteScripts,omitempty"` + CnbBuild []BuildOptions `json:"cnbBuild,omitempty"` } type BuildOptions struct { @@ -74,9 +74,9 @@ func CreateBuildSettingsInfo(config *BuildOptions, buildTool string) (string, er jsonResult, err = json.Marshal(BuildSettings{ GolangBuild: settings, }) - case "gradleBuild": + case "gradleExecuteBuild": jsonResult, err = json.Marshal(BuildSettings{ - GradleBuild: settings, + GradleExecuteBuild: settings, }) case "helmExecute": jsonResult, err = json.Marshal(BuildSettings{ diff --git a/pkg/buildsettings/buildSettings_test.go b/pkg/buildsettings/buildSettings_test.go index 7d135b97af..264d2145f2 100644 --- a/pkg/buildsettings/buildSettings_test.go +++ b/pkg/buildsettings/buildSettings_test.go @@ -1,6 +1,3 @@ -//go:build unit -// +build unit - package buildsettings import ( @@ -28,9 +25,9 @@ func TestCreateBuildSettingsInfo(t *testing.T) { expected: "{\"golangBuild\":[{\"dockerImage\":\"golang:latest\"}]}", }, { - config: BuildOptions{CreateBOM: true}, - buildTool: "gradleBuild", - expected: "{\"gradleBuild\":[{\"createBOM\":true}]}", + config: BuildOptions{CreateBOM: true, DockerImage: "gradle:latest"}, + buildTool: "gradleExecuteBuild", + expected: "{\"gradleExecuteBuild\":[{\"createBOM\":true,\"dockerImage\":\"gradle:latest\"}]}", }, { config: BuildOptions{Publish: true}, @@ -77,7 +74,7 @@ func TestCreateBuildSettingsInfo(t *testing.T) { for _, testCase := range testTableConfig { buildSettings, err := CreateBuildSettingsInfo(&testCase.config, testCase.buildTool) assert.Nil(t, err) - assert.Equal(t, buildSettings, testCase.expected) + assert.Equal(t, testCase.expected, buildSettings) } }) diff --git a/resources/metadata/gradleExecuteBuild.yaml b/resources/metadata/gradleExecuteBuild.yaml index e44c92f46e..ed0ba89ce6 100644 --- a/resources/metadata/gradleExecuteBuild.yaml +++ b/resources/metadata/gradleExecuteBuild.yaml @@ -157,6 +157,16 @@ spec: - PARAMETERS - STAGES - STEPS + - name: buildSettingsInfo + type: string + description: build settings info is typically filled by the step automatically to create information about the build settings that were used during the gradle build. This information is typically used for compliance related processes. + scope: + - STEPS + - STAGES + - PARAMETERS + resourceRef: + - name: commonPipelineEnvironment + param: custom/buildSettingsInfo outputs: resources: - name: reports @@ -169,6 +179,7 @@ spec: params: - name: custom/artifacts type: "piperenv.Artifacts" + - name: custom/buildSettingsInfo containers: - name: gradle image: gradle:6-jdk11-alpine