From d034cd6fc368bb4f4efe98fc31b9c93bc8370807 Mon Sep 17 00:00:00 2001 From: Andrew Bayer Date: Fri, 27 Jan 2023 09:02:28 -0500 Subject: [PATCH] Add support for array task results as matrix parameters fixes #5925 Signed-off-by: Andrew Bayer --- docs/matrix.md | 30 ++++++----------- .../pipelinerun-with-matrix-and-results.yaml | 13 +++----- pkg/reconciler/pipelinerun/resources/apply.go | 2 +- .../pipelinerun/resources/apply_test.go | 32 +++++++++++++++++++ 4 files changed, 46 insertions(+), 31 deletions(-) diff --git a/docs/matrix.md b/docs/matrix.md index e0b21d3ba5b..ad820550799 100644 --- a/docs/matrix.md +++ b/docs/matrix.md @@ -166,40 +166,28 @@ Consuming `Results` from previous `TaskRuns` or `Runs` in a `Matrix`, which woul `TaskRuns` or `Runs` from the fanned out `PipelineTask`, is supported. Producing `Results` in from a `PipelineTask` with a `Matrix` is not yet supported - see [further details](#results-from-fanned-out-pipelinetasks). -`Matrix` supports Results of type String that are passed in individually: +`Matrix` supports Results of type String that are passed in individually, and of type Array, either individual +values by index or the entire array: ```yaml tasks: ... -- name: task-4 +- name: task-5 taskRef: - name: task-4 + name: task-5 matrix: params: - name: values value: - - (tasks.task-1.results.foo) # string - - (tasks.task-2.results.bar) # string - - (tasks.task-3.results.rad) # string + - $(tasks.task-1.results.foo) # string + - $(tasks.task-2.results.bar) # string + - $(tasks.task-3.results.rad[0]) # array index + - name: otherValues + value: $(tasks.task-4.results.someArray[*]) ``` For further information, see the example in [`PipelineRun` with `Matrix` and `Results`][pr-with-matrix-and-results]. -When we support `Results` of type Array at the `Pipeline` level, we will support passing Results into the `Matrix`. -> Note: Results of type Array are not yet supported in the Pipeline level. - -```yaml -tasks: -... -- name: task-5 - taskRef: - name: task-5 - matrix: - params: - - name: values - value: (tasks.task-4.results.foo) # array -``` - #### Results from fanned out PipelineTasks Consuming `Results` from fanned out `PipelineTasks` will not be in the supported in the initial iteration diff --git a/examples/v1beta1/pipelineruns/alpha/pipelinerun-with-matrix-and-results.yaml b/examples/v1beta1/pipelineruns/alpha/pipelinerun-with-matrix-and-results.yaml index 2b8c62b96bd..d53ce940186 100644 --- a/examples/v1beta1/pipelineruns/alpha/pipelinerun-with-matrix-and-results.yaml +++ b/examples/v1beta1/pipelineruns/alpha/pipelinerun-with-matrix-and-results.yaml @@ -39,16 +39,13 @@ spec: - name: get-browsers taskSpec: results: - - name: one - - name: two - - name: three + - name: browsers steps: - name: echo image: alpine script: | - printf chrome | tee /tekton/results/one - printf safari | tee /tekton/results/two - printf firefox | tee /tekton/results/three + #!/usr/bin/env bash + echo -n "[\"chrome\",\"safari\",\"firefox\"]" | tee $(results.browsers.path) - name: platforms-and-browsers-dag matrix: params: @@ -58,8 +55,6 @@ spec: - $(tasks.get-platforms.results.two) - $(tasks.get-platforms.results.three) - name: browser - value: - - $(tasks.get-browsers.results.one) - - $(tasks.get-browsers.results.two) + value: $(tasks.get-browsers.results.browsers[*]) taskRef: name: platform-browsers diff --git a/pkg/reconciler/pipelinerun/resources/apply.go b/pkg/reconciler/pipelinerun/resources/apply.go index c43ec9e5dbf..dee0bd542de 100644 --- a/pkg/reconciler/pipelinerun/resources/apply.go +++ b/pkg/reconciler/pipelinerun/resources/apply.go @@ -178,7 +178,7 @@ func ApplyTaskResults(targets PipelineRunState, resolvedResultRefs ResolvedResul pipelineTask := resolvedPipelineRunTask.PipelineTask.DeepCopy() pipelineTask.Params = replaceParamValues(pipelineTask.Params, stringReplacements, arrayReplacements, objectReplacements) if pipelineTask.IsMatrixed() { - pipelineTask.Matrix.Params = replaceParamValues(pipelineTask.Matrix.Params, stringReplacements, nil, nil) + pipelineTask.Matrix.Params = replaceParamValues(pipelineTask.Matrix.Params, stringReplacements, arrayReplacements, nil) } pipelineTask.WhenExpressions = pipelineTask.WhenExpressions.ReplaceWhenExpressionsVariables(stringReplacements, arrayReplacements) if pipelineTask.TaskRef != nil && pipelineTask.TaskRef.Params != nil { diff --git a/pkg/reconciler/pipelinerun/resources/apply_test.go b/pkg/reconciler/pipelinerun/resources/apply_test.go index af3c0503318..1e1dcd89e75 100644 --- a/pkg/reconciler/pipelinerun/resources/apply_test.go +++ b/pkg/reconciler/pipelinerun/resources/apply_test.go @@ -2330,6 +2330,38 @@ func TestApplyTaskResults_MinimalExpression(t *testing.T) { }}}, }, }}, + }, { + name: "Test array result substitution on minimal variable substitution expression - matrix", + resolvedResultRefs: ResolvedResultRefs{{ + Value: *v1beta1.NewStructuredValues("arrayResultValueOne", "arrayResultValueTwo"), + ResultReference: v1beta1.ResultRef{ + PipelineTask: "aTask", + Result: "aResult", + }, + FromTaskRun: "aTaskRun", + }}, + targets: PipelineRunState{{ + PipelineTask: &v1beta1.PipelineTask{ + Name: "bTask", + TaskRef: &v1beta1.TaskRef{Name: "bTask"}, + Matrix: &v1beta1.Matrix{ + Params: []v1beta1.Param{{ + Name: "bParam", + Value: *v1beta1.NewStructuredValues(`$(tasks.aTask.results.aResult[*])`), + }}}, + }, + }}, + want: PipelineRunState{{ + PipelineTask: &v1beta1.PipelineTask{ + Name: "bTask", + TaskRef: &v1beta1.TaskRef{Name: "bTask"}, + Matrix: &v1beta1.Matrix{ + Params: []v1beta1.Param{{ + Name: "bParam", + Value: *v1beta1.NewStructuredValues("arrayResultValueOne", "arrayResultValueTwo"), + }}}, + }, + }}, }, { name: "Test array result substitution on minimal variable substitution expression - when expressions", resolvedResultRefs: ResolvedResultRefs{{