diff --git a/docs/variables.md b/docs/variables.md
index 3d1f95830d9..4d4ef6f5c6d 100644
--- a/docs/variables.md
+++ b/docs/variables.md
@@ -15,6 +15,7 @@ This page documents the variable substitions supported by `Tasks` and `Pipelines
| `params.` | The value of the parameter at runtime. |
| `tasks..results.` | The value of the `Task's` result. Can alter `Task` execution order within a `Pipeline`.) |
| `context.pipelineRun.name` | The name of the `PipelineRun` that this `Pipeline` is running in. |
+| `context.pipelineRun.namespace` | The namespace of the `PipelineRun` that this `Pipeline` is running in. |
| `context.pipeline.name` | The name of this `Pipeline` . |
@@ -31,6 +32,7 @@ This page documents the variable substitions supported by `Tasks` and `Pipelines
| `workspaces..volume` | The name of the volume populating the `Workspace`. |
| `credentials.path` | The path to credentials injected from Secrets with matching annotations. |
| `context.taskRun.name` | The name of the `TaskRun` that this `Task` is running in. |
+| `context.taskRun.namespace` | The namespace of the `TaskRun` that this `Task` is running in. |
| `context.task.name` | The name of this `Task`. |
### `PipelineResource` variables available in a `Task`
diff --git a/pkg/reconciler/pipelinerun/resources/apply.go b/pkg/reconciler/pipelinerun/resources/apply.go
index 33cb61e83c2..4962f727b05 100644
--- a/pkg/reconciler/pipelinerun/resources/apply.go
+++ b/pkg/reconciler/pipelinerun/resources/apply.go
@@ -56,12 +56,10 @@ func ApplyParameters(p *v1beta1.PipelineSpec, pr *v1beta1.PipelineRun) *v1beta1.
// ApplyContexts applies the substitution from $(context.(pipelineRun|pipeline).*) with the specified values.
// Currently supports only name substitution. Uses "" as a default if name is not specified.
func ApplyContexts(spec *v1beta1.PipelineSpec, pipelineName string, pr *v1beta1.PipelineRun) *v1beta1.PipelineSpec {
- stringReplacements := map[string]string{}
- stringReplacements["context.pipelineRun.name"] = pr.Name
- stringReplacements["context.pipeline.name"] = pipelineName
-
return ApplyReplacements(spec,
- map[string]string{"context.pipelineRun.name": pr.Name, "context.pipeline.name": pipelineName},
+ map[string]string{"context.pipelineRun.name": pr.Name,
+ "context.pipeline.name": pipelineName,
+ "context.pipelineRun.namespace": pr.Namespace},
map[string][]string{})
}
diff --git a/pkg/reconciler/pipelinerun/resources/apply_test.go b/pkg/reconciler/pipelinerun/resources/apply_test.go
index bac752218ab..003b5beef6b 100644
--- a/pkg/reconciler/pipelinerun/resources/apply_test.go
+++ b/pkg/reconciler/pipelinerun/resources/apply_test.go
@@ -451,6 +451,19 @@ func TestContext(t *testing.T) {
tb.PipelineTask("first-task-1", "first-task",
tb.PipelineTaskParam("first-task-first-param", "pipelineRunName-1"),
))),
+ }, {
+ description: "context pipelineRunNameNamespace replacement with defined pipelineRunNamepsace in spec",
+ pr: tb.PipelineRun("pipelineRunName", tb.PipelineRunNamespace("prns")),
+ original: tb.Pipeline("test-pipeline",
+ tb.PipelineSpec(
+ tb.PipelineTask("first-task-1", "first-task",
+ tb.PipelineTaskParam("first-task-first-param", "$(context.pipelineRun.namespace)-1"),
+ ))),
+ expected: tb.Pipeline("test-pipeline",
+ tb.PipelineSpec(
+ tb.PipelineTask("first-task-1", "first-task",
+ tb.PipelineTaskParam("first-task-first-param", "prns-1"),
+ ))),
}, {
description: "context pipelineRunName replacement with no defined pipeline in spec",
pr: &v1beta1.PipelineRun{},
@@ -464,6 +477,19 @@ func TestContext(t *testing.T) {
tb.PipelineTask("first-task-1", "first-task",
tb.PipelineTaskParam("first-task-first-param", "-1"),
))),
+ }, {
+ description: "context pipelineRunNamespace replacement with no defined pipelineRunNamespace in spec",
+ pr: tb.PipelineRun("pipelineRunName"),
+ original: tb.Pipeline("test-pipeline",
+ tb.PipelineSpec(
+ tb.PipelineTask("first-task-1", "first-task",
+ tb.PipelineTaskParam("first-task-first-param", "$(context.pipelineRun.namespace)-1"),
+ ))),
+ expected: tb.Pipeline("test-pipeline",
+ tb.PipelineSpec(
+ tb.PipelineTask("first-task-1", "first-task",
+ tb.PipelineTaskParam("first-task-first-param", "-1"),
+ ))),
}} {
t.Run(tc.description, func(t *testing.T) {
got := ApplyContexts(&tc.original.Spec, tc.original.Name, tc.pr)
diff --git a/pkg/reconciler/taskrun/resources/apply.go b/pkg/reconciler/taskrun/resources/apply.go
index 8d2e5828c57..e8706acfb69 100644
--- a/pkg/reconciler/taskrun/resources/apply.go
+++ b/pkg/reconciler/taskrun/resources/apply.go
@@ -99,12 +99,8 @@ func ApplyResources(spec *v1beta1.TaskSpec, resolvedResources map[string]v1beta1
// ApplyContexts applies the substitution from $(context.(taskRun|task).*) with the specified values.
// Currently supports only name substitution. Uses "" as a default if name is not specified.
func ApplyContexts(spec *v1beta1.TaskSpec, rtr *ResolvedTaskResources, tr *v1beta1.TaskRun) *v1beta1.TaskSpec {
- stringReplacements := map[string]string{}
- stringReplacements["context.taskRun.name"] = tr.Name
- stringReplacements["context.task.name"] = rtr.TaskName
-
return ApplyReplacements(spec,
- map[string]string{"context.taskRun.name": tr.Name, "context.task.name": rtr.TaskName},
+ map[string]string{"context.taskRun.name": tr.Name, "context.task.name": rtr.TaskName, "context.taskRun.namespace": tr.Namespace},
map[string][]string{})
}
diff --git a/pkg/reconciler/taskrun/resources/apply_test.go b/pkg/reconciler/taskrun/resources/apply_test.go
index 6d44575d457..e30587b0013 100644
--- a/pkg/reconciler/taskrun/resources/apply_test.go
+++ b/pkg/reconciler/taskrun/resources/apply_test.go
@@ -839,7 +839,7 @@ func TestContext(t *testing.T) {
}},
},
}, {
- description: "context taskRunName replacement with no defined taskRun in spec container",
+ description: "context taskRunName replacement with no defined taskRun name in spec container",
rtr: resources.ResolvedTaskResources{
TaskName: "Task1",
},
@@ -860,6 +860,55 @@ func TestContext(t *testing.T) {
},
}},
},
+ }, {
+ description: "context taskRun namespace replacement with no defined namepsace in spec container",
+ rtr: resources.ResolvedTaskResources{
+ TaskName: "Task1",
+ },
+ tr: v1beta1.TaskRun{},
+ spec: v1beta1.TaskSpec{
+ Steps: []v1beta1.Step{{
+ Container: corev1.Container{
+ Name: "ImageName",
+ Image: "$(context.taskRun.namespace)-1",
+ },
+ }},
+ },
+ want: v1beta1.TaskSpec{
+ Steps: []v1beta1.Step{{
+ Container: corev1.Container{
+ Name: "ImageName",
+ Image: "-1",
+ },
+ }},
+ },
+ }, {
+ description: "context taskRun namespace replacement with defined namepsace in spec container",
+ rtr: resources.ResolvedTaskResources{
+ TaskName: "Task1",
+ },
+ tr: v1beta1.TaskRun{
+ ObjectMeta: metav1.ObjectMeta{
+ Name: "taskrunName",
+ Namespace: "trNamespace",
+ },
+ },
+ spec: v1beta1.TaskSpec{
+ Steps: []v1beta1.Step{{
+ Container: corev1.Container{
+ Name: "ImageName",
+ Image: "$(context.taskRun.namespace)-1",
+ },
+ }},
+ },
+ want: v1beta1.TaskSpec{
+ Steps: []v1beta1.Step{{
+ Container: corev1.Container{
+ Name: "ImageName",
+ Image: "trNamespace-1",
+ },
+ }},
+ },
}, {
description: "context taskRunName replacement with no defined taskName in spec container",
rtr: resources.ResolvedTaskResources{},