diff --git a/pkg/apis/pipeline/v1alpha1/stepaction_types.go b/pkg/apis/pipeline/v1alpha1/stepaction_types.go index faee28dc2e5..2e90321b142 100644 --- a/pkg/apis/pipeline/v1alpha1/stepaction_types.go +++ b/pkg/apis/pipeline/v1alpha1/stepaction_types.go @@ -142,6 +142,21 @@ type StepActionSpec struct { VolumeMounts []corev1.VolumeMount `json:"volumeMounts,omitempty" patchStrategy:"merge" patchMergeKey:"mountPath" protobuf:"bytes,9,rep,name=volumeMounts"` } +// ToStep converts the StepActionSpec to a Step struct +func (ss *StepActionSpec) ToStep() *v1.Step { + return &v1.Step{ + Image: ss.Image, + Command: ss.Command, + Args: ss.Args, + WorkingDir: ss.WorkingDir, + Script: ss.Script, + Env: ss.Env, + VolumeMounts: ss.VolumeMounts, + SecurityContext: ss.SecurityContext, + Results: ss.Results, + } +} + // StepActionObject is implemented by StepAction type StepActionObject interface { apis.Defaultable diff --git a/pkg/reconciler/taskrun/resources/taskspec.go b/pkg/reconciler/taskrun/resources/taskspec.go index 9b9e14174d5..64d71df04e8 100644 --- a/pkg/reconciler/taskrun/resources/taskspec.go +++ b/pkg/reconciler/taskrun/resources/taskspec.go @@ -114,31 +114,33 @@ func GetStepActionsData(ctx context.Context, taskSpec v1.TaskSpec, taskRun *v1.T stepActionSpec := stepAction.StepActionSpec() stepActionSpec.SetDefaults(ctx) - s.Image = stepActionSpec.Image - s.SecurityContext = stepActionSpec.SecurityContext - if len(stepActionSpec.Command) > 0 { - s.Command = stepActionSpec.Command + stepFromStepAction := stepActionSpec.ToStep() + if err := validateStepHasStepActionParameters(s.Params, stepActionSpec.Params); err != nil { + return nil, err } - if len(stepActionSpec.Args) > 0 { - s.Args = stepActionSpec.Args + stepFromStepAction = applyStepActionParameters(stepFromStepAction, &taskSpec, taskRun, s.Params, stepActionSpec.Params) + + s.Image = stepFromStepAction.Image + s.SecurityContext = stepFromStepAction.SecurityContext + if len(stepFromStepAction.Command) > 0 { + s.Command = stepFromStepAction.Command } - if stepActionSpec.Script != "" { - s.Script = stepActionSpec.Script + if len(stepFromStepAction.Args) > 0 { + s.Args = stepFromStepAction.Args } - s.WorkingDir = stepActionSpec.WorkingDir - if stepActionSpec.Env != nil { - s.Env = stepActionSpec.Env + if stepFromStepAction.Script != "" { + s.Script = stepFromStepAction.Script } - if len(stepActionSpec.VolumeMounts) > 0 { - s.VolumeMounts = stepActionSpec.VolumeMounts + s.WorkingDir = stepFromStepAction.WorkingDir + if stepFromStepAction.Env != nil { + s.Env = stepFromStepAction.Env } - if len(stepActionSpec.Results) > 0 { - s.Results = stepActionSpec.Results + if len(stepFromStepAction.VolumeMounts) > 0 { + s.VolumeMounts = stepFromStepAction.VolumeMounts } - if err := validateStepHasStepActionParameters(s.Params, stepActionSpec.Params); err != nil { - return nil, err + if len(stepFromStepAction.Results) > 0 { + s.Results = stepFromStepAction.Results } - s = applyStepActionParameters(s, &taskSpec, taskRun, s.Params, stepActionSpec.Params) s.Params = nil s.Ref = nil steps = append(steps, *s) diff --git a/pkg/reconciler/taskrun/resources/taskspec_test.go b/pkg/reconciler/taskrun/resources/taskspec_test.go index 1e8d8ac603e..2fdde6b2b6e 100644 --- a/pkg/reconciler/taskrun/resources/taskspec_test.go +++ b/pkg/reconciler/taskrun/resources/taskspec_test.go @@ -817,6 +817,61 @@ func TestGetStepActionsData(t *testing.T) { Image: "myimage", Args: []string{"taskrun string param", "taskspec", "array", "taskspec", "array", "param", "taskrun key", "taskspec key2", "step action key3"}, }}, + }, { + name: "params in step propagated to stepaction only", + tr: &v1.TaskRun{ + ObjectMeta: metav1.ObjectMeta{ + Name: "mytaskrun", + Namespace: "default", + }, + Spec: v1.TaskRunSpec{ + Params: v1.Params{{ + Name: "stringparam", + Value: v1.ParamValue{ + Type: v1.ParamTypeString, + StringVal: "taskrun string param", + }, + }}, + TaskSpec: &v1.TaskSpec{ + Steps: []v1.Step{{ + Ref: &v1.Ref{ + Name: "stepAction", + }, + Params: v1.Params{{ + Name: "stringparam", + Value: v1.ParamValue{ + Type: v1.ParamTypeString, + StringVal: "step string param", + }, + }}, + OnError: v1.OnErrorType("$(params.stringparam)"), + StdoutConfig: &v1.StepOutputConfig{Path: "$(params.stringparam)"}, + StderrConfig: &v1.StepOutputConfig{Path: "$(params.stringparam)"}, + }}, + }, + }, + }, + stepAction: &v1alpha1.StepAction{ + ObjectMeta: metav1.ObjectMeta{ + Name: "stepAction", + Namespace: "default", + }, + Spec: v1alpha1.StepActionSpec{ + Image: "myimage", + Args: []string{"echo", "$(params.stringparam)"}, + Params: v1.ParamSpecs{{ + Name: "stringparam", + Type: v1.ParamTypeString, + }}, + }, + }, + want: []v1.Step{{ + Image: "myimage", + Args: []string{"echo", "step string param"}, + OnError: v1.OnErrorType("$(params.stringparam)"), + StdoutConfig: &v1.StepOutputConfig{Path: "$(params.stringparam)"}, + StderrConfig: &v1.StepOutputConfig{Path: "$(params.stringparam)"}, + }}, }, { name: "propagating step result substitution strings into step actions", tr: &v1.TaskRun{ diff --git a/pkg/reconciler/taskrun/taskrun_test.go b/pkg/reconciler/taskrun/taskrun_test.go index 0f265e5cf9f..256364c2333 100644 --- a/pkg/reconciler/taskrun/taskrun_test.go +++ b/pkg/reconciler/taskrun/taskrun_test.go @@ -3693,6 +3693,47 @@ spec: Args: []string{"taskrun string param", "taskrun", "array", "taskrun", "array", "param", "taskrun object param"}, Name: "step1", }}, + }, { + name: "params with the same name propagated from taskrun and step", + taskRun: parse.MustParseV1TaskRun(t, ` +metadata: + name: taskrun-with-string-params + namespace: foo +spec: + params: + - name: stringparam + value: "taskrun string param" + taskSpec: + steps: + - ref: + name: stepAction + name: step1 + stdoutConfig: + path: $(params.stringparam) + params: + - name: stringparam + value: "step string param" +`), + stepAction: parse.MustParseV1alpha1StepAction(t, ` +metadata: + name: stepAction + namespace: foo +spec: + params: + - name: stringparam + image: myImage + command: ["echo"] + args: ["$(params.stringparam)"] +`), + want: []v1.Step{{ + Image: "myImage", + Command: []string{"echo"}, + Args: []string{"step string param"}, + Name: "step1", + StdoutConfig: &v1.StepOutputConfig{ + Path: "taskrun string param", + }, + }}, }, { name: "step results", taskRun: parse.MustParseV1TaskRun(t, `