github.com/jaylevin/jenkins-library@v1.230.4/cmd/pythonBuild_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 pythonBuildOptions struct { 21 BuildFlags []string `json:"buildFlags,omitempty"` 22 CreateBOM bool `json:"createBOM,omitempty"` 23 Publish bool `json:"publish,omitempty"` 24 TargetRepositoryPassword string `json:"targetRepositoryPassword,omitempty"` 25 TargetRepositoryUser string `json:"targetRepositoryUser,omitempty"` 26 TargetRepositoryURL string `json:"targetRepositoryURL,omitempty"` 27 BuildSettingsInfo string `json:"buildSettingsInfo,omitempty"` 28 VirutalEnvironmentName string `json:"virutalEnvironmentName,omitempty"` 29 } 30 31 type pythonBuildCommonPipelineEnvironment struct { 32 custom struct { 33 buildSettingsInfo string 34 } 35 } 36 37 func (p *pythonBuildCommonPipelineEnvironment) persist(path, resourceName string) { 38 content := []struct { 39 category string 40 name string 41 value interface{} 42 }{ 43 {category: "custom", name: "buildSettingsInfo", value: p.custom.buildSettingsInfo}, 44 } 45 46 errCount := 0 47 for _, param := range content { 48 err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(param.category, param.name), param.value) 49 if err != nil { 50 log.Entry().WithError(err).Error("Error persisting piper environment.") 51 errCount++ 52 } 53 } 54 if errCount > 0 { 55 log.Entry().Error("failed to persist Piper environment") 56 } 57 } 58 59 // PythonBuildCommand Step build a python project 60 func PythonBuildCommand() *cobra.Command { 61 const STEP_NAME = "pythonBuild" 62 63 metadata := pythonBuildMetadata() 64 var stepConfig pythonBuildOptions 65 var startTime time.Time 66 var commonPipelineEnvironment pythonBuildCommonPipelineEnvironment 67 var logCollector *log.CollectorHook 68 var splunkClient *splunk.Splunk 69 telemetryClient := &telemetry.Telemetry{} 70 71 var createPythonBuildCmd = &cobra.Command{ 72 Use: STEP_NAME, 73 Short: "Step build a python project", 74 Long: `Step build python project with using test Vault credentials`, 75 PreRunE: func(cmd *cobra.Command, _ []string) error { 76 startTime = time.Now() 77 log.SetStepName(STEP_NAME) 78 log.SetVerbose(GeneralConfig.Verbose) 79 80 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 81 82 path, _ := os.Getwd() 83 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 84 log.RegisterHook(fatalHook) 85 86 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 87 if err != nil { 88 log.SetErrorCategory(log.ErrorConfiguration) 89 return err 90 } 91 log.RegisterSecret(stepConfig.TargetRepositoryPassword) 92 log.RegisterSecret(stepConfig.TargetRepositoryUser) 93 94 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 95 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 96 log.RegisterHook(&sentryHook) 97 } 98 99 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 100 splunkClient = &splunk.Splunk{} 101 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 102 log.RegisterHook(logCollector) 103 } 104 105 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 106 if err != nil { 107 return err 108 } 109 if err = validation.ValidateStruct(stepConfig); err != nil { 110 log.SetErrorCategory(log.ErrorConfiguration) 111 return err 112 } 113 114 return nil 115 }, 116 Run: func(_ *cobra.Command, _ []string) { 117 stepTelemetryData := telemetry.CustomData{} 118 stepTelemetryData.ErrorCode = "1" 119 handler := func() { 120 commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment") 121 config.RemoveVaultSecretFiles() 122 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 123 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 124 stepTelemetryData.PiperCommitHash = GitCommit 125 telemetryClient.SetData(&stepTelemetryData) 126 telemetryClient.Send() 127 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 128 splunkClient.Send(telemetryClient.GetData(), logCollector) 129 } 130 } 131 log.DeferExitHandler(handler) 132 defer handler() 133 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) 134 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 135 splunkClient.Initialize(GeneralConfig.CorrelationID, 136 GeneralConfig.HookConfig.SplunkConfig.Dsn, 137 GeneralConfig.HookConfig.SplunkConfig.Token, 138 GeneralConfig.HookConfig.SplunkConfig.Index, 139 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 140 } 141 pythonBuild(stepConfig, &stepTelemetryData, &commonPipelineEnvironment) 142 stepTelemetryData.ErrorCode = "0" 143 log.Entry().Info("SUCCESS") 144 }, 145 } 146 147 addPythonBuildFlags(createPythonBuildCmd, &stepConfig) 148 return createPythonBuildCmd 149 } 150 151 func addPythonBuildFlags(cmd *cobra.Command, stepConfig *pythonBuildOptions) { 152 cmd.Flags().StringSliceVar(&stepConfig.BuildFlags, "buildFlags", []string{}, "Defines list of build flags to be used.") 153 cmd.Flags().BoolVar(&stepConfig.CreateBOM, "createBOM", false, "Creates the bill of materials (BOM) using CycloneDX plugin.") 154 cmd.Flags().BoolVar(&stepConfig.Publish, "publish", false, "Configures the build to publish artifacts to a repository.") 155 cmd.Flags().StringVar(&stepConfig.TargetRepositoryPassword, "targetRepositoryPassword", os.Getenv("PIPER_targetRepositoryPassword"), "Password for the target repository where the compiled binaries shall be uploaded - typically provided by the CI/CD environment.") 156 cmd.Flags().StringVar(&stepConfig.TargetRepositoryUser, "targetRepositoryUser", os.Getenv("PIPER_targetRepositoryUser"), "Username for the target repository where the compiled binaries shall be uploaded - typically provided by the CI/CD environment.") 157 cmd.Flags().StringVar(&stepConfig.TargetRepositoryURL, "targetRepositoryURL", os.Getenv("PIPER_targetRepositoryURL"), "URL of the target repository where the compiled binaries shall be uploaded - typically provided by the CI/CD environment.") 158 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 maven build . This information is typically used for compliance related processes.") 159 cmd.Flags().StringVar(&stepConfig.VirutalEnvironmentName, "virutalEnvironmentName", `piperBuild-env`, "name of the virtual environment that will be used for the build") 160 161 } 162 163 // retrieve step metadata 164 func pythonBuildMetadata() config.StepData { 165 var theMetaData = config.StepData{ 166 Metadata: config.StepMetadata{ 167 Name: "pythonBuild", 168 Aliases: []config.Alias{}, 169 Description: "Step build a python project", 170 }, 171 Spec: config.StepSpec{ 172 Inputs: config.StepInputs{ 173 Parameters: []config.StepParameters{ 174 { 175 Name: "buildFlags", 176 ResourceRef: []config.ResourceReference{}, 177 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 178 Type: "[]string", 179 Mandatory: false, 180 Aliases: []config.Alias{}, 181 Default: []string{}, 182 }, 183 { 184 Name: "createBOM", 185 ResourceRef: []config.ResourceReference{}, 186 Scope: []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS"}, 187 Type: "bool", 188 Mandatory: false, 189 Aliases: []config.Alias{}, 190 Default: false, 191 }, 192 { 193 Name: "publish", 194 ResourceRef: []config.ResourceReference{}, 195 Scope: []string{"STEPS", "STAGES", "PARAMETERS"}, 196 Type: "bool", 197 Mandatory: false, 198 Aliases: []config.Alias{}, 199 Default: false, 200 }, 201 { 202 Name: "targetRepositoryPassword", 203 ResourceRef: []config.ResourceReference{ 204 { 205 Name: "commonPipelineEnvironment", 206 Param: "custom/repositoryPassword", 207 }, 208 }, 209 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 210 Type: "string", 211 Mandatory: false, 212 Aliases: []config.Alias{}, 213 Default: os.Getenv("PIPER_targetRepositoryPassword"), 214 }, 215 { 216 Name: "targetRepositoryUser", 217 ResourceRef: []config.ResourceReference{ 218 { 219 Name: "commonPipelineEnvironment", 220 Param: "custom/repositoryUsername", 221 }, 222 }, 223 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 224 Type: "string", 225 Mandatory: false, 226 Aliases: []config.Alias{}, 227 Default: os.Getenv("PIPER_targetRepositoryUser"), 228 }, 229 { 230 Name: "targetRepositoryURL", 231 ResourceRef: []config.ResourceReference{ 232 { 233 Name: "commonPipelineEnvironment", 234 Param: "custom/repositoryUrl", 235 }, 236 }, 237 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 238 Type: "string", 239 Mandatory: false, 240 Aliases: []config.Alias{}, 241 Default: os.Getenv("PIPER_targetRepositoryURL"), 242 }, 243 { 244 Name: "buildSettingsInfo", 245 ResourceRef: []config.ResourceReference{ 246 { 247 Name: "commonPipelineEnvironment", 248 Param: "custom/buildSettingsInfo", 249 }, 250 }, 251 Scope: []string{"STEPS", "STAGES", "PARAMETERS"}, 252 Type: "string", 253 Mandatory: false, 254 Aliases: []config.Alias{}, 255 Default: os.Getenv("PIPER_buildSettingsInfo"), 256 }, 257 { 258 Name: "virutalEnvironmentName", 259 ResourceRef: []config.ResourceReference{}, 260 Scope: []string{"STEPS", "STAGES", "PARAMETERS"}, 261 Type: "string", 262 Mandatory: false, 263 Aliases: []config.Alias{}, 264 Default: `piperBuild-env`, 265 }, 266 }, 267 }, 268 Containers: []config.Container{ 269 {Name: "python", Image: "python:3.9"}, 270 }, 271 Outputs: config.StepOutputs{ 272 Resources: []config.StepResources{ 273 { 274 Name: "commonPipelineEnvironment", 275 Type: "piperEnvironment", 276 Parameters: []map[string]interface{}{ 277 {"name": "custom/buildSettingsInfo"}, 278 }, 279 }, 280 }, 281 }, 282 }, 283 } 284 return theMetaData 285 }