-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parameter variable substitution support for conditionals #1143
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ This document defines `Conditions` and their capabilities. | |
|
||
- [Syntax](#syntax) | ||
- [Check](#check) | ||
- [Parameters](#parameters) | ||
- [Examples](#examples) | ||
|
||
## Syntax | ||
|
@@ -32,11 +33,34 @@ following fields: | |
### Check | ||
|
||
The `check` field is required. You define a single check to define the body of a `Condition`. The | ||
check must specify a container image that adheres to the [container contract](./container-contract.md). The container image | ||
runs till completion. The container must exit successfully i.e. with an exit code 0 for the | ||
condition evaluation to be successful. All other exit codes are considered to be a condition check | ||
check must specify a container image that adheres to the [container contract](./container-contract.md). | ||
The container image runs till completion. The container must exit successfully i.e. with an exit code 0 | ||
for the condition evaluation to be successful. All other exit codes are considered to be a condition check | ||
failure. | ||
|
||
### Parameters | ||
|
||
A Condition can declare parameters that must be supplied to it during a PipelineRun. Sub-fields | ||
within the check field can access the parameter values using the templating syntax: | ||
|
||
```yaml | ||
spec: | ||
parameters: | ||
- name: image | ||
default: ubuntu | ||
check: | ||
image: ${params.image} | ||
``` | ||
|
||
Parameters name are limited to alpha-numeric characters, `-` and `_` and can | ||
only start with alpha characters and `_`. For example, `fooIs-Bar_` is a valid | ||
parameter name, `barIsBa$` or `0banana` are not. | ||
|
||
Each declared parameter has a type field, assumed to be string if not provided by the user. | ||
The other possible type is array — useful,checking a pushed branch name doesn't match any of | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: space after the comma |
||
multiple protected branch names. When the actual parameter value is supplied, its parsed type | ||
is validated against the type field. | ||
|
||
## Examples | ||
|
||
For complete examples, see | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,14 +60,19 @@ func ApplyReplacements(p *v1alpha1.Pipeline, replacements map[string]string, arr | |
tasks := p.Spec.Tasks | ||
|
||
for i := range tasks { | ||
params := tasks[i].Params | ||
|
||
for j := range params { | ||
params[j].Value.ApplyReplacements(replacements, arrayReplacements) | ||
tasks[i].Params = replaceParamValues(tasks[i].Params, replacements, arrayReplacements) | ||
for j := range tasks[i].Conditions { | ||
c := tasks[i].Conditions[j] | ||
c.Params = replaceParamValues(c.Params, replacements, arrayReplacements) | ||
} | ||
|
||
tasks[i].Params = params | ||
} | ||
|
||
return p | ||
} | ||
|
||
func replaceParamValues(params []v1alpha1.Param, stringReplacements map[string]string, arrayReplacements map[string][]string) []v1alpha1.Param { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice :) |
||
for i := range params { | ||
params[i].Value.ApplyReplacements(stringReplacements, arrayReplacements) | ||
} | ||
return params | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ import ( | |
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
|
||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" | ||
tb "github.com/tektoncd/pipeline/test/builder" | ||
) | ||
|
@@ -69,6 +70,49 @@ func TestApplyParameters(t *testing.T) { | |
tb.PipelineTask("first-task-1", "first-task", | ||
tb.PipelineTaskParam("first-task-first-param", "${input.workspace.default-value}"), | ||
))), | ||
}, { | ||
name: "single parameter in task condition", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: I guess the test name refers to the amount of parameters passed in the run? One is left to default and one not. Perhaps the name could be changed? |
||
original: tb.Pipeline("test-pipeline", "foo", | ||
tb.PipelineSpec( | ||
tb.PipelineParamSpec("first-param", v1alpha1.ParamTypeString, tb.ParamSpecDefault("default-value")), | ||
tb.PipelineParamSpec("second-param", v1alpha1.ParamTypeString), | ||
tb.PipelineTask("first-task-1", "first-task", | ||
tb.PipelineTaskCondition("task-condition", | ||
tb.PipelineTaskConditionParam("cond-first-param", "${params.first-param}"), | ||
tb.PipelineTaskConditionParam("cond-second-param", "${params.second-param}"), | ||
), | ||
))), | ||
run: tb.PipelineRun("test-pipeline-run", "foo", | ||
tb.PipelineRunSpec("test-pipeline", | ||
tb.PipelineRunParam("second-param", "second-value"))), | ||
expected: tb.Pipeline("test-pipeline", "foo", | ||
tb.PipelineSpec( | ||
tb.PipelineParamSpec("first-param", v1alpha1.ParamTypeString, tb.ParamSpecDefault("default-value")), | ||
tb.PipelineParamSpec("second-param", v1alpha1.ParamTypeString), | ||
tb.PipelineTask("first-task-1", "first-task", | ||
tb.PipelineTaskCondition("task-condition", | ||
tb.PipelineTaskConditionParam("cond-first-param", "default-value"), | ||
tb.PipelineTaskConditionParam("cond-second-param", "second-value"), | ||
), | ||
))), | ||
}, { | ||
name: "pipeline parameter nested inside condition parameter", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: again, the name of the test is a bit confusing for me, in this case there is only one parameter and it's left to default. |
||
original: tb.Pipeline("test-pipeline", "foo", | ||
tb.PipelineSpec( | ||
tb.PipelineParamSpec("first-param", v1alpha1.ParamTypeString, tb.ParamSpecDefault("default-value")), | ||
tb.PipelineTask("first-task-1", "first-task", | ||
tb.PipelineTaskCondition("cond", | ||
tb.PipelineTaskConditionParam("cond-first-param", "${params.first-param}")), | ||
))), | ||
run: tb.PipelineRun("test-pipeline-run", "foo", | ||
tb.PipelineRunSpec("test-pipeline")), | ||
expected: tb.Pipeline("test-pipeline", "foo", | ||
tb.PipelineSpec( | ||
tb.PipelineParamSpec("first-param", v1alpha1.ParamTypeString, tb.ParamSpecDefault("default-value")), | ||
tb.PipelineTask("first-task-1", "first-task", | ||
tb.PipelineTaskCondition("cond", | ||
tb.PipelineTaskConditionParam("cond-first-param", "default-value")), | ||
))), | ||
}, { | ||
name: "single array parameter", | ||
original: tb.Pipeline("test-pipeline", "foo", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: space after the comma