2022-03-29 19:01:44 +02:00
// Code generated by piper's step-generator. DO NOT EDIT.
package cmd
import (
"fmt"
"os"
2022-04-05 09:16:46 +02:00
"path/filepath"
2022-03-29 19:01:44 +02:00
"time"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
2022-04-05 09:16:46 +02:00
"github.com/SAP/jenkins-library/pkg/piperenv"
2022-03-29 19:01:44 +02:00
"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" `
2022-04-05 09:16:46 +02:00
BuildSettingsInfo string ` json:"buildSettingsInfo,omitempty" `
2022-04-26 14:15:24 +02:00
VirutalEnvironmentName string ` json:"virutalEnvironmentName,omitempty" `
2022-04-05 09:16:46 +02:00
}
type pythonBuildCommonPipelineEnvironment struct {
custom struct {
buildSettingsInfo string
}
}
func ( p * pythonBuildCommonPipelineEnvironment ) persist ( path , resourceName string ) {
content := [ ] struct {
category string
name string
value interface { }
} {
{ category : "custom" , name : "buildSettingsInfo" , value : p . custom . buildSettingsInfo } ,
}
errCount := 0
for _ , param := range content {
err := piperenv . SetResourceParameter ( path , resourceName , filepath . Join ( param . category , param . name ) , param . value )
if err != nil {
log . Entry ( ) . WithError ( err ) . Error ( "Error persisting piper environment." )
errCount ++
}
}
if errCount > 0 {
log . Entry ( ) . Error ( "failed to persist Piper environment" )
}
2022-03-29 19:01:44 +02:00
}
// PythonBuildCommand Step build a python project
func PythonBuildCommand ( ) * cobra . Command {
const STEP_NAME = "pythonBuild"
metadata := pythonBuildMetadata ( )
var stepConfig pythonBuildOptions
var startTime time . Time
2022-04-05 09:16:46 +02:00
var commonPipelineEnvironment pythonBuildCommonPipelineEnvironment
2022-03-29 19:01:44 +02:00
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 )
}
2022-06-22 13:31:17 +02:00
if err = log . RegisterANSHookIfConfigured ( GeneralConfig . CorrelationID ) ; err != nil {
log . Entry ( ) . WithError ( err ) . Warn ( "failed to set up SAP Alert Notification Service log hook" )
}
2022-03-29 19:01:44 +02:00
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 ( ) {
2022-04-05 09:16:46 +02:00
commonPipelineEnvironment . persist ( GeneralConfig . EnvRootPath , "commonPipelineEnvironment" )
2022-03-29 19:01:44 +02:00
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 {
2023-07-14 15:19:57 +02:00
splunkClient . Initialize ( GeneralConfig . CorrelationID ,
GeneralConfig . HookConfig . SplunkConfig . Dsn ,
GeneralConfig . HookConfig . SplunkConfig . Token ,
GeneralConfig . HookConfig . SplunkConfig . Index ,
GeneralConfig . HookConfig . SplunkConfig . SendLogs )
splunkClient . Send ( telemetryClient . GetData ( ) , logCollector )
}
if len ( GeneralConfig . HookConfig . SplunkConfig . ProdCriblEndpoint ) > 0 {
splunkClient . Initialize ( GeneralConfig . CorrelationID ,
GeneralConfig . HookConfig . SplunkConfig . ProdCriblEndpoint ,
GeneralConfig . HookConfig . SplunkConfig . ProdCriblToken ,
GeneralConfig . HookConfig . SplunkConfig . ProdCriblIndex ,
GeneralConfig . HookConfig . SplunkConfig . SendLogs )
2022-03-29 19:01:44 +02:00
splunkClient . Send ( telemetryClient . GetData ( ) , logCollector )
}
}
log . DeferExitHandler ( handler )
defer handler ( )
telemetryClient . Initialize ( GeneralConfig . NoTelemetry , STEP_NAME )
2022-04-05 09:16:46 +02:00
pythonBuild ( stepConfig , & stepTelemetryData , & commonPipelineEnvironment )
2022-03-29 19:01:44 +02:00
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." )
2022-04-05 09:16:46 +02:00
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." )
2022-04-26 14:15:24 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . VirutalEnvironmentName , "virutalEnvironmentName" , ` piperBuild-env ` , "name of the virtual environment that will be used for the build" )
2022-03-29 19:01:44 +02:00
}
// 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" ) ,
} ,
2022-04-05 09:16:46 +02:00
{
Name : "buildSettingsInfo" ,
ResourceRef : [ ] config . ResourceReference {
{
Name : "commonPipelineEnvironment" ,
Param : "custom/buildSettingsInfo" ,
} ,
} ,
Scope : [ ] string { "STEPS" , "STAGES" , "PARAMETERS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
Default : os . Getenv ( "PIPER_buildSettingsInfo" ) ,
} ,
2022-04-26 14:15:24 +02:00
{
Name : "virutalEnvironmentName" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "STEPS" , "STAGES" , "PARAMETERS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
Default : ` piperBuild-env ` ,
} ,
2022-03-29 19:01:44 +02:00
} ,
} ,
Containers : [ ] config . Container {
2022-04-26 14:15:24 +02:00
{ Name : "python" , Image : "python:3.9" } ,
2022-04-05 09:16:46 +02:00
} ,
Outputs : config . StepOutputs {
Resources : [ ] config . StepResources {
{
Name : "commonPipelineEnvironment" ,
Type : "piperEnvironment" ,
Parameters : [ ] map [ string ] interface { } {
{ "name" : "custom/buildSettingsInfo" } ,
} ,
} ,
} ,
2022-03-29 19:01:44 +02:00
} ,
} ,
}
return theMetaData
}