github.com/jaylevin/jenkins-library@v1.230.4/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 Publish bool `json:"publish,omitempty"` 26 RepositoryURL string `json:"repositoryUrl,omitempty"` 27 RepositoryPassword string `json:"repositoryPassword,omitempty"` 28 RepositoryUsername string `json:"repositoryUsername,omitempty"` 29 CreateBOM bool `json:"createBOM,omitempty"` 30 ArtifactVersion string `json:"artifactVersion,omitempty"` 31 ArtifactGroupID string `json:"artifactGroupId,omitempty"` 32 ArtifactID string `json:"artifactId,omitempty"` 33 UseWrapper bool `json:"useWrapper,omitempty"` 34 } 35 36 type gradleExecuteBuildReports struct { 37 } 38 39 func (p *gradleExecuteBuildReports) persist(stepConfig gradleExecuteBuildOptions, gcpJsonKeyFilePath string, gcsBucketId string, gcsFolderPath string, gcsSubFolder string) { 40 if gcsBucketId == "" { 41 log.Entry().Info("persisting reports to GCS is disabled, because gcsBucketId is empty") 42 return 43 } 44 log.Entry().Info("Uploading reports to Google Cloud Storage...") 45 content := []gcs.ReportOutputParam{ 46 {FilePattern: "**/bom.xml", ParamRef: "", StepResultType: "sbom"}, 47 } 48 envVars := []gcs.EnvVar{ 49 {Name: "GOOGLE_APPLICATION_CREDENTIALS", Value: gcpJsonKeyFilePath, Modified: false}, 50 } 51 gcsClient, err := gcs.NewClient(gcs.WithEnvVars(envVars)) 52 if err != nil { 53 log.Entry().Errorf("creation of GCS client failed: %v", err) 54 return 55 } 56 defer gcsClient.Close() 57 structVal := reflect.ValueOf(&stepConfig).Elem() 58 inputParameters := map[string]string{} 59 for i := 0; i < structVal.NumField(); i++ { 60 field := structVal.Type().Field(i) 61 if field.Type.String() == "string" { 62 paramName := strings.Split(field.Tag.Get("json"), ",") 63 paramValue, _ := structVal.Field(i).Interface().(string) 64 inputParameters[paramName[0]] = paramValue 65 } 66 } 67 if err := gcs.PersistReportsToGCS(gcsClient, content, inputParameters, gcsFolderPath, gcsBucketId, gcsSubFolder, doublestar.Glob, os.Stat); err != nil { 68 log.Entry().Errorf("failed to persist reports: %v", err) 69 } 70 } 71 72 // GradleExecuteBuildCommand This step runs a gradle build command with parameters provided to the step. 73 func GradleExecuteBuildCommand() *cobra.Command { 74 const STEP_NAME = "gradleExecuteBuild" 75 76 metadata := gradleExecuteBuildMetadata() 77 var stepConfig gradleExecuteBuildOptions 78 var startTime time.Time 79 var reports gradleExecuteBuildReports 80 var logCollector *log.CollectorHook 81 var splunkClient *splunk.Splunk 82 telemetryClient := &telemetry.Telemetry{} 83 84 var createGradleExecuteBuildCmd = &cobra.Command{ 85 Use: STEP_NAME, 86 Short: "This step runs a gradle build command with parameters provided to the step.", 87 Long: `This step runs a gradle build command with parameters provided to the step.`, 88 PreRunE: func(cmd *cobra.Command, _ []string) error { 89 startTime = time.Now() 90 log.SetStepName(STEP_NAME) 91 log.SetVerbose(GeneralConfig.Verbose) 92 93 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 94 95 path, _ := os.Getwd() 96 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 97 log.RegisterHook(fatalHook) 98 99 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 100 if err != nil { 101 log.SetErrorCategory(log.ErrorConfiguration) 102 return err 103 } 104 log.RegisterSecret(stepConfig.RepositoryPassword) 105 log.RegisterSecret(stepConfig.RepositoryUsername) 106 107 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 108 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 109 log.RegisterHook(&sentryHook) 110 } 111 112 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 113 splunkClient = &splunk.Splunk{} 114 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 115 log.RegisterHook(logCollector) 116 } 117 118 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 119 if err != nil { 120 return err 121 } 122 if err = validation.ValidateStruct(stepConfig); err != nil { 123 log.SetErrorCategory(log.ErrorConfiguration) 124 return err 125 } 126 127 return nil 128 }, 129 Run: func(_ *cobra.Command, _ []string) { 130 stepTelemetryData := telemetry.CustomData{} 131 stepTelemetryData.ErrorCode = "1" 132 handler := func() { 133 reports.persist(stepConfig, GeneralConfig.GCPJsonKeyFilePath, GeneralConfig.GCSBucketId, GeneralConfig.GCSFolderPath, GeneralConfig.GCSSubFolder) 134 config.RemoveVaultSecretFiles() 135 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 136 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 137 stepTelemetryData.PiperCommitHash = GitCommit 138 telemetryClient.SetData(&stepTelemetryData) 139 telemetryClient.Send() 140 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 141 splunkClient.Send(telemetryClient.GetData(), logCollector) 142 } 143 } 144 log.DeferExitHandler(handler) 145 defer handler() 146 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) 147 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 148 splunkClient.Initialize(GeneralConfig.CorrelationID, 149 GeneralConfig.HookConfig.SplunkConfig.Dsn, 150 GeneralConfig.HookConfig.SplunkConfig.Token, 151 GeneralConfig.HookConfig.SplunkConfig.Index, 152 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 153 } 154 gradleExecuteBuild(stepConfig, &stepTelemetryData) 155 stepTelemetryData.ErrorCode = "0" 156 log.Entry().Info("SUCCESS") 157 }, 158 } 159 160 addGradleExecuteBuildFlags(createGradleExecuteBuildCmd, &stepConfig) 161 return createGradleExecuteBuildCmd 162 } 163 164 func addGradleExecuteBuildFlags(cmd *cobra.Command, stepConfig *gradleExecuteBuildOptions) { 165 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.") 166 cmd.Flags().StringVar(&stepConfig.Task, "task", `build`, "Gradle task that should be executed.") 167 cmd.Flags().BoolVar(&stepConfig.Publish, "publish", false, "Configures gradle to publish the artifact to a repository.") 168 cmd.Flags().StringVar(&stepConfig.RepositoryURL, "repositoryUrl", os.Getenv("PIPER_repositoryUrl"), "Url to the repository to which the project artifacts should be published.") 169 cmd.Flags().StringVar(&stepConfig.RepositoryPassword, "repositoryPassword", os.Getenv("PIPER_repositoryPassword"), "Password for the repository to which the project artifacts should be published.") 170 cmd.Flags().StringVar(&stepConfig.RepositoryUsername, "repositoryUsername", os.Getenv("PIPER_repositoryUsername"), "Username for the repository to which the project artifacts should be published.") 171 cmd.Flags().BoolVar(&stepConfig.CreateBOM, "createBOM", false, "Creates the bill of materials (BOM) using CycloneDX plugin.") 172 cmd.Flags().StringVar(&stepConfig.ArtifactVersion, "artifactVersion", os.Getenv("PIPER_artifactVersion"), "Version of the artifact to be built.") 173 cmd.Flags().StringVar(&stepConfig.ArtifactGroupID, "artifactGroupId", os.Getenv("PIPER_artifactGroupId"), "The group of the artifact.") 174 cmd.Flags().StringVar(&stepConfig.ArtifactID, "artifactId", os.Getenv("PIPER_artifactId"), "The name of the artifact.") 175 cmd.Flags().BoolVar(&stepConfig.UseWrapper, "useWrapper", false, "If set to false all commands are executed using 'gradle', otherwise 'gradlew' is executed.") 176 177 } 178 179 // retrieve step metadata 180 func gradleExecuteBuildMetadata() config.StepData { 181 var theMetaData = config.StepData{ 182 Metadata: config.StepMetadata{ 183 Name: "gradleExecuteBuild", 184 Aliases: []config.Alias{}, 185 Description: "This step runs a gradle build command with parameters provided to the step.", 186 }, 187 Spec: config.StepSpec{ 188 Inputs: config.StepInputs{ 189 Parameters: []config.StepParameters{ 190 { 191 Name: "path", 192 ResourceRef: []config.ResourceReference{}, 193 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 194 Type: "string", 195 Mandatory: false, 196 Aliases: []config.Alias{{Name: "buildGradlePath"}}, 197 Default: os.Getenv("PIPER_path"), 198 }, 199 { 200 Name: "task", 201 ResourceRef: []config.ResourceReference{}, 202 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 203 Type: "string", 204 Mandatory: false, 205 Aliases: []config.Alias{}, 206 Default: `build`, 207 }, 208 { 209 Name: "publish", 210 ResourceRef: []config.ResourceReference{}, 211 Scope: []string{"STEPS", "STAGES", "PARAMETERS"}, 212 Type: "bool", 213 Mandatory: false, 214 Aliases: []config.Alias{}, 215 Default: false, 216 }, 217 { 218 Name: "repositoryUrl", 219 ResourceRef: []config.ResourceReference{ 220 { 221 Name: "commonPipelineEnvironment", 222 Param: "custom/repositoryUrl", 223 }, 224 }, 225 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 226 Type: "string", 227 Mandatory: false, 228 Aliases: []config.Alias{}, 229 Default: os.Getenv("PIPER_repositoryUrl"), 230 }, 231 { 232 Name: "repositoryPassword", 233 ResourceRef: []config.ResourceReference{ 234 { 235 Name: "commonPipelineEnvironment", 236 Param: "custom/repositoryPassword", 237 }, 238 }, 239 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 240 Type: "string", 241 Mandatory: false, 242 Aliases: []config.Alias{}, 243 Default: os.Getenv("PIPER_repositoryPassword"), 244 }, 245 { 246 Name: "repositoryUsername", 247 ResourceRef: []config.ResourceReference{ 248 { 249 Name: "commonPipelineEnvironment", 250 Param: "custom/repositoryUsername", 251 }, 252 }, 253 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 254 Type: "string", 255 Mandatory: false, 256 Aliases: []config.Alias{}, 257 Default: os.Getenv("PIPER_repositoryUsername"), 258 }, 259 { 260 Name: "createBOM", 261 ResourceRef: []config.ResourceReference{}, 262 Scope: []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS"}, 263 Type: "bool", 264 Mandatory: false, 265 Aliases: []config.Alias{}, 266 Default: false, 267 }, 268 { 269 Name: "artifactVersion", 270 ResourceRef: []config.ResourceReference{ 271 { 272 Name: "commonPipelineEnvironment", 273 Param: "artifactVersion", 274 }, 275 }, 276 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 277 Type: "string", 278 Mandatory: false, 279 Aliases: []config.Alias{}, 280 Default: os.Getenv("PIPER_artifactVersion"), 281 }, 282 { 283 Name: "artifactGroupId", 284 ResourceRef: []config.ResourceReference{ 285 { 286 Name: "commonPipelineEnvironment", 287 Param: "groupId", 288 }, 289 }, 290 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 291 Type: "string", 292 Mandatory: false, 293 Aliases: []config.Alias{}, 294 Default: os.Getenv("PIPER_artifactGroupId"), 295 }, 296 { 297 Name: "artifactId", 298 ResourceRef: []config.ResourceReference{ 299 { 300 Name: "commonPipelineEnvironment", 301 Param: "artifactId", 302 }, 303 }, 304 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 305 Type: "string", 306 Mandatory: false, 307 Aliases: []config.Alias{}, 308 Default: os.Getenv("PIPER_artifactId"), 309 }, 310 { 311 Name: "useWrapper", 312 ResourceRef: []config.ResourceReference{}, 313 Scope: []string{"STEPS", "STAGES", "PARAMETERS"}, 314 Type: "bool", 315 Mandatory: false, 316 Aliases: []config.Alias{}, 317 Default: false, 318 }, 319 }, 320 }, 321 Containers: []config.Container{ 322 {Name: "gradle", Image: "gradle:6-jdk11-alpine"}, 323 }, 324 Outputs: config.StepOutputs{ 325 Resources: []config.StepResources{ 326 { 327 Name: "reports", 328 Type: "reports", 329 Parameters: []map[string]interface{}{ 330 {"filePattern": "**/bom.xml", "type": "sbom"}, 331 }, 332 }, 333 }, 334 }, 335 }, 336 } 337 return theMetaData 338 }