github.com/SAP/jenkins-library@v1.362.0/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  	RequirementsFilePath     string   `json:"requirementsFilePath,omitempty"`
    30  }
    31  
    32  type pythonBuildCommonPipelineEnvironment struct {
    33  	custom struct {
    34  		buildSettingsInfo string
    35  	}
    36  }
    37  
    38  func (p *pythonBuildCommonPipelineEnvironment) persist(path, resourceName string) {
    39  	content := []struct {
    40  		category string
    41  		name     string
    42  		value    interface{}
    43  	}{
    44  		{category: "custom", name: "buildSettingsInfo", value: p.custom.buildSettingsInfo},
    45  	}
    46  
    47  	errCount := 0
    48  	for _, param := range content {
    49  		err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(param.category, param.name), param.value)
    50  		if err != nil {
    51  			log.Entry().WithError(err).Error("Error persisting piper environment.")
    52  			errCount++
    53  		}
    54  	}
    55  	if errCount > 0 {
    56  		log.Entry().Error("failed to persist Piper environment")
    57  	}
    58  }
    59  
    60  // PythonBuildCommand Step builds a python project
    61  func PythonBuildCommand() *cobra.Command {
    62  	const STEP_NAME = "pythonBuild"
    63  
    64  	metadata := pythonBuildMetadata()
    65  	var stepConfig pythonBuildOptions
    66  	var startTime time.Time
    67  	var commonPipelineEnvironment pythonBuildCommonPipelineEnvironment
    68  	var logCollector *log.CollectorHook
    69  	var splunkClient *splunk.Splunk
    70  	telemetryClient := &telemetry.Telemetry{}
    71  
    72  	var createPythonBuildCmd = &cobra.Command{
    73  		Use:   STEP_NAME,
    74  		Short: "Step builds a python project",
    75  		Long: `Step build python project using the setup.py manifest and builds a wheel and tarball artifact . please note that currently python build only supports setup.py
    76  
    77  ### build with depedencies from a private repository
    78  if your build has dependencies from a private repository you can include the standard requirements.txt into the source code with ` + "`" + `--extra-index-url` + "`" + ` as the first line
    79  
    80  ` + "`" + `` + "`" + `` + "`" + `
    81  --extra-index-url https://${PIPER_VAULTCREDENTIAL_USERNAME}:${PIPER_VAULTCREDENTIAL_PASSWORD}@<privateRepoUrl>/simple
    82  ` + "`" + `` + "`" + `` + "`" + `
    83  ` + "`" + `PIPER_VAULTCREDENTIAL_USERNAME` + "`" + ` and ` + "`" + `PIPER_VAULTCREDENTIAL_PASSWORD` + "`" + ` are the username and password for the private repository
    84  and are exposed are environment variables that must be present in the environment where the Piper step runs or alternatively can be created using :
    85  [vault general purpose credentials](../infrastructure/vault.md#using-vault-for-general-purpose-and-test-credentials)`,
    86  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    87  			startTime = time.Now()
    88  			log.SetStepName(STEP_NAME)
    89  			log.SetVerbose(GeneralConfig.Verbose)
    90  
    91  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    92  
    93  			path, _ := os.Getwd()
    94  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    95  			log.RegisterHook(fatalHook)
    96  
    97  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    98  			if err != nil {
    99  				log.SetErrorCategory(log.ErrorConfiguration)
   100  				return err
   101  			}
   102  			log.RegisterSecret(stepConfig.TargetRepositoryPassword)
   103  			log.RegisterSecret(stepConfig.TargetRepositoryUser)
   104  
   105  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
   106  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
   107  				log.RegisterHook(&sentryHook)
   108  			}
   109  
   110  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 || len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   111  				splunkClient = &splunk.Splunk{}
   112  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
   113  				log.RegisterHook(logCollector)
   114  			}
   115  
   116  			if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
   117  				log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
   118  			}
   119  
   120  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
   121  			if err != nil {
   122  				return err
   123  			}
   124  			if err = validation.ValidateStruct(stepConfig); err != nil {
   125  				log.SetErrorCategory(log.ErrorConfiguration)
   126  				return err
   127  			}
   128  
   129  			return nil
   130  		},
   131  		Run: func(_ *cobra.Command, _ []string) {
   132  			stepTelemetryData := telemetry.CustomData{}
   133  			stepTelemetryData.ErrorCode = "1"
   134  			handler := func() {
   135  				commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment")
   136  				config.RemoveVaultSecretFiles()
   137  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
   138  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
   139  				stepTelemetryData.PiperCommitHash = GitCommit
   140  				telemetryClient.SetData(&stepTelemetryData)
   141  				telemetryClient.Send()
   142  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   143  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   144  						GeneralConfig.HookConfig.SplunkConfig.Dsn,
   145  						GeneralConfig.HookConfig.SplunkConfig.Token,
   146  						GeneralConfig.HookConfig.SplunkConfig.Index,
   147  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   148  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   149  				}
   150  				if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   151  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   152  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
   153  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
   154  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
   155  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   156  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   157  				}
   158  			}
   159  			log.DeferExitHandler(handler)
   160  			defer handler()
   161  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME, GeneralConfig.HookConfig.PendoConfig.Token)
   162  			pythonBuild(stepConfig, &stepTelemetryData, &commonPipelineEnvironment)
   163  			stepTelemetryData.ErrorCode = "0"
   164  			log.Entry().Info("SUCCESS")
   165  		},
   166  	}
   167  
   168  	addPythonBuildFlags(createPythonBuildCmd, &stepConfig)
   169  	return createPythonBuildCmd
   170  }
   171  
   172  func addPythonBuildFlags(cmd *cobra.Command, stepConfig *pythonBuildOptions) {
   173  	cmd.Flags().StringSliceVar(&stepConfig.BuildFlags, "buildFlags", []string{}, "Defines list of build flags to be used.")
   174  	cmd.Flags().BoolVar(&stepConfig.CreateBOM, "createBOM", false, "Creates the bill of materials (BOM) using CycloneDX plugin.")
   175  	cmd.Flags().BoolVar(&stepConfig.Publish, "publish", false, "Configures the build to publish artifacts to a repository.")
   176  	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.")
   177  	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.")
   178  	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.")
   179  	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.")
   180  	cmd.Flags().StringVar(&stepConfig.VirutalEnvironmentName, "virutalEnvironmentName", `piperBuild-env`, "name of the virtual environment that will be used for the build")
   181  	cmd.Flags().StringVar(&stepConfig.RequirementsFilePath, "requirementsFilePath", `requirements.txt`, "file path to the requirements.txt file needed for the sbom cycloneDx file creation.")
   182  
   183  }
   184  
   185  // retrieve step metadata
   186  func pythonBuildMetadata() config.StepData {
   187  	var theMetaData = config.StepData{
   188  		Metadata: config.StepMetadata{
   189  			Name:        "pythonBuild",
   190  			Aliases:     []config.Alias{},
   191  			Description: "Step builds a python project",
   192  		},
   193  		Spec: config.StepSpec{
   194  			Inputs: config.StepInputs{
   195  				Parameters: []config.StepParameters{
   196  					{
   197  						Name:        "buildFlags",
   198  						ResourceRef: []config.ResourceReference{},
   199  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   200  						Type:        "[]string",
   201  						Mandatory:   false,
   202  						Aliases:     []config.Alias{},
   203  						Default:     []string{},
   204  					},
   205  					{
   206  						Name:        "createBOM",
   207  						ResourceRef: []config.ResourceReference{},
   208  						Scope:       []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS"},
   209  						Type:        "bool",
   210  						Mandatory:   false,
   211  						Aliases:     []config.Alias{},
   212  						Default:     false,
   213  					},
   214  					{
   215  						Name:        "publish",
   216  						ResourceRef: []config.ResourceReference{},
   217  						Scope:       []string{"STEPS", "STAGES", "PARAMETERS"},
   218  						Type:        "bool",
   219  						Mandatory:   false,
   220  						Aliases:     []config.Alias{},
   221  						Default:     false,
   222  					},
   223  					{
   224  						Name: "targetRepositoryPassword",
   225  						ResourceRef: []config.ResourceReference{
   226  							{
   227  								Name:  "commonPipelineEnvironment",
   228  								Param: "custom/repositoryPassword",
   229  							},
   230  						},
   231  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   232  						Type:      "string",
   233  						Mandatory: false,
   234  						Aliases:   []config.Alias{},
   235  						Default:   os.Getenv("PIPER_targetRepositoryPassword"),
   236  					},
   237  					{
   238  						Name: "targetRepositoryUser",
   239  						ResourceRef: []config.ResourceReference{
   240  							{
   241  								Name:  "commonPipelineEnvironment",
   242  								Param: "custom/repositoryUsername",
   243  							},
   244  						},
   245  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   246  						Type:      "string",
   247  						Mandatory: false,
   248  						Aliases:   []config.Alias{},
   249  						Default:   os.Getenv("PIPER_targetRepositoryUser"),
   250  					},
   251  					{
   252  						Name: "targetRepositoryURL",
   253  						ResourceRef: []config.ResourceReference{
   254  							{
   255  								Name:  "commonPipelineEnvironment",
   256  								Param: "custom/repositoryUrl",
   257  							},
   258  						},
   259  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   260  						Type:      "string",
   261  						Mandatory: false,
   262  						Aliases:   []config.Alias{},
   263  						Default:   os.Getenv("PIPER_targetRepositoryURL"),
   264  					},
   265  					{
   266  						Name: "buildSettingsInfo",
   267  						ResourceRef: []config.ResourceReference{
   268  							{
   269  								Name:  "commonPipelineEnvironment",
   270  								Param: "custom/buildSettingsInfo",
   271  							},
   272  						},
   273  						Scope:     []string{"STEPS", "STAGES", "PARAMETERS"},
   274  						Type:      "string",
   275  						Mandatory: false,
   276  						Aliases:   []config.Alias{},
   277  						Default:   os.Getenv("PIPER_buildSettingsInfo"),
   278  					},
   279  					{
   280  						Name:        "virutalEnvironmentName",
   281  						ResourceRef: []config.ResourceReference{},
   282  						Scope:       []string{"STEPS", "STAGES", "PARAMETERS"},
   283  						Type:        "string",
   284  						Mandatory:   false,
   285  						Aliases:     []config.Alias{},
   286  						Default:     `piperBuild-env`,
   287  					},
   288  					{
   289  						Name:        "requirementsFilePath",
   290  						ResourceRef: []config.ResourceReference{},
   291  						Scope:       []string{"STEPS", "STAGES", "PARAMETERS"},
   292  						Type:        "string",
   293  						Mandatory:   false,
   294  						Aliases:     []config.Alias{},
   295  						Default:     `requirements.txt`,
   296  					},
   297  				},
   298  			},
   299  			Containers: []config.Container{
   300  				{Name: "python", Image: "python:3.9"},
   301  			},
   302  			Outputs: config.StepOutputs{
   303  				Resources: []config.StepResources{
   304  					{
   305  						Name: "commonPipelineEnvironment",
   306  						Type: "piperEnvironment",
   307  						Parameters: []map[string]interface{}{
   308  							{"name": "custom/buildSettingsInfo"},
   309  						},
   310  					},
   311  				},
   312  			},
   313  		},
   314  	}
   315  	return theMetaData
   316  }