github.com/jaylevin/jenkins-library@v1.230.4/cmd/abapEnvironmentBuild_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 "path/filepath" 9 "time" 10 11 "github.com/SAP/jenkins-library/pkg/config" 12 "github.com/SAP/jenkins-library/pkg/log" 13 "github.com/SAP/jenkins-library/pkg/piperenv" 14 "github.com/SAP/jenkins-library/pkg/splunk" 15 "github.com/SAP/jenkins-library/pkg/telemetry" 16 "github.com/SAP/jenkins-library/pkg/validation" 17 "github.com/spf13/cobra" 18 ) 19 20 type abapEnvironmentBuildOptions struct { 21 CfAPIEndpoint string `json:"cfApiEndpoint,omitempty"` 22 CfOrg string `json:"cfOrg,omitempty"` 23 CfSpace string `json:"cfSpace,omitempty"` 24 CfServiceInstance string `json:"cfServiceInstance,omitempty"` 25 CfServiceKeyName string `json:"cfServiceKeyName,omitempty"` 26 Host string `json:"host,omitempty"` 27 Username string `json:"username,omitempty"` 28 Password string `json:"password,omitempty"` 29 Phase string `json:"phase,omitempty"` 30 Values string `json:"values,omitempty"` 31 DownloadAllResultFiles bool `json:"downloadAllResultFiles,omitempty"` 32 DownloadResultFilenames []string `json:"downloadResultFilenames,omitempty"` 33 PublishAllDownloadedResultFiles bool `json:"publishAllDownloadedResultFiles,omitempty"` 34 PublishResultFilenames []string `json:"publishResultFilenames,omitempty"` 35 SubDirectoryForDownload string `json:"subDirectoryForDownload,omitempty"` 36 FilenamePrefixForDownload string `json:"filenamePrefixForDownload,omitempty"` 37 TreatWarningsAsError bool `json:"treatWarningsAsError,omitempty"` 38 MaxRuntimeInMinutes int `json:"maxRuntimeInMinutes,omitempty"` 39 PollingIntervalInSeconds int `json:"pollingIntervalInSeconds,omitempty"` 40 CertificateNames []string `json:"certificateNames,omitempty"` 41 CpeValues string `json:"cpeValues,omitempty"` 42 UseFieldsOfAddonDescriptor string `json:"useFieldsOfAddonDescriptor,omitempty"` 43 ConditionOnAddonDescriptor string `json:"conditionOnAddonDescriptor,omitempty"` 44 StopOnFirstError bool `json:"stopOnFirstError,omitempty"` 45 AddonDescriptor string `json:"addonDescriptor,omitempty"` 46 } 47 48 type abapEnvironmentBuildCommonPipelineEnvironment struct { 49 abap struct { 50 buildValues string 51 } 52 } 53 54 func (p *abapEnvironmentBuildCommonPipelineEnvironment) persist(path, resourceName string) { 55 content := []struct { 56 category string 57 name string 58 value interface{} 59 }{ 60 {category: "abap", name: "buildValues", value: p.abap.buildValues}, 61 } 62 63 errCount := 0 64 for _, param := range content { 65 err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(param.category, param.name), param.value) 66 if err != nil { 67 log.Entry().WithError(err).Error("Error persisting piper environment.") 68 errCount++ 69 } 70 } 71 if errCount > 0 { 72 log.Entry().Error("failed to persist Piper environment") 73 } 74 } 75 76 // AbapEnvironmentBuildCommand Executes builds as defined with the build framework 77 func AbapEnvironmentBuildCommand() *cobra.Command { 78 const STEP_NAME = "abapEnvironmentBuild" 79 80 metadata := abapEnvironmentBuildMetadata() 81 var stepConfig abapEnvironmentBuildOptions 82 var startTime time.Time 83 var commonPipelineEnvironment abapEnvironmentBuildCommonPipelineEnvironment 84 var logCollector *log.CollectorHook 85 var splunkClient *splunk.Splunk 86 telemetryClient := &telemetry.Telemetry{} 87 88 var createAbapEnvironmentBuildCmd = &cobra.Command{ 89 Use: STEP_NAME, 90 Short: "Executes builds as defined with the build framework", 91 Long: `Executes builds as defined with the build framework. Transaction overview /n/BUILD/OVERVIEW`, 92 PreRunE: func(cmd *cobra.Command, _ []string) error { 93 startTime = time.Now() 94 log.SetStepName(STEP_NAME) 95 log.SetVerbose(GeneralConfig.Verbose) 96 97 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 98 99 path, _ := os.Getwd() 100 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 101 log.RegisterHook(fatalHook) 102 103 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 104 if err != nil { 105 log.SetErrorCategory(log.ErrorConfiguration) 106 return err 107 } 108 log.RegisterSecret(stepConfig.Username) 109 log.RegisterSecret(stepConfig.Password) 110 111 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 112 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 113 log.RegisterHook(&sentryHook) 114 } 115 116 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 117 splunkClient = &splunk.Splunk{} 118 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 119 log.RegisterHook(logCollector) 120 } 121 122 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 123 if err != nil { 124 return err 125 } 126 if err = validation.ValidateStruct(stepConfig); err != nil { 127 log.SetErrorCategory(log.ErrorConfiguration) 128 return err 129 } 130 131 return nil 132 }, 133 Run: func(_ *cobra.Command, _ []string) { 134 stepTelemetryData := telemetry.CustomData{} 135 stepTelemetryData.ErrorCode = "1" 136 handler := func() { 137 commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment") 138 config.RemoveVaultSecretFiles() 139 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 140 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 141 stepTelemetryData.PiperCommitHash = GitCommit 142 telemetryClient.SetData(&stepTelemetryData) 143 telemetryClient.Send() 144 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 145 splunkClient.Send(telemetryClient.GetData(), logCollector) 146 } 147 } 148 log.DeferExitHandler(handler) 149 defer handler() 150 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) 151 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 152 splunkClient.Initialize(GeneralConfig.CorrelationID, 153 GeneralConfig.HookConfig.SplunkConfig.Dsn, 154 GeneralConfig.HookConfig.SplunkConfig.Token, 155 GeneralConfig.HookConfig.SplunkConfig.Index, 156 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 157 } 158 abapEnvironmentBuild(stepConfig, &stepTelemetryData, &commonPipelineEnvironment) 159 stepTelemetryData.ErrorCode = "0" 160 log.Entry().Info("SUCCESS") 161 }, 162 } 163 164 addAbapEnvironmentBuildFlags(createAbapEnvironmentBuildCmd, &stepConfig) 165 return createAbapEnvironmentBuildCmd 166 } 167 168 func addAbapEnvironmentBuildFlags(cmd *cobra.Command, stepConfig *abapEnvironmentBuildOptions) { 169 cmd.Flags().StringVar(&stepConfig.CfAPIEndpoint, "cfApiEndpoint", os.Getenv("PIPER_cfApiEndpoint"), "Cloud Foundry API endpoint") 170 cmd.Flags().StringVar(&stepConfig.CfOrg, "cfOrg", os.Getenv("PIPER_cfOrg"), "Cloud Foundry target organization") 171 cmd.Flags().StringVar(&stepConfig.CfSpace, "cfSpace", os.Getenv("PIPER_cfSpace"), "Cloud Foundry target space") 172 cmd.Flags().StringVar(&stepConfig.CfServiceInstance, "cfServiceInstance", os.Getenv("PIPER_cfServiceInstance"), "Cloud Foundry Service Instance") 173 cmd.Flags().StringVar(&stepConfig.CfServiceKeyName, "cfServiceKeyName", os.Getenv("PIPER_cfServiceKeyName"), "Cloud Foundry Service Key") 174 cmd.Flags().StringVar(&stepConfig.Host, "host", os.Getenv("PIPER_host"), "Specifies the host address of the SAP Cloud Platform ABAP Environment system") 175 cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User") 176 cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password") 177 cmd.Flags().StringVar(&stepConfig.Phase, "phase", os.Getenv("PIPER_phase"), "Phase as specified in the build script in the backend system") 178 cmd.Flags().StringVar(&stepConfig.Values, "values", os.Getenv("PIPER_values"), "Input values for the build framework, please enter in the format '[{\"value_id\":\"Id1\",\"value\":\"value1\"},{\"value_id\":\"Id2\",\"value\":\"value2\"}]'") 179 cmd.Flags().BoolVar(&stepConfig.DownloadAllResultFiles, "downloadAllResultFiles", false, "If true, all build artefacts are downloaded") 180 cmd.Flags().StringSliceVar(&stepConfig.DownloadResultFilenames, "downloadResultFilenames", []string{}, "Only the specified files are downloaded. If downloadAllResultFiles is true, this parameter is ignored") 181 cmd.Flags().BoolVar(&stepConfig.PublishAllDownloadedResultFiles, "publishAllDownloadedResultFiles", false, "If true, it publishes all downloaded files") 182 cmd.Flags().StringSliceVar(&stepConfig.PublishResultFilenames, "publishResultFilenames", []string{}, "Only the specified files get published, in case the file was not downloaded before an error occures") 183 cmd.Flags().StringVar(&stepConfig.SubDirectoryForDownload, "subDirectoryForDownload", os.Getenv("PIPER_subDirectoryForDownload"), "Target directory to store the downloaded files, {buildID} and {taskID} can be used and will be resolved accordingly") 184 cmd.Flags().StringVar(&stepConfig.FilenamePrefixForDownload, "filenamePrefixForDownload", os.Getenv("PIPER_filenamePrefixForDownload"), "Filename prefix for the downloaded files, {buildID} and {taskID} can be used and will be resolved accordingly") 185 cmd.Flags().BoolVar(&stepConfig.TreatWarningsAsError, "treatWarningsAsError", false, "If a warrning occures, the step will be set to unstable") 186 cmd.Flags().IntVar(&stepConfig.MaxRuntimeInMinutes, "maxRuntimeInMinutes", 360, "maximal runtime of the step in minutes") 187 cmd.Flags().IntVar(&stepConfig.PollingIntervalInSeconds, "pollingIntervalInSeconds", 60, "wait time in seconds till next status request in the backend system") 188 cmd.Flags().StringSliceVar(&stepConfig.CertificateNames, "certificateNames", []string{}, "certificates for the backend system, this certificates needs to be stored in .pipeline/trustStore") 189 cmd.Flags().StringVar(&stepConfig.CpeValues, "cpeValues", os.Getenv("PIPER_cpeValues"), "Values taken from the previous step, if a value was also specified in the config file, the value from cpe will be discarded") 190 cmd.Flags().StringVar(&stepConfig.UseFieldsOfAddonDescriptor, "useFieldsOfAddonDescriptor", os.Getenv("PIPER_useFieldsOfAddonDescriptor"), "use fields of the addonDescriptor in the cpe as input values. Please enter in the format '[{\"use\":\"Name\",\"renameTo\":\"SWC\"}]'") 191 cmd.Flags().StringVar(&stepConfig.ConditionOnAddonDescriptor, "conditionOnAddonDescriptor", os.Getenv("PIPER_conditionOnAddonDescriptor"), "normally if useFieldsOfAddonDescriptor is not initial, a build is triggered for each repository in the addonDescriptor. This can be changed by posing conditions. Please enter in the format '[{\"field\":\"Status\",\"operator\":\"==\",\"value\":\"P\"}]'") 192 cmd.Flags().BoolVar(&stepConfig.StopOnFirstError, "stopOnFirstError", false, "If false, it does not stop if an error occured for one repository in the addonDescriptor, but continues with the next repository. However the step is marked as failed in the end if an error occured.") 193 cmd.Flags().StringVar(&stepConfig.AddonDescriptor, "addonDescriptor", os.Getenv("PIPER_addonDescriptor"), "Structure in the commonPipelineEnvironment containing information about the Product Version and corresponding Software Component Versions") 194 195 cmd.MarkFlagRequired("username") 196 cmd.MarkFlagRequired("password") 197 cmd.MarkFlagRequired("phase") 198 cmd.MarkFlagRequired("downloadAllResultFiles") 199 cmd.MarkFlagRequired("publishAllDownloadedResultFiles") 200 cmd.MarkFlagRequired("treatWarningsAsError") 201 cmd.MarkFlagRequired("maxRuntimeInMinutes") 202 cmd.MarkFlagRequired("pollingIntervalInSeconds") 203 } 204 205 // retrieve step metadata 206 func abapEnvironmentBuildMetadata() config.StepData { 207 var theMetaData = config.StepData{ 208 Metadata: config.StepMetadata{ 209 Name: "abapEnvironmentBuild", 210 Aliases: []config.Alias{}, 211 Description: "Executes builds as defined with the build framework", 212 }, 213 Spec: config.StepSpec{ 214 Inputs: config.StepInputs{ 215 Secrets: []config.StepSecrets{ 216 {Name: "abapCredentialsId", Description: "Jenkins credentials ID containing user and password to authenticate to the Cloud Platform ABAP Environment system or the Cloud Foundry API", Type: "jenkins", Aliases: []config.Alias{{Name: "cfCredentialsId", Deprecated: false}, {Name: "credentialsId", Deprecated: false}}}, 217 }, 218 Parameters: []config.StepParameters{ 219 { 220 Name: "cfApiEndpoint", 221 ResourceRef: []config.ResourceReference{}, 222 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 223 Type: "string", 224 Mandatory: false, 225 Aliases: []config.Alias{{Name: "cloudFoundry/apiEndpoint"}}, 226 Default: os.Getenv("PIPER_cfApiEndpoint"), 227 }, 228 { 229 Name: "cfOrg", 230 ResourceRef: []config.ResourceReference{}, 231 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 232 Type: "string", 233 Mandatory: false, 234 Aliases: []config.Alias{{Name: "cloudFoundry/org"}}, 235 Default: os.Getenv("PIPER_cfOrg"), 236 }, 237 { 238 Name: "cfSpace", 239 ResourceRef: []config.ResourceReference{}, 240 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 241 Type: "string", 242 Mandatory: false, 243 Aliases: []config.Alias{{Name: "cloudFoundry/space"}}, 244 Default: os.Getenv("PIPER_cfSpace"), 245 }, 246 { 247 Name: "cfServiceInstance", 248 ResourceRef: []config.ResourceReference{}, 249 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 250 Type: "string", 251 Mandatory: false, 252 Aliases: []config.Alias{{Name: "cloudFoundry/serviceInstance"}}, 253 Default: os.Getenv("PIPER_cfServiceInstance"), 254 }, 255 { 256 Name: "cfServiceKeyName", 257 ResourceRef: []config.ResourceReference{}, 258 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 259 Type: "string", 260 Mandatory: false, 261 Aliases: []config.Alias{{Name: "cloudFoundry/serviceKey"}, {Name: "cloudFoundry/serviceKeyName"}, {Name: "cfServiceKey"}}, 262 Default: os.Getenv("PIPER_cfServiceKeyName"), 263 }, 264 { 265 Name: "host", 266 ResourceRef: []config.ResourceReference{}, 267 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 268 Type: "string", 269 Mandatory: false, 270 Aliases: []config.Alias{}, 271 Default: os.Getenv("PIPER_host"), 272 }, 273 { 274 Name: "username", 275 ResourceRef: []config.ResourceReference{}, 276 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 277 Type: "string", 278 Mandatory: true, 279 Aliases: []config.Alias{}, 280 Default: os.Getenv("PIPER_username"), 281 }, 282 { 283 Name: "password", 284 ResourceRef: []config.ResourceReference{}, 285 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 286 Type: "string", 287 Mandatory: true, 288 Aliases: []config.Alias{}, 289 Default: os.Getenv("PIPER_password"), 290 }, 291 { 292 Name: "phase", 293 ResourceRef: []config.ResourceReference{}, 294 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 295 Type: "string", 296 Mandatory: true, 297 Aliases: []config.Alias{}, 298 Default: os.Getenv("PIPER_phase"), 299 }, 300 { 301 Name: "values", 302 ResourceRef: []config.ResourceReference{}, 303 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 304 Type: "string", 305 Mandatory: false, 306 Aliases: []config.Alias{}, 307 Default: os.Getenv("PIPER_values"), 308 }, 309 { 310 Name: "downloadAllResultFiles", 311 ResourceRef: []config.ResourceReference{}, 312 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 313 Type: "bool", 314 Mandatory: true, 315 Aliases: []config.Alias{}, 316 Default: false, 317 }, 318 { 319 Name: "downloadResultFilenames", 320 ResourceRef: []config.ResourceReference{}, 321 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 322 Type: "[]string", 323 Mandatory: false, 324 Aliases: []config.Alias{}, 325 Default: []string{}, 326 }, 327 { 328 Name: "publishAllDownloadedResultFiles", 329 ResourceRef: []config.ResourceReference{}, 330 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 331 Type: "bool", 332 Mandatory: true, 333 Aliases: []config.Alias{}, 334 Default: false, 335 }, 336 { 337 Name: "publishResultFilenames", 338 ResourceRef: []config.ResourceReference{}, 339 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 340 Type: "[]string", 341 Mandatory: false, 342 Aliases: []config.Alias{}, 343 Default: []string{}, 344 }, 345 { 346 Name: "subDirectoryForDownload", 347 ResourceRef: []config.ResourceReference{}, 348 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 349 Type: "string", 350 Mandatory: false, 351 Aliases: []config.Alias{}, 352 Default: os.Getenv("PIPER_subDirectoryForDownload"), 353 }, 354 { 355 Name: "filenamePrefixForDownload", 356 ResourceRef: []config.ResourceReference{}, 357 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 358 Type: "string", 359 Mandatory: false, 360 Aliases: []config.Alias{}, 361 Default: os.Getenv("PIPER_filenamePrefixForDownload"), 362 }, 363 { 364 Name: "treatWarningsAsError", 365 ResourceRef: []config.ResourceReference{}, 366 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 367 Type: "bool", 368 Mandatory: true, 369 Aliases: []config.Alias{}, 370 Default: false, 371 }, 372 { 373 Name: "maxRuntimeInMinutes", 374 ResourceRef: []config.ResourceReference{}, 375 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 376 Type: "int", 377 Mandatory: true, 378 Aliases: []config.Alias{}, 379 Default: 360, 380 }, 381 { 382 Name: "pollingIntervalInSeconds", 383 ResourceRef: []config.ResourceReference{}, 384 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 385 Type: "int", 386 Mandatory: true, 387 Aliases: []config.Alias{}, 388 Default: 60, 389 }, 390 { 391 Name: "certificateNames", 392 ResourceRef: []config.ResourceReference{}, 393 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 394 Type: "[]string", 395 Mandatory: false, 396 Aliases: []config.Alias{}, 397 Default: []string{}, 398 }, 399 { 400 Name: "cpeValues", 401 ResourceRef: []config.ResourceReference{ 402 { 403 Name: "commonPipelineEnvironment", 404 Param: "abap/buildValues", 405 }, 406 }, 407 Scope: []string{}, 408 Type: "string", 409 Mandatory: false, 410 Aliases: []config.Alias{}, 411 Default: os.Getenv("PIPER_cpeValues"), 412 }, 413 { 414 Name: "useFieldsOfAddonDescriptor", 415 ResourceRef: []config.ResourceReference{}, 416 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 417 Type: "string", 418 Mandatory: false, 419 Aliases: []config.Alias{}, 420 Default: os.Getenv("PIPER_useFieldsOfAddonDescriptor"), 421 }, 422 { 423 Name: "conditionOnAddonDescriptor", 424 ResourceRef: []config.ResourceReference{}, 425 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 426 Type: "string", 427 Mandatory: false, 428 Aliases: []config.Alias{}, 429 Default: os.Getenv("PIPER_conditionOnAddonDescriptor"), 430 }, 431 { 432 Name: "stopOnFirstError", 433 ResourceRef: []config.ResourceReference{}, 434 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 435 Type: "bool", 436 Mandatory: false, 437 Aliases: []config.Alias{}, 438 Default: false, 439 }, 440 { 441 Name: "addonDescriptor", 442 ResourceRef: []config.ResourceReference{ 443 { 444 Name: "commonPipelineEnvironment", 445 Param: "abap/addonDescriptor", 446 }, 447 }, 448 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 449 Type: "string", 450 Mandatory: false, 451 Aliases: []config.Alias{}, 452 Default: os.Getenv("PIPER_addonDescriptor"), 453 }, 454 }, 455 }, 456 Containers: []config.Container{ 457 {Name: "cf", Image: "ppiper/cf-cli:7"}, 458 }, 459 Outputs: config.StepOutputs{ 460 Resources: []config.StepResources{ 461 { 462 Name: "commonPipelineEnvironment", 463 Type: "piperEnvironment", 464 Parameters: []map[string]interface{}{ 465 {"name": "abap/buildValues"}, 466 }, 467 }, 468 }, 469 }, 470 }, 471 } 472 return theMetaData 473 }