github.com/xgoffin/jenkins-library@v1.154.0/cmd/gradleExecuteBuild_generated.go (about) 1 // Code generated by piper's step-generator. DO NOT EDIT. 2 3 package cmd 4 5 import ( 6 "fmt" 7 "os" 8 "reflect" 9 "strings" 10 "time" 11 12 "github.com/SAP/jenkins-library/pkg/config" 13 "github.com/SAP/jenkins-library/pkg/gcs" 14 "github.com/SAP/jenkins-library/pkg/log" 15 "github.com/SAP/jenkins-library/pkg/splunk" 16 "github.com/SAP/jenkins-library/pkg/telemetry" 17 "github.com/SAP/jenkins-library/pkg/validation" 18 "github.com/bmatcuk/doublestar" 19 "github.com/spf13/cobra" 20 ) 21 22 type gradleExecuteBuildOptions struct { 23 Path string `json:"path,omitempty"` 24 Task string `json:"task,omitempty"` 25 CreateBOM bool `json:"createBOM,omitempty"` 26 } 27 28 type gradleExecuteBuildReports struct { 29 } 30 31 func (p *gradleExecuteBuildReports) persist(stepConfig gradleExecuteBuildOptions, gcpJsonKeyFilePath string, gcsBucketId string, gcsFolderPath string, gcsSubFolder string) { 32 if gcsBucketId == "" { 33 log.Entry().Info("persisting reports to GCS is disabled, because gcsBucketId is empty") 34 return 35 } 36 log.Entry().Info("Uploading reports to Google Cloud Storage...") 37 content := []gcs.ReportOutputParam{ 38 {FilePattern: "**/bom.xml", ParamRef: "", StepResultType: "sbom"}, 39 } 40 envVars := []gcs.EnvVar{ 41 {Name: "GOOGLE_APPLICATION_CREDENTIALS", Value: gcpJsonKeyFilePath, Modified: false}, 42 } 43 gcsClient, err := gcs.NewClient(gcs.WithEnvVars(envVars)) 44 if err != nil { 45 log.Entry().Errorf("creation of GCS client failed: %v", err) 46 return 47 } 48 defer gcsClient.Close() 49 structVal := reflect.ValueOf(&stepConfig).Elem() 50 inputParameters := map[string]string{} 51 for i := 0; i < structVal.NumField(); i++ { 52 field := structVal.Type().Field(i) 53 if field.Type.String() == "string" { 54 paramName := strings.Split(field.Tag.Get("json"), ",") 55 paramValue, _ := structVal.Field(i).Interface().(string) 56 inputParameters[paramName[0]] = paramValue 57 } 58 } 59 if err := gcs.PersistReportsToGCS(gcsClient, content, inputParameters, gcsFolderPath, gcsBucketId, gcsSubFolder, doublestar.Glob, os.Stat); err != nil { 60 log.Entry().Errorf("failed to persist reports: %v", err) 61 } 62 } 63 64 // GradleExecuteBuildCommand This step runs a gradle build command with parameters provided to the step. 65 func GradleExecuteBuildCommand() *cobra.Command { 66 const STEP_NAME = "gradleExecuteBuild" 67 68 metadata := gradleExecuteBuildMetadata() 69 var stepConfig gradleExecuteBuildOptions 70 var startTime time.Time 71 var reports gradleExecuteBuildReports 72 var logCollector *log.CollectorHook 73 var splunkClient *splunk.Splunk 74 telemetryClient := &telemetry.Telemetry{} 75 76 var createGradleExecuteBuildCmd = &cobra.Command{ 77 Use: STEP_NAME, 78 Short: "This step runs a gradle build command with parameters provided to the step.", 79 Long: `This step runs a gradle build command with parameters provided to the step.`, 80 PreRunE: func(cmd *cobra.Command, _ []string) error { 81 startTime = time.Now() 82 log.SetStepName(STEP_NAME) 83 log.SetVerbose(GeneralConfig.Verbose) 84 85 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 86 87 path, _ := os.Getwd() 88 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 89 log.RegisterHook(fatalHook) 90 91 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 92 if err != nil { 93 log.SetErrorCategory(log.ErrorConfiguration) 94 return err 95 } 96 97 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 98 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 99 log.RegisterHook(&sentryHook) 100 } 101 102 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 103 splunkClient = &splunk.Splunk{} 104 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 105 log.RegisterHook(logCollector) 106 } 107 108 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 109 if err != nil { 110 return err 111 } 112 if err = validation.ValidateStruct(stepConfig); err != nil { 113 log.SetErrorCategory(log.ErrorConfiguration) 114 return err 115 } 116 117 return nil 118 }, 119 Run: func(_ *cobra.Command, _ []string) { 120 stepTelemetryData := telemetry.CustomData{} 121 stepTelemetryData.ErrorCode = "1" 122 handler := func() { 123 reports.persist(stepConfig, GeneralConfig.GCPJsonKeyFilePath, GeneralConfig.GCSBucketId, GeneralConfig.GCSFolderPath, GeneralConfig.GCSSubFolder) 124 config.RemoveVaultSecretFiles() 125 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 126 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 127 stepTelemetryData.PiperCommitHash = GitCommit 128 telemetryClient.SetData(&stepTelemetryData) 129 telemetryClient.Send() 130 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 131 splunkClient.Send(telemetryClient.GetData(), logCollector) 132 } 133 } 134 log.DeferExitHandler(handler) 135 defer handler() 136 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) 137 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 138 splunkClient.Initialize(GeneralConfig.CorrelationID, 139 GeneralConfig.HookConfig.SplunkConfig.Dsn, 140 GeneralConfig.HookConfig.SplunkConfig.Token, 141 GeneralConfig.HookConfig.SplunkConfig.Index, 142 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 143 } 144 gradleExecuteBuild(stepConfig, &stepTelemetryData) 145 stepTelemetryData.ErrorCode = "0" 146 log.Entry().Info("SUCCESS") 147 }, 148 } 149 150 addGradleExecuteBuildFlags(createGradleExecuteBuildCmd, &stepConfig) 151 return createGradleExecuteBuildCmd 152 } 153 154 func addGradleExecuteBuildFlags(cmd *cobra.Command, stepConfig *gradleExecuteBuildOptions) { 155 cmd.Flags().StringVar(&stepConfig.Path, "path", os.Getenv("PIPER_path"), "Path to the folder with build.gradle (or build.gradle.kts) file which should be executed.") 156 cmd.Flags().StringVar(&stepConfig.Task, "task", `build`, "Gradle task that should be executed.") 157 cmd.Flags().BoolVar(&stepConfig.CreateBOM, "createBOM", false, "Creates the bill of materials (BOM) using CycloneDX plugin.") 158 159 } 160 161 // retrieve step metadata 162 func gradleExecuteBuildMetadata() config.StepData { 163 var theMetaData = config.StepData{ 164 Metadata: config.StepMetadata{ 165 Name: "gradleExecuteBuild", 166 Aliases: []config.Alias{}, 167 Description: "This step runs a gradle build command with parameters provided to the step.", 168 }, 169 Spec: config.StepSpec{ 170 Inputs: config.StepInputs{ 171 Parameters: []config.StepParameters{ 172 { 173 Name: "path", 174 ResourceRef: []config.ResourceReference{}, 175 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 176 Type: "string", 177 Mandatory: false, 178 Aliases: []config.Alias{{Name: "buildGradlePath"}}, 179 Default: os.Getenv("PIPER_path"), 180 }, 181 { 182 Name: "task", 183 ResourceRef: []config.ResourceReference{}, 184 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 185 Type: "string", 186 Mandatory: false, 187 Aliases: []config.Alias{}, 188 Default: `build`, 189 }, 190 { 191 Name: "createBOM", 192 ResourceRef: []config.ResourceReference{}, 193 Scope: []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS"}, 194 Type: "bool", 195 Mandatory: false, 196 Aliases: []config.Alias{}, 197 Default: false, 198 }, 199 }, 200 }, 201 Containers: []config.Container{ 202 {Name: "gradle", Image: "gradle:6-jdk11-alpine"}, 203 }, 204 Outputs: config.StepOutputs{ 205 Resources: []config.StepResources{ 206 { 207 Name: "reports", 208 Type: "reports", 209 Parameters: []map[string]interface{}{ 210 {"filePattern": "**/bom.xml", "type": "sbom"}, 211 }, 212 }, 213 }, 214 }, 215 }, 216 } 217 return theMetaData 218 }