diff --git a/docs/developers/README.md b/docs/developers/README.md index 4d444241cac..aca8b70cba6 100644 --- a/docs/developers/README.md +++ b/docs/developers/README.md @@ -275,3 +275,5 @@ status: value: | 1579722445 ``` + +Instead of hardcoding the path to the result file, the user can also use a variable. So `/tekton/results/current-date-unix-timestamp` can be replaced with: `$(results.current-date-unix-timestamp.path)`. This is more flexible if the path to result files ever changes. diff --git a/pkg/reconciler/taskrun/resources/apply.go b/pkg/reconciler/taskrun/resources/apply.go index b621a0bc75b..482a5a78a4f 100644 --- a/pkg/reconciler/taskrun/resources/apply.go +++ b/pkg/reconciler/taskrun/resources/apply.go @@ -18,9 +18,11 @@ package resources import ( "fmt" + "path/filepath" "github.com/tektoncd/pipeline/pkg/workspace" + "github.com/tektoncd/pipeline/pkg/apis/pipeline" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" "github.com/tektoncd/pipeline/pkg/substitution" ) @@ -96,6 +98,17 @@ func ApplyWorkspaces(spec *v1alpha1.TaskSpec, w []v1alpha1.WorkspaceDeclaration, return ApplyReplacements(spec, stringReplacements, map[string][]string{}) } +// ApplyTaskResults applies the substitution from values in results which are referenced in spec as subitems +// of the replacementStr. +func ApplyTaskResults(spec *v1alpha1.TaskSpec) *v1alpha1.TaskSpec { + stringReplacements := map[string]string{} + + for _, result := range spec.Results { + stringReplacements[fmt.Sprintf("results.%s.path", result.Name)] = filepath.Join(pipeline.DefaultResultPath, result.Name) + } + return ApplyReplacements(spec, stringReplacements, map[string][]string{}) +} + // ApplyReplacements replaces placeholders for declared parameters with the specified replacements. func ApplyReplacements(spec *v1alpha1.TaskSpec, stringReplacements map[string]string, arrayReplacements map[string][]string) *v1alpha1.TaskSpec { spec = spec.DeepCopy() diff --git a/pkg/reconciler/taskrun/resources/apply_test.go b/pkg/reconciler/taskrun/resources/apply_test.go index 2fa61c3e270..c5ff0bef4ed 100644 --- a/pkg/reconciler/taskrun/resources/apply_test.go +++ b/pkg/reconciler/taskrun/resources/apply_test.go @@ -720,6 +720,42 @@ func TestApplyWorkspaces(t *testing.T) { }} got := resources.ApplyWorkspaces(ts, w, wb) if d := cmp.Diff(got, want); d != "" { - t.Errorf("ApplyParameters() got diff %s", d) + t.Errorf("TestApplyWorkspaces() got diff %s", d) + } +} + +func TestTaskResults(t *testing.T) { + names.TestingSeed() + ts := &v1alpha1.TaskSpec{ + Results: []v1alpha1.TaskResult{{ + Name: "current-date-unix-timestamp", + Description: "The current date in unix timestamp format", + }, { + Name: "current-date-human-readable", + Description: "The current date in humand readable format"}, + }, + Steps: []v1alpha1.Step{{ + Container: corev1.Container{ + Name: "print-date-unix-timestamp", + Image: "bash:latest", + Args: []string{"$(results.current-date-unix-timestamp.path)"}, + }, + Script: "#!/usr/bin/env bash\ndate +%s | tee $(results.current-date-unix-timestamp.path)", + }, { + Container: corev1.Container{ + Name: "print-date-humman-readable", + Image: "bash:latest", + }, + Script: "#!/usr/bin/env bash\ndate | tee $(results.current-date-human-readable.path)", + }}, + } + want := applyMutation(ts, func(spec *v1alpha1.TaskSpec) { + spec.Steps[0].Script = "#!/usr/bin/env bash\ndate +%s | tee /tekton/results/current-date-unix-timestamp" + spec.Steps[0].Args[0] = "/tekton/results/current-date-unix-timestamp" + spec.Steps[1].Script = "#!/usr/bin/env bash\ndate | tee /tekton/results/current-date-human-readable" + }) + got := resources.ApplyTaskResults(ts) + if d := cmp.Diff(got, want); d != "" { + t.Errorf("ApplyTaskResults() got diff %s", d) } } diff --git a/pkg/reconciler/taskrun/taskrun.go b/pkg/reconciler/taskrun/taskrun.go index c45345c630f..e286d4fba7e 100644 --- a/pkg/reconciler/taskrun/taskrun.go +++ b/pkg/reconciler/taskrun/taskrun.go @@ -513,6 +513,9 @@ func (c *Reconciler) createPod(tr *v1alpha1.TaskRun, rtr *resources.ResolvedTask // Apply workspace resource substitution ts = resources.ApplyWorkspaces(ts, ts.Workspaces, tr.Spec.Workspaces) + // Apply task result substitution + ts = resources.ApplyTaskResults(ts) + ts, err = workspace.Apply(*ts, tr.Spec.Workspaces) if err != nil { c.Logger.Errorf("Failed to create a pod for taskrun: %s due to workspace error %v", tr.Name, err)