mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-12 10:55:20 +02:00
216 lines
7.6 KiB
Go
216 lines
7.6 KiB
Go
|
// Code generated by piper's step-generator. DO NOT EDIT.
|
||
|
|
||
|
package cmd
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"time"
|
||
|
|
||
|
"github.com/SAP/jenkins-library/pkg/config"
|
||
|
"github.com/SAP/jenkins-library/pkg/log"
|
||
|
"github.com/SAP/jenkins-library/pkg/splunk"
|
||
|
"github.com/SAP/jenkins-library/pkg/telemetry"
|
||
|
"github.com/SAP/jenkins-library/pkg/validation"
|
||
|
"github.com/spf13/cobra"
|
||
|
)
|
||
|
|
||
|
type pythonBuildOptions struct {
|
||
|
BuildFlags []string `json:"buildFlags,omitempty"`
|
||
|
CreateBOM bool `json:"createBOM,omitempty"`
|
||
|
Publish bool `json:"publish,omitempty"`
|
||
|
TargetRepositoryPassword string `json:"targetRepositoryPassword,omitempty"`
|
||
|
TargetRepositoryUser string `json:"targetRepositoryUser,omitempty"`
|
||
|
TargetRepositoryURL string `json:"targetRepositoryURL,omitempty"`
|
||
|
}
|
||
|
|
||
|
// PythonBuildCommand Step build a python project
|
||
|
func PythonBuildCommand() *cobra.Command {
|
||
|
const STEP_NAME = "pythonBuild"
|
||
|
|
||
|
metadata := pythonBuildMetadata()
|
||
|
var stepConfig pythonBuildOptions
|
||
|
var startTime time.Time
|
||
|
var logCollector *log.CollectorHook
|
||
|
var splunkClient *splunk.Splunk
|
||
|
telemetryClient := &telemetry.Telemetry{}
|
||
|
|
||
|
var createPythonBuildCmd = &cobra.Command{
|
||
|
Use: STEP_NAME,
|
||
|
Short: "Step build a python project",
|
||
|
Long: `Step build python project with using test Vault credentials`,
|
||
|
PreRunE: func(cmd *cobra.Command, _ []string) error {
|
||
|
startTime = time.Now()
|
||
|
log.SetStepName(STEP_NAME)
|
||
|
log.SetVerbose(GeneralConfig.Verbose)
|
||
|
|
||
|
GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
|
||
|
|
||
|
path, _ := os.Getwd()
|
||
|
fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
|
||
|
log.RegisterHook(fatalHook)
|
||
|
|
||
|
err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
|
||
|
if err != nil {
|
||
|
log.SetErrorCategory(log.ErrorConfiguration)
|
||
|
return err
|
||
|
}
|
||
|
log.RegisterSecret(stepConfig.TargetRepositoryPassword)
|
||
|
log.RegisterSecret(stepConfig.TargetRepositoryUser)
|
||
|
|
||
|
if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
|
||
|
sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
|
||
|
log.RegisterHook(&sentryHook)
|
||
|
}
|
||
|
|
||
|
if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
|
||
|
splunkClient = &splunk.Splunk{}
|
||
|
logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
|
||
|
log.RegisterHook(logCollector)
|
||
|
}
|
||
|
|
||
|
validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if err = validation.ValidateStruct(stepConfig); err != nil {
|
||
|
log.SetErrorCategory(log.ErrorConfiguration)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
},
|
||
|
Run: func(_ *cobra.Command, _ []string) {
|
||
|
stepTelemetryData := telemetry.CustomData{}
|
||
|
stepTelemetryData.ErrorCode = "1"
|
||
|
handler := func() {
|
||
|
config.RemoveVaultSecretFiles()
|
||
|
stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
|
||
|
stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
|
||
|
stepTelemetryData.PiperCommitHash = GitCommit
|
||
|
telemetryClient.SetData(&stepTelemetryData)
|
||
|
telemetryClient.Send()
|
||
|
if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
|
||
|
splunkClient.Send(telemetryClient.GetData(), logCollector)
|
||
|
}
|
||
|
}
|
||
|
log.DeferExitHandler(handler)
|
||
|
defer handler()
|
||
|
telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
|
||
|
if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
|
||
|
splunkClient.Initialize(GeneralConfig.CorrelationID,
|
||
|
GeneralConfig.HookConfig.SplunkConfig.Dsn,
|
||
|
GeneralConfig.HookConfig.SplunkConfig.Token,
|
||
|
GeneralConfig.HookConfig.SplunkConfig.Index,
|
||
|
GeneralConfig.HookConfig.SplunkConfig.SendLogs)
|
||
|
}
|
||
|
pythonBuild(stepConfig, &stepTelemetryData)
|
||
|
stepTelemetryData.ErrorCode = "0"
|
||
|
log.Entry().Info("SUCCESS")
|
||
|
},
|
||
|
}
|
||
|
|
||
|
addPythonBuildFlags(createPythonBuildCmd, &stepConfig)
|
||
|
return createPythonBuildCmd
|
||
|
}
|
||
|
|
||
|
func addPythonBuildFlags(cmd *cobra.Command, stepConfig *pythonBuildOptions) {
|
||
|
cmd.Flags().StringSliceVar(&stepConfig.BuildFlags, "buildFlags", []string{}, "Defines list of build flags to be used.")
|
||
|
cmd.Flags().BoolVar(&stepConfig.CreateBOM, "createBOM", false, "Creates the bill of materials (BOM) using CycloneDX plugin.")
|
||
|
cmd.Flags().BoolVar(&stepConfig.Publish, "publish", false, "Configures the build to publish artifacts to a repository.")
|
||
|
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.")
|
||
|
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.")
|
||
|
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.")
|
||
|
|
||
|
}
|
||
|
|
||
|
// retrieve step metadata
|
||
|
func pythonBuildMetadata() config.StepData {
|
||
|
var theMetaData = config.StepData{
|
||
|
Metadata: config.StepMetadata{
|
||
|
Name: "pythonBuild",
|
||
|
Aliases: []config.Alias{},
|
||
|
Description: "Step build a python project",
|
||
|
},
|
||
|
Spec: config.StepSpec{
|
||
|
Inputs: config.StepInputs{
|
||
|
Parameters: []config.StepParameters{
|
||
|
{
|
||
|
Name: "buildFlags",
|
||
|
ResourceRef: []config.ResourceReference{},
|
||
|
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
|
||
|
Type: "[]string",
|
||
|
Mandatory: false,
|
||
|
Aliases: []config.Alias{},
|
||
|
Default: []string{},
|
||
|
},
|
||
|
{
|
||
|
Name: "createBOM",
|
||
|
ResourceRef: []config.ResourceReference{},
|
||
|
Scope: []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS"},
|
||
|
Type: "bool",
|
||
|
Mandatory: false,
|
||
|
Aliases: []config.Alias{},
|
||
|
Default: false,
|
||
|
},
|
||
|
{
|
||
|
Name: "publish",
|
||
|
ResourceRef: []config.ResourceReference{},
|
||
|
Scope: []string{"STEPS", "STAGES", "PARAMETERS"},
|
||
|
Type: "bool",
|
||
|
Mandatory: false,
|
||
|
Aliases: []config.Alias{},
|
||
|
Default: false,
|
||
|
},
|
||
|
{
|
||
|
Name: "targetRepositoryPassword",
|
||
|
ResourceRef: []config.ResourceReference{
|
||
|
{
|
||
|
Name: "commonPipelineEnvironment",
|
||
|
Param: "custom/repositoryPassword",
|
||
|
},
|
||
|
},
|
||
|
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
|
||
|
Type: "string",
|
||
|
Mandatory: false,
|
||
|
Aliases: []config.Alias{},
|
||
|
Default: os.Getenv("PIPER_targetRepositoryPassword"),
|
||
|
},
|
||
|
{
|
||
|
Name: "targetRepositoryUser",
|
||
|
ResourceRef: []config.ResourceReference{
|
||
|
{
|
||
|
Name: "commonPipelineEnvironment",
|
||
|
Param: "custom/repositoryUsername",
|
||
|
},
|
||
|
},
|
||
|
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
|
||
|
Type: "string",
|
||
|
Mandatory: false,
|
||
|
Aliases: []config.Alias{},
|
||
|
Default: os.Getenv("PIPER_targetRepositoryUser"),
|
||
|
},
|
||
|
{
|
||
|
Name: "targetRepositoryURL",
|
||
|
ResourceRef: []config.ResourceReference{
|
||
|
{
|
||
|
Name: "commonPipelineEnvironment",
|
||
|
Param: "custom/repositoryUrl",
|
||
|
},
|
||
|
},
|
||
|
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
|
||
|
Type: "string",
|
||
|
Mandatory: false,
|
||
|
Aliases: []config.Alias{},
|
||
|
Default: os.Getenv("PIPER_targetRepositoryURL"),
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
Containers: []config.Container{
|
||
|
{Name: "python", Image: "python:3.9", WorkingDir: "/home/node"},
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
return theMetaData
|
||
|
}
|