1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-28 05:47:08 +02:00

feat: add ability to pass flags to setup.py in pythonBuild step (#5235)

This commit is contained in:
phgermanov 2025-01-15 18:12:24 +02:00 committed by GitHub
parent fb23269074
commit 6dfccc5753
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 54 additions and 5 deletions

View File

@ -52,7 +52,6 @@ func pythonBuild(config pythonBuildOptions, telemetryData *telemetry.CustomData,
}
func runPythonBuild(config *pythonBuildOptions, telemetryData *telemetry.CustomData, utils pythonBuildUtils, commonPipelineEnvironment *pythonBuildCommonPipelineEnvironment) error {
pipInstallFlags := []string{"install", "--upgrade"}
virutalEnvironmentPathMap := make(map[string]string)
@ -106,10 +105,11 @@ func runPythonBuild(config *pythonBuildOptions, telemetryData *telemetry.CustomD
}
func buildExecute(config *pythonBuildOptions, utils pythonBuildUtils, pipInstallFlags []string, virutalEnvironmentPathMap map[string]string) error {
var flags []string
flags = append(flags, config.BuildFlags...)
flags = append(flags, "setup.py", "sdist", "bdist_wheel")
flags = append(flags, "setup.py")
flags = append(flags, config.SetupFlags...)
flags = append(flags, "sdist", "bdist_wheel")
log.Entry().Info("starting building python project:")
err := utils.RunExecutable(virutalEnvironmentPathMap["python"], flags...)

View File

@ -20,6 +20,7 @@ import (
type pythonBuildOptions struct {
BuildFlags []string `json:"buildFlags,omitempty"`
SetupFlags []string `json:"setupFlags,omitempty"`
CreateBOM bool `json:"createBOM,omitempty"`
Publish bool `json:"publish,omitempty"`
TargetRepositoryPassword string `json:"targetRepositoryPassword,omitempty"`
@ -192,7 +193,8 @@ and are exposed are environment variables that must be present in the environmen
}
func addPythonBuildFlags(cmd *cobra.Command, stepConfig *pythonBuildOptions) {
cmd.Flags().StringSliceVar(&stepConfig.BuildFlags, "buildFlags", []string{}, "Defines list of build flags to be used.")
cmd.Flags().StringSliceVar(&stepConfig.BuildFlags, "buildFlags", []string{}, "Defines list of build flags passed to python binary.")
cmd.Flags().StringSliceVar(&stepConfig.SetupFlags, "setupFlags", []string{}, "Defines list of flags passed to setup.py.")
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.")
@ -224,6 +226,15 @@ func pythonBuildMetadata() config.StepData {
Aliases: []config.Alias{},
Default: []string{},
},
{
Name: "setupFlags",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "[]string",
Mandatory: false,
Aliases: []config.Alias{},
Default: []string{},
},
{
Name: "createBOM",
ResourceRef: []config.ResourceReference{},

View File

@ -105,3 +105,34 @@ func TestRunPythonBuild(t *testing.T) {
assert.Equal(t, []string{"--e", "--output", "bom-pip.xml", "--format", "xml", "--schema-version", "1.4"}, utils.ExecMockRunner.Calls[4].Params)
})
}
func TestPythonBuildExecute(t *testing.T) {
t.Run("Test build with flags", func(t *testing.T) {
config := pythonBuildOptions{
BuildFlags: []string{"--verbose"},
SetupFlags: []string{"egg_info", "--tag-build=pr13"},
VirutalEnvironmentName: "venv",
}
utils := pythonBuildMockUtils{
ExecMockRunner: &mock.ExecMockRunner{},
}
virutalEnvironmentPathMap := map[string]string{
"python": "python",
}
err := buildExecute(&config, &utils, []string{}, virutalEnvironmentPathMap)
assert.NoError(t, err)
assert.Equal(t, "python", utils.ExecMockRunner.Calls[0].Exec)
assert.Equal(t, []string{
"--verbose",
"setup.py",
"egg_info",
"--tag-build=pr13",
"sdist",
"bdist_wheel",
}, utils.ExecMockRunner.Calls[0].Params)
})
}

View File

@ -18,7 +18,14 @@ spec:
params:
- name: buildFlags
type: "[]string"
description: Defines list of build flags to be used.
description: Defines list of build flags passed to python binary.
scope:
- PARAMETERS
- STAGES
- STEPS
- name: setupFlags
type: "[]string"
description: Defines list of flags passed to setup.py.
scope:
- PARAMETERS
- STAGES