github.com/xgoffin/jenkins-library@v1.154.0/cmd/mtaBuild_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 "reflect" 10 "strings" 11 "time" 12 13 "github.com/SAP/jenkins-library/pkg/config" 14 "github.com/SAP/jenkins-library/pkg/gcs" 15 "github.com/SAP/jenkins-library/pkg/log" 16 "github.com/SAP/jenkins-library/pkg/piperenv" 17 "github.com/SAP/jenkins-library/pkg/splunk" 18 "github.com/SAP/jenkins-library/pkg/telemetry" 19 "github.com/SAP/jenkins-library/pkg/validation" 20 "github.com/bmatcuk/doublestar" 21 "github.com/spf13/cobra" 22 ) 23 24 type mtaBuildOptions struct { 25 MtarName string `json:"mtarName,omitempty"` 26 MtarGroup string `json:"mtarGroup,omitempty"` 27 Version string `json:"version,omitempty"` 28 Extensions string `json:"extensions,omitempty"` 29 Jobs int `json:"jobs,omitempty"` 30 Platform string `json:"platform,omitempty" validate:"possible-values=CF NEO XSA"` 31 ApplicationName string `json:"applicationName,omitempty"` 32 Source string `json:"source,omitempty"` 33 Target string `json:"target,omitempty"` 34 DefaultNpmRegistry string `json:"defaultNpmRegistry,omitempty"` 35 ProjectSettingsFile string `json:"projectSettingsFile,omitempty"` 36 GlobalSettingsFile string `json:"globalSettingsFile,omitempty"` 37 M2Path string `json:"m2Path,omitempty"` 38 InstallArtifacts bool `json:"installArtifacts,omitempty"` 39 MtaDeploymentRepositoryPassword string `json:"mtaDeploymentRepositoryPassword,omitempty"` 40 MtaDeploymentRepositoryUser string `json:"mtaDeploymentRepositoryUser,omitempty"` 41 MtaDeploymentRepositoryURL string `json:"mtaDeploymentRepositoryUrl,omitempty"` 42 Publish bool `json:"publish,omitempty"` 43 Profiles []string `json:"profiles,omitempty"` 44 BuildSettingsInfo string `json:"buildSettingsInfo,omitempty"` 45 } 46 47 type mtaBuildCommonPipelineEnvironment struct { 48 mtarFilePath string 49 custom struct { 50 mtaBuildToolDesc string 51 mtarPublishedURL string 52 buildSettingsInfo string 53 } 54 } 55 56 func (p *mtaBuildCommonPipelineEnvironment) persist(path, resourceName string) { 57 content := []struct { 58 category string 59 name string 60 value interface{} 61 }{ 62 {category: "", name: "mtarFilePath", value: p.mtarFilePath}, 63 {category: "custom", name: "mtaBuildToolDesc", value: p.custom.mtaBuildToolDesc}, 64 {category: "custom", name: "mtarPublishedUrl", value: p.custom.mtarPublishedURL}, 65 {category: "custom", name: "buildSettingsInfo", value: p.custom.buildSettingsInfo}, 66 } 67 68 errCount := 0 69 for _, param := range content { 70 err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(param.category, param.name), param.value) 71 if err != nil { 72 log.Entry().WithError(err).Error("Error persisting piper environment.") 73 errCount++ 74 } 75 } 76 if errCount > 0 { 77 log.Entry().Error("failed to persist Piper environment") 78 } 79 } 80 81 type mtaBuildReports struct { 82 } 83 84 func (p *mtaBuildReports) persist(stepConfig mtaBuildOptions, gcpJsonKeyFilePath string, gcsBucketId string, gcsFolderPath string, gcsSubFolder string) { 85 if gcsBucketId == "" { 86 log.Entry().Info("persisting reports to GCS is disabled, because gcsBucketId is empty") 87 return 88 } 89 log.Entry().Info("Uploading reports to Google Cloud Storage...") 90 content := []gcs.ReportOutputParam{ 91 {FilePattern: "**/TEST-*.xml", ParamRef: "", StepResultType: "junit"}, 92 {FilePattern: "**/cobertura-coverage.xml", ParamRef: "", StepResultType: "cobertura-coverage"}, 93 {FilePattern: "**/jacoco.xml", ParamRef: "", StepResultType: "jacoco-coverage"}, 94 } 95 envVars := []gcs.EnvVar{ 96 {Name: "GOOGLE_APPLICATION_CREDENTIALS", Value: gcpJsonKeyFilePath, Modified: false}, 97 } 98 gcsClient, err := gcs.NewClient(gcs.WithEnvVars(envVars)) 99 if err != nil { 100 log.Entry().Errorf("creation of GCS client failed: %v", err) 101 return 102 } 103 defer gcsClient.Close() 104 structVal := reflect.ValueOf(&stepConfig).Elem() 105 inputParameters := map[string]string{} 106 for i := 0; i < structVal.NumField(); i++ { 107 field := structVal.Type().Field(i) 108 if field.Type.String() == "string" { 109 paramName := strings.Split(field.Tag.Get("json"), ",") 110 paramValue, _ := structVal.Field(i).Interface().(string) 111 inputParameters[paramName[0]] = paramValue 112 } 113 } 114 if err := gcs.PersistReportsToGCS(gcsClient, content, inputParameters, gcsFolderPath, gcsBucketId, gcsSubFolder, doublestar.Glob, os.Stat); err != nil { 115 log.Entry().Errorf("failed to persist reports: %v", err) 116 } 117 } 118 119 // MtaBuildCommand Performs an mta build 120 func MtaBuildCommand() *cobra.Command { 121 const STEP_NAME = "mtaBuild" 122 123 metadata := mtaBuildMetadata() 124 var stepConfig mtaBuildOptions 125 var startTime time.Time 126 var commonPipelineEnvironment mtaBuildCommonPipelineEnvironment 127 var reports mtaBuildReports 128 var logCollector *log.CollectorHook 129 var splunkClient *splunk.Splunk 130 telemetryClient := &telemetry.Telemetry{} 131 132 var createMtaBuildCmd = &cobra.Command{ 133 Use: STEP_NAME, 134 Short: "Performs an mta build", 135 Long: `Executes the SAP Multitarget Application Archive Builder to create an mtar archive of the application. 136 ### build with depedencies from a private repository 137 1. For maven related settings refer [maven build dependencies](./mavenBuild.md#build-with-depedencies-from-a-private-repository) 138 2. For NPM related settings refer [NPM build dependencies](./npmExecuteScripts.md#build-with-depedencies-from-a-private-repository)`, 139 PreRunE: func(cmd *cobra.Command, _ []string) error { 140 startTime = time.Now() 141 log.SetStepName(STEP_NAME) 142 log.SetVerbose(GeneralConfig.Verbose) 143 144 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 145 146 path, _ := os.Getwd() 147 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 148 log.RegisterHook(fatalHook) 149 150 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 151 if err != nil { 152 log.SetErrorCategory(log.ErrorConfiguration) 153 return err 154 } 155 log.RegisterSecret(stepConfig.MtaDeploymentRepositoryPassword) 156 157 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 158 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 159 log.RegisterHook(&sentryHook) 160 } 161 162 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 163 splunkClient = &splunk.Splunk{} 164 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 165 log.RegisterHook(logCollector) 166 } 167 168 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 169 if err != nil { 170 return err 171 } 172 if err = validation.ValidateStruct(stepConfig); err != nil { 173 log.SetErrorCategory(log.ErrorConfiguration) 174 return err 175 } 176 177 return nil 178 }, 179 Run: func(_ *cobra.Command, _ []string) { 180 stepTelemetryData := telemetry.CustomData{} 181 stepTelemetryData.ErrorCode = "1" 182 handler := func() { 183 commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment") 184 reports.persist(stepConfig, GeneralConfig.GCPJsonKeyFilePath, GeneralConfig.GCSBucketId, GeneralConfig.GCSFolderPath, GeneralConfig.GCSSubFolder) 185 config.RemoveVaultSecretFiles() 186 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 187 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 188 stepTelemetryData.PiperCommitHash = GitCommit 189 telemetryClient.SetData(&stepTelemetryData) 190 telemetryClient.Send() 191 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 192 splunkClient.Send(telemetryClient.GetData(), logCollector) 193 } 194 } 195 log.DeferExitHandler(handler) 196 defer handler() 197 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) 198 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 199 splunkClient.Initialize(GeneralConfig.CorrelationID, 200 GeneralConfig.HookConfig.SplunkConfig.Dsn, 201 GeneralConfig.HookConfig.SplunkConfig.Token, 202 GeneralConfig.HookConfig.SplunkConfig.Index, 203 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 204 } 205 mtaBuild(stepConfig, &stepTelemetryData, &commonPipelineEnvironment) 206 stepTelemetryData.ErrorCode = "0" 207 log.Entry().Info("SUCCESS") 208 }, 209 } 210 211 addMtaBuildFlags(createMtaBuildCmd, &stepConfig) 212 return createMtaBuildCmd 213 } 214 215 func addMtaBuildFlags(cmd *cobra.Command, stepConfig *mtaBuildOptions) { 216 cmd.Flags().StringVar(&stepConfig.MtarName, "mtarName", os.Getenv("PIPER_mtarName"), "The name of the generated mtar file including its extension.") 217 cmd.Flags().StringVar(&stepConfig.MtarGroup, "mtarGroup", os.Getenv("PIPER_mtarGroup"), "The group to which the mtar artifact will be uploaded. Required when publish is True.") 218 cmd.Flags().StringVar(&stepConfig.Version, "version", os.Getenv("PIPER_version"), "Version of the mtar artifact") 219 cmd.Flags().StringVar(&stepConfig.Extensions, "extensions", os.Getenv("PIPER_extensions"), "The path to the extension descriptor file.") 220 cmd.Flags().IntVar(&stepConfig.Jobs, "jobs", 0, "Configures the number of Make jobs that can run simultaneously. Maximum value allowed is 8") 221 cmd.Flags().StringVar(&stepConfig.Platform, "platform", `CF`, "The target platform to which the mtar can be deployed.") 222 cmd.Flags().StringVar(&stepConfig.ApplicationName, "applicationName", os.Getenv("PIPER_applicationName"), "The name of the application which is being built. If the parameter has been provided and no `mta.yaml` exists, the `mta.yaml` will be automatically generated using this parameter and the information (`name` and `version`) from 'package.json` before the actual build starts.") 223 cmd.Flags().StringVar(&stepConfig.Source, "source", `./`, "The path to the MTA project.") 224 cmd.Flags().StringVar(&stepConfig.Target, "target", `./`, "The folder for the generated `MTAR` file. If the parameter has been provided, the `MTAR` file is saved in the root of the folder provided by the argument.") 225 cmd.Flags().StringVar(&stepConfig.DefaultNpmRegistry, "defaultNpmRegistry", os.Getenv("PIPER_defaultNpmRegistry"), "Url to the npm registry that should be used for installing npm dependencies.") 226 cmd.Flags().StringVar(&stepConfig.ProjectSettingsFile, "projectSettingsFile", os.Getenv("PIPER_projectSettingsFile"), "Path or url to the mvn settings file that should be used as project settings file.") 227 cmd.Flags().StringVar(&stepConfig.GlobalSettingsFile, "globalSettingsFile", os.Getenv("PIPER_globalSettingsFile"), "Path or url to the mvn settings file that should be used as global settings file") 228 cmd.Flags().StringVar(&stepConfig.M2Path, "m2Path", os.Getenv("PIPER_m2Path"), "Path to the location of the local repository that should be used.") 229 cmd.Flags().BoolVar(&stepConfig.InstallArtifacts, "installArtifacts", false, "If enabled, for npm packages this step will install all dependencies including dev dependencies. For maven it will install all artifacts to the local maven repository. Note: This happens _after_ mta build was done. The default mta build tool does not install dev-dependencies as part of the process. If you require dev-dependencies for building the mta, you will need to use a [custom builder](https://sap.github.io/cloud-mta-build-tool/configuration/#configuring-the-custom-builder)") 230 cmd.Flags().StringVar(&stepConfig.MtaDeploymentRepositoryPassword, "mtaDeploymentRepositoryPassword", os.Getenv("PIPER_mtaDeploymentRepositoryPassword"), "Password for the alternative deployment repository to which mtar artifacts will be publised") 231 cmd.Flags().StringVar(&stepConfig.MtaDeploymentRepositoryUser, "mtaDeploymentRepositoryUser", os.Getenv("PIPER_mtaDeploymentRepositoryUser"), "User for the alternative deployment repository to which which mtar artifacts will be publised") 232 cmd.Flags().StringVar(&stepConfig.MtaDeploymentRepositoryURL, "mtaDeploymentRepositoryUrl", os.Getenv("PIPER_mtaDeploymentRepositoryUrl"), "Url for the alternative deployment repository to which mtar artifacts will be publised") 233 cmd.Flags().BoolVar(&stepConfig.Publish, "publish", false, "pushed mtar artifact to altDeploymentRepositoryUrl/altDeploymentRepositoryID when set to true") 234 cmd.Flags().StringSliceVar(&stepConfig.Profiles, "profiles", []string{}, "Defines list of maven build profiles to be used. profiles will overwrite existing values in the global settings xml at $M2_HOME/conf/settings.xml") 235 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 mta build . This information is typically used for compliance related processes.") 236 237 } 238 239 // retrieve step metadata 240 func mtaBuildMetadata() config.StepData { 241 var theMetaData = config.StepData{ 242 Metadata: config.StepMetadata{ 243 Name: "mtaBuild", 244 Aliases: []config.Alias{}, 245 Description: "Performs an mta build", 246 }, 247 Spec: config.StepSpec{ 248 Inputs: config.StepInputs{ 249 Parameters: []config.StepParameters{ 250 { 251 Name: "mtarName", 252 ResourceRef: []config.ResourceReference{}, 253 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 254 Type: "string", 255 Mandatory: false, 256 Aliases: []config.Alias{}, 257 Default: os.Getenv("PIPER_mtarName"), 258 }, 259 { 260 Name: "mtarGroup", 261 ResourceRef: []config.ResourceReference{}, 262 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 263 Type: "string", 264 Mandatory: false, 265 Aliases: []config.Alias{}, 266 Default: os.Getenv("PIPER_mtarGroup"), 267 }, 268 { 269 Name: "version", 270 ResourceRef: []config.ResourceReference{ 271 { 272 Name: "commonPipelineEnvironment", 273 Param: "artifactVersion", 274 }, 275 }, 276 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 277 Type: "string", 278 Mandatory: false, 279 Aliases: []config.Alias{{Name: "artifactVersion"}}, 280 Default: os.Getenv("PIPER_version"), 281 }, 282 { 283 Name: "extensions", 284 ResourceRef: []config.ResourceReference{}, 285 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 286 Type: "string", 287 Mandatory: false, 288 Aliases: []config.Alias{{Name: "extension"}}, 289 Default: os.Getenv("PIPER_extensions"), 290 }, 291 { 292 Name: "jobs", 293 ResourceRef: []config.ResourceReference{}, 294 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 295 Type: "int", 296 Mandatory: false, 297 Aliases: []config.Alias{{Name: "jobs"}}, 298 Default: 0, 299 }, 300 { 301 Name: "platform", 302 ResourceRef: []config.ResourceReference{}, 303 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 304 Type: "string", 305 Mandatory: false, 306 Aliases: []config.Alias{}, 307 Default: `CF`, 308 }, 309 { 310 Name: "applicationName", 311 ResourceRef: []config.ResourceReference{}, 312 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 313 Type: "string", 314 Mandatory: false, 315 Aliases: []config.Alias{}, 316 Default: os.Getenv("PIPER_applicationName"), 317 }, 318 { 319 Name: "source", 320 ResourceRef: []config.ResourceReference{}, 321 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 322 Type: "string", 323 Mandatory: false, 324 Aliases: []config.Alias{}, 325 Default: `./`, 326 }, 327 { 328 Name: "target", 329 ResourceRef: []config.ResourceReference{}, 330 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 331 Type: "string", 332 Mandatory: false, 333 Aliases: []config.Alias{}, 334 Default: `./`, 335 }, 336 { 337 Name: "defaultNpmRegistry", 338 ResourceRef: []config.ResourceReference{}, 339 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 340 Type: "string", 341 Mandatory: false, 342 Aliases: []config.Alias{{Name: "npm/defaultNpmRegistry"}}, 343 Default: os.Getenv("PIPER_defaultNpmRegistry"), 344 }, 345 { 346 Name: "projectSettingsFile", 347 ResourceRef: []config.ResourceReference{}, 348 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 349 Type: "string", 350 Mandatory: false, 351 Aliases: []config.Alias{{Name: "maven/projectSettingsFile"}}, 352 Default: os.Getenv("PIPER_projectSettingsFile"), 353 }, 354 { 355 Name: "globalSettingsFile", 356 ResourceRef: []config.ResourceReference{}, 357 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 358 Type: "string", 359 Mandatory: false, 360 Aliases: []config.Alias{{Name: "maven/globalSettingsFile"}}, 361 Default: os.Getenv("PIPER_globalSettingsFile"), 362 }, 363 { 364 Name: "m2Path", 365 ResourceRef: []config.ResourceReference{}, 366 Scope: []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS"}, 367 Type: "string", 368 Mandatory: false, 369 Aliases: []config.Alias{{Name: "maven/m2Path"}}, 370 Default: os.Getenv("PIPER_m2Path"), 371 }, 372 { 373 Name: "installArtifacts", 374 ResourceRef: []config.ResourceReference{}, 375 Scope: []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS"}, 376 Type: "bool", 377 Mandatory: false, 378 Aliases: []config.Alias{}, 379 Default: false, 380 }, 381 { 382 Name: "mtaDeploymentRepositoryPassword", 383 ResourceRef: []config.ResourceReference{ 384 { 385 Name: "commonPipelineEnvironment", 386 Param: "custom/mavenRepositoryPassword", 387 }, 388 389 { 390 Name: "commonPipelineEnvironment", 391 Param: "custom/repositoryPassword", 392 }, 393 394 { 395 Name: "mtaDeploymentRepositoryPasswordId", 396 Type: "secret", 397 }, 398 399 { 400 Name: "mtaDeploymentRepositoryPasswordFileVaultSecretName", 401 Type: "vaultSecretFile", 402 Default: "mta-deployment-repository-passowrd", 403 }, 404 }, 405 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 406 Type: "string", 407 Mandatory: false, 408 Aliases: []config.Alias{}, 409 Default: os.Getenv("PIPER_mtaDeploymentRepositoryPassword"), 410 }, 411 { 412 Name: "mtaDeploymentRepositoryUser", 413 ResourceRef: []config.ResourceReference{ 414 { 415 Name: "commonPipelineEnvironment", 416 Param: "custom/mavenRepositoryUsername", 417 }, 418 419 { 420 Name: "commonPipelineEnvironment", 421 Param: "custom/repositoryUsername", 422 }, 423 }, 424 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 425 Type: "string", 426 Mandatory: false, 427 Aliases: []config.Alias{}, 428 Default: os.Getenv("PIPER_mtaDeploymentRepositoryUser"), 429 }, 430 { 431 Name: "mtaDeploymentRepositoryUrl", 432 ResourceRef: []config.ResourceReference{ 433 { 434 Name: "commonPipelineEnvironment", 435 Param: "custom/mavenRepositoryURL", 436 }, 437 438 { 439 Name: "commonPipelineEnvironment", 440 Param: "custom/repositoryUrl", 441 }, 442 }, 443 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 444 Type: "string", 445 Mandatory: false, 446 Aliases: []config.Alias{}, 447 Default: os.Getenv("PIPER_mtaDeploymentRepositoryUrl"), 448 }, 449 { 450 Name: "publish", 451 ResourceRef: []config.ResourceReference{}, 452 Scope: []string{"STEPS", "STAGES", "PARAMETERS"}, 453 Type: "bool", 454 Mandatory: false, 455 Aliases: []config.Alias{{Name: "mta/publish"}}, 456 Default: false, 457 }, 458 { 459 Name: "profiles", 460 ResourceRef: []config.ResourceReference{}, 461 Scope: []string{"PARAMETERS", "GENERAL", "STAGES", "STEPS"}, 462 Type: "[]string", 463 Mandatory: false, 464 Aliases: []config.Alias{}, 465 Default: []string{}, 466 }, 467 { 468 Name: "buildSettingsInfo", 469 ResourceRef: []config.ResourceReference{ 470 { 471 Name: "commonPipelineEnvironment", 472 Param: "custom/buildSettingsInfo", 473 }, 474 }, 475 Scope: []string{"STEPS", "STAGES", "PARAMETERS"}, 476 Type: "string", 477 Mandatory: false, 478 Aliases: []config.Alias{}, 479 Default: os.Getenv("PIPER_buildSettingsInfo"), 480 }, 481 }, 482 }, 483 Containers: []config.Container{ 484 {Image: "devxci/mbtci-java11-node14"}, 485 }, 486 Outputs: config.StepOutputs{ 487 Resources: []config.StepResources{ 488 { 489 Name: "commonPipelineEnvironment", 490 Type: "piperEnvironment", 491 Parameters: []map[string]interface{}{ 492 {"name": "mtarFilePath"}, 493 {"name": "custom/mtaBuildToolDesc"}, 494 {"name": "custom/mtarPublishedUrl"}, 495 {"name": "custom/buildSettingsInfo"}, 496 }, 497 }, 498 { 499 Name: "reports", 500 Type: "reports", 501 Parameters: []map[string]interface{}{ 502 {"filePattern": "**/TEST-*.xml", "type": "junit"}, 503 {"filePattern": "**/cobertura-coverage.xml", "type": "cobertura-coverage"}, 504 {"filePattern": "**/jacoco.xml", "type": "jacoco-coverage"}, 505 }, 506 }, 507 }, 508 }, 509 }, 510 } 511 return theMetaData 512 }