mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-03-29 21:57:01 +02:00
feat(gradle): Support for more than one task/flags (#4329)
* feat(gradle) support task list * Change parameter name to buildFlags to align with other piper steps'
This commit is contained in:
parent
52944953eb
commit
01cfb07d15
@ -177,9 +177,11 @@ func runGradleExecuteBuild(config *gradleExecuteBuildOptions, telemetryData *tel
|
||||
}
|
||||
|
||||
// gradle build
|
||||
// if user provides BuildFlags, it is respected over a single Task
|
||||
gradleOptions := &gradle.ExecuteOptions{
|
||||
BuildGradlePath: config.Path,
|
||||
Task: config.Task,
|
||||
BuildFlags: config.BuildFlags,
|
||||
UseWrapper: config.UseWrapper,
|
||||
}
|
||||
if _, err := gradle.Execute(gradleOptions, utils); err != nil {
|
||||
|
@ -36,6 +36,7 @@ type gradleExecuteBuildOptions struct {
|
||||
ApplyPublishingForAllProjects bool `json:"applyPublishingForAllProjects,omitempty"`
|
||||
ExcludeCreateBOMForProjects []string `json:"excludeCreateBOMForProjects,omitempty"`
|
||||
ExcludePublishingForProjects []string `json:"excludePublishingForProjects,omitempty"`
|
||||
BuildFlags []string `json:"buildFlags,omitempty"`
|
||||
}
|
||||
|
||||
type gradleExecuteBuildReports struct {
|
||||
@ -118,7 +119,7 @@ func GradleExecuteBuildCommand() *cobra.Command {
|
||||
var createGradleExecuteBuildCmd = &cobra.Command{
|
||||
Use: STEP_NAME,
|
||||
Short: "This step runs a gradle build command with parameters provided to the step.",
|
||||
Long: `This step runs a gradle build command with parameters provided to the step.`,
|
||||
Long: `This step runs a gradle build command with parameters provided to the step.Supports execution of gradle tasks with or without wrapper.Gradle tasks and flags can be specified via 'task' or 'buildFlags' parameter. If both are not specified 'build' task will run by default.`,
|
||||
PreRunE: func(cmd *cobra.Command, _ []string) error {
|
||||
startTime = time.Now()
|
||||
log.SetStepName(STEP_NAME)
|
||||
@ -202,7 +203,7 @@ func GradleExecuteBuildCommand() *cobra.Command {
|
||||
|
||||
func addGradleExecuteBuildFlags(cmd *cobra.Command, stepConfig *gradleExecuteBuildOptions) {
|
||||
cmd.Flags().StringVar(&stepConfig.Path, "path", os.Getenv("PIPER_path"), "Path to the folder with build.gradle (or build.gradle.kts) file which should be executed.")
|
||||
cmd.Flags().StringVar(&stepConfig.Task, "task", `build`, "Gradle task that should be executed.")
|
||||
cmd.Flags().StringVar(&stepConfig.Task, "task", `build`, "A single gradle task that should be executed. If you prefer more than one, use 'buildFlags' parameter. If 'buildFlags' parameter is specified, this parameter will be ignored.")
|
||||
cmd.Flags().BoolVar(&stepConfig.Publish, "publish", false, "Configures gradle to publish the artifact to a repository.")
|
||||
cmd.Flags().StringVar(&stepConfig.RepositoryURL, "repositoryUrl", os.Getenv("PIPER_repositoryUrl"), "Url to the repository to which the project artifacts should be published.")
|
||||
cmd.Flags().StringVar(&stepConfig.RepositoryPassword, "repositoryPassword", os.Getenv("PIPER_repositoryPassword"), "Password for the repository to which the project artifacts should be published.")
|
||||
@ -215,6 +216,7 @@ func addGradleExecuteBuildFlags(cmd *cobra.Command, stepConfig *gradleExecuteBui
|
||||
cmd.Flags().BoolVar(&stepConfig.ApplyPublishingForAllProjects, "applyPublishingForAllProjects", false, "If set to false publishing logic will be applied in 'rootProject' directive, otherwise 'allprojects' will be directive used")
|
||||
cmd.Flags().StringSliceVar(&stepConfig.ExcludeCreateBOMForProjects, "excludeCreateBOMForProjects", []string{}, "Defines which projects/subprojects will be ignored during bom creation. Only if applyCreateBOMForAllProjects is set to true")
|
||||
cmd.Flags().StringSliceVar(&stepConfig.ExcludePublishingForProjects, "excludePublishingForProjects", []string{}, "Defines which projects/subprojects will be ignored during publishing. Only if applyCreateBOMForAllProjects is set to true")
|
||||
cmd.Flags().StringSliceVar(&stepConfig.BuildFlags, "buildFlags", []string{}, "Defines a list of tasks and/or arguments to be provided for gradle in the respective order to be executed. This list takes precedence if specified over 'task' parameter")
|
||||
|
||||
}
|
||||
|
||||
@ -385,6 +387,15 @@ func gradleExecuteBuildMetadata() config.StepData {
|
||||
Aliases: []config.Alias{},
|
||||
Default: []string{},
|
||||
},
|
||||
{
|
||||
Name: "buildFlags",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
|
||||
Type: "[]string",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
Default: []string{},
|
||||
},
|
||||
},
|
||||
},
|
||||
Containers: []config.Container{
|
||||
|
@ -75,6 +75,25 @@ func TestRunGradleExecuteBuild(t *testing.T) {
|
||||
assert.Equal(t, mock.ExecCall{Exec: "gradle", Params: []string{"build", "-p", "path/to"}}, utils.Calls[0])
|
||||
})
|
||||
|
||||
t.Run("success case - build with flags", func(t *testing.T) {
|
||||
utils := gradleExecuteBuildMockUtils{
|
||||
ExecMockRunner: &mock.ExecMockRunner{},
|
||||
FilesMock: &mock.FilesMock{},
|
||||
}
|
||||
utils.FilesMock.AddFile("path/to/build.gradle", []byte{})
|
||||
options := &gradleExecuteBuildOptions{
|
||||
Path: "path/to",
|
||||
Task: "build",
|
||||
BuildFlags: []string{"clean", "build", "-x", "test"},
|
||||
UseWrapper: false,
|
||||
}
|
||||
|
||||
err := runGradleExecuteBuild(options, nil, utils, pipelineEnv)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, len(utils.Calls))
|
||||
assert.Equal(t, mock.ExecCall{Exec: "gradle", Params: []string{"clean", "build", "-x", "test", "-p", "path/to"}}, utils.Calls[0])
|
||||
})
|
||||
|
||||
t.Run("success case - bom creation", func(t *testing.T) {
|
||||
utils := gradleExecuteBuildMockUtils{
|
||||
ExecMockRunner: &mock.ExecMockRunner{},
|
||||
@ -168,6 +187,26 @@ func TestRunGradleExecuteBuild(t *testing.T) {
|
||||
assert.Contains(t, err.Error(), "failed to build")
|
||||
})
|
||||
|
||||
t.Run("failed case - build with flags", func(t *testing.T) {
|
||||
utils := gradleExecuteBuildMockUtils{
|
||||
ExecMockRunner: &mock.ExecMockRunner{
|
||||
ShouldFailOnCommand: map[string]error{"gradle clean build -x test -p path/to": errors.New("failed to build with flags")},
|
||||
},
|
||||
FilesMock: &mock.FilesMock{},
|
||||
}
|
||||
utils.FilesMock.AddFile("path/to/build.gradle", []byte{})
|
||||
options := &gradleExecuteBuildOptions{
|
||||
Path: "path/to",
|
||||
Task: "build",
|
||||
BuildFlags: []string{"clean", "build", "-x", "test"},
|
||||
UseWrapper: false,
|
||||
}
|
||||
|
||||
err := runGradleExecuteBuild(options, nil, utils, pipelineEnv)
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "failed to build with flags")
|
||||
})
|
||||
|
||||
t.Run("failed case - bom creation", func(t *testing.T) {
|
||||
utils := gradleExecuteBuildMockUtils{
|
||||
ExecMockRunner: &mock.ExecMockRunner{
|
||||
|
@ -35,6 +35,7 @@ type Utils interface {
|
||||
type ExecuteOptions struct {
|
||||
BuildGradlePath string `json:"path,omitempty"`
|
||||
Task string `json:"task,omitempty"`
|
||||
BuildFlags []string `json:"buildFlags,omitempty"`
|
||||
InitScriptContent string `json:"initScriptContent,omitempty"`
|
||||
UseWrapper bool `json:"useWrapper,omitempty"`
|
||||
ProjectProperties map[string]string `json:"projectProperties,omitempty"`
|
||||
@ -100,8 +101,13 @@ func Execute(options *ExecuteOptions, utils Utils) (string, error) {
|
||||
func getParametersFromOptions(options *ExecuteOptions) []string {
|
||||
var parameters []string
|
||||
|
||||
// default value for task is 'build', so no necessary to checking for empty parameter
|
||||
parameters = append(parameters, options.Task)
|
||||
if len(options.BuildFlags) > 0 {
|
||||
// respect the list of tasks/flags user wants to execute
|
||||
parameters = append(parameters, options.BuildFlags...)
|
||||
} else {
|
||||
// default value for task is 'build', so no necessary to checking for empty parameter
|
||||
parameters = append(parameters, options.Task)
|
||||
}
|
||||
|
||||
// resolve path for build.gradle execution
|
||||
if options.BuildGradlePath != "" {
|
||||
|
@ -1,7 +1,7 @@
|
||||
metadata:
|
||||
name: gradleExecuteBuild
|
||||
description: This step runs a gradle build command with parameters provided to the step.
|
||||
longDescription: This step runs a gradle build command with parameters provided to the step.
|
||||
longDescription: This step runs a gradle build command with parameters provided to the step.Supports execution of gradle tasks with or without wrapper.Gradle tasks and flags can be specified via 'task' or 'buildFlags' parameter. If both are not specified 'build' task will run by default.
|
||||
spec:
|
||||
inputs:
|
||||
params:
|
||||
@ -18,7 +18,7 @@ spec:
|
||||
mandatory: false
|
||||
- name: task
|
||||
type: string
|
||||
description: Gradle task that should be executed.
|
||||
description: A single gradle task that should be executed. If you prefer more than one, use 'buildFlags' parameter. If 'buildFlags' parameter is specified, this parameter will be ignored.
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
@ -137,6 +137,25 @@ spec:
|
||||
- STAGES
|
||||
- STEPS
|
||||
type: "[]string"
|
||||
- name: buildFlags
|
||||
type: "[]string"
|
||||
description: Defines a list of tasks and/or arguments to be provided for gradle in the respective order to be executed. This list takes precedence if specified over 'task' parameter
|
||||
longDescription: |
|
||||
To run command `gradle clean build -x test` , it can be achieved as follows
|
||||
|
||||
```
|
||||
steps:
|
||||
gradleExecuteBuild:
|
||||
buildFlags:
|
||||
- clean
|
||||
- build
|
||||
- -x
|
||||
- test
|
||||
```
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
||||
outputs:
|
||||
resources:
|
||||
- name: reports
|
||||
|
Loading…
x
Reference in New Issue
Block a user