1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-07-13 01:30:24 +02:00

fix (pythonBuild) run as root and generate build settings info (#3695)

* adding PIP to BuildTool.groovy

* trying to run the container with root

* only creating sdist

* including wheel distribution

* adding settings info

Co-authored-by: anilkeshav27 <you@example.com>
This commit is contained in:
Anil Keshav
2022-04-05 09:16:46 +02:00
committed by GitHub
parent 3c55d3c99c
commit aa74e090d5
5 changed files with 112 additions and 14 deletions

View File

@ -5,10 +5,12 @@ package cmd
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/piperenv"
"github.com/SAP/jenkins-library/pkg/splunk"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/SAP/jenkins-library/pkg/validation"
@ -22,6 +24,35 @@ type pythonBuildOptions struct {
TargetRepositoryPassword string `json:"targetRepositoryPassword,omitempty"`
TargetRepositoryUser string `json:"targetRepositoryUser,omitempty"`
TargetRepositoryURL string `json:"targetRepositoryURL,omitempty"`
BuildSettingsInfo string `json:"buildSettingsInfo,omitempty"`
}
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")
}
}
// PythonBuildCommand Step build a python project
@ -31,6 +62,7 @@ func PythonBuildCommand() *cobra.Command {
metadata := pythonBuildMetadata()
var stepConfig pythonBuildOptions
var startTime time.Time
var commonPipelineEnvironment pythonBuildCommonPipelineEnvironment
var logCollector *log.CollectorHook
var splunkClient *splunk.Splunk
telemetryClient := &telemetry.Telemetry{}
@ -84,6 +116,7 @@ func PythonBuildCommand() *cobra.Command {
stepTelemetryData := telemetry.CustomData{}
stepTelemetryData.ErrorCode = "1"
handler := func() {
commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment")
config.RemoveVaultSecretFiles()
stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
@ -104,7 +137,7 @@ func PythonBuildCommand() *cobra.Command {
GeneralConfig.HookConfig.SplunkConfig.Index,
GeneralConfig.HookConfig.SplunkConfig.SendLogs)
}
pythonBuild(stepConfig, &stepTelemetryData)
pythonBuild(stepConfig, &stepTelemetryData, &commonPipelineEnvironment)
stepTelemetryData.ErrorCode = "0"
log.Entry().Info("SUCCESS")
},
@ -121,6 +154,7 @@ func addPythonBuildFlags(cmd *cobra.Command, stepConfig *pythonBuildOptions) {
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.")
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.")
}
@ -204,10 +238,35 @@ func pythonBuildMetadata() config.StepData {
Aliases: []config.Alias{},
Default: os.Getenv("PIPER_targetRepositoryURL"),
},
{
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"),
},
},
},
Containers: []config.Container{
{Name: "python", Image: "python:3.9", WorkingDir: "/home/node"},
{Name: "python", Image: "python:3.9", Options: []config.Option{{Name: "-u", Value: "0"}}},
},
Outputs: config.StepOutputs{
Resources: []config.StepResources{
{
Name: "commonPipelineEnvironment",
Type: "piperEnvironment",
Parameters: []map[string]interface{}{
{"name": "custom/buildSettingsInfo"},
},
},
},
},
},
}