Skip to content

Commit

Permalink
Improve documentation around variable behaviour and gotchas
Browse files Browse the repository at this point in the history
We used to link from the docs directory's README.md to our variables.md doc
but variables.md doesn't really explain the way variables work - it just
describes what values they can be used for and where they can be used in
our CRDs.

This commit changes the docs directory's README.md to point at the variable
documentation in our task doc, and adds a few lines there to talk a bit
more about how the substitution happens.

This commit also describes how to safely inject variables into `bash`
`script` blocks in Tasks without accidentally eval'ing the variable
contents.
  • Loading branch information
Scott authored and tekton-robot committed Dec 17, 2020
1 parent a8415d9 commit 55fdb95
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ See the following topics to learn how to use Tekton Pipelines in your project:
- [Using labels](labels.md)
- [Viewing logs](logs.md)
- [Pipelines metrics](metrics.md)
- [Variable Substitutions](variables.md)
- [Variable Substitutions](tasks.md#using-variable-substitution)
- [Running a Custom Task (alpha)](runs.md)

## Contributing to Tekton Pipelines
Expand Down
14 changes: 14 additions & 0 deletions docs/pipelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ weight: 3
- [Guard `Task` execution using `When Expressions`](#guard-task-execution-using-whenexpressions)
- [Guard `Task` execution using `Conditions`](#guard-task-execution-using-conditions)
- [Configuring the failure timeout](#configuring-the-failure-timeout)
- [Using variable substitution](#using-variable-substitution)
- [Using `Results`](#using-results)
- [Passing one Task's `Results` into the `Parameters` of another](#passing-one-tasks-results-into-the-parameters-of-another)
- [Emitting `Results` from a `Pipeline`](#emitting-results-from-a-pipeline)
Expand Down Expand Up @@ -514,6 +515,19 @@ spec:
timeout: "0h1m30s"
```

## Using variable substitution

Tekton provides variables to inject values into the contents of certain fields.
The values you can inject come from a range of sources including other fields
in the Pipeline, context-sensitive information that Tekton provides, and runtime
information received from a PipelineRun.

The mechanism of variable substitution is quite simple - string replacement is
performed by the Tekton Controller when a PipelineRun is executed.

See the [complete list of variable substitutions for Pipelines](./variables.md#variables-available-in-a-pipeline)
and the [list of fields that accept substitutions](./variables.md#fields-that-accept-variable-substitutions).

## Using `Results`

Tasks can emit [`Results`](tasks.md#emitting-results) when they execute. A Pipeline can use these
Expand Down
40 changes: 39 additions & 1 deletion docs/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ weight: 1
- [Substituting `Array` parameters](#substituting-array-parameters)
- [Substituting `Workspace` paths](#substituting-workspace-paths)
- [Substituting `Volume` names and types](#substituting-volume-names-and-types)
- [Substituting in `Script` blocks](#substituting-in-script-blocks)
- [Code examples](#code-examples)
- [Building and pushing a Docker image](#building-and-pushing-a-docker-image)
- [Mounting multiple `Volumes`](#mounting-multiple-volumes)
Expand Down Expand Up @@ -581,14 +582,23 @@ The `description` field is an optional field that allows you to add an informati

### Using variable substitution

Tekton provides variables to inject values into the contents of certain fields.
The values you can inject come from a range of sources including other fields
in the Task, context-sensitive information that Tekton provides, and runtime
information received from a TaskRun.

The mechanism of variable substitution is quite simple - string replacement is
performed by the Tekton Controller when a TaskRun is executed.

`Tasks` allow you to substitute variable names for the following entities:

- [Parameters and resources](#substituting-parameters-and-resources)
- [`Array` parameters](#substituting-array-parameters)
- [`Workspaces`](#substituting-workspace-paths)
- [`Volume` names and types](#substituting-volume-names-and-paths)

Also see the [complete list of variable substitutions for Tasks](./variables.md#variables-available-in-a-task).
See the [complete list of variable substitutions for Tasks](./variables.md#variables-available-in-a-task)
and the [list of fields that accept substitutions](./variables.md#fields-that-accept-variable-substitutions).

#### Substituting parameters and resources

Expand Down Expand Up @@ -666,6 +676,34 @@ by parameterizing them. Tekton supports popular `Volume` types such as `ConfigMa
See this [example](#mounting-a-configmap-as-a-volume-source) to find out how to perform this type of substitution
in your `Task.`

#### Substituting in `Script` blocks

Variables can contain any string, including snippets of script that can
be injected into a Task's `Script` field. If you are using Tekton's variables
in your Task's `Script` field be aware that the strings you're interpolating
could include executable instructions.

Preventing a substituted variable from executing as code depends on the container
image, language or shell that your Task uses. Here's an example of interpolating
a Tekton variable into a `bash` `Script` block that prevents the variable's string
contents from being executed:

```yaml
# Task.yaml
spec:
steps:
- image: an-image-that-runs-bash
env:
- name: SCRIPT_CONTENTS
value: $(params.script)
script: |
printf '%s' "${SCRIPT_CONTENTS}" > input-script
```

This works by injecting Tekton's variable as an environment variable into the Step's
container. The `printf` program is then used to write the environment variable's
content to a file.

## Code examples

Study the following code examples to better understand how to configure your `Tasks`:
Expand Down
3 changes: 3 additions & 0 deletions docs/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ weight: 15
# Variable Substitutions Supported by `Tasks` and `Pipelines`

This page documents the variable substitutions supported by `Tasks` and `Pipelines`.

For instructions on using variable substitutions see the relevant section of [the Tasks doc](tasks.md#using-variable-substitution).

**Note:** Tekton does not escape the contents of variables. Task authors are responsible for properly escaping a variable's value according to the shell, image or scripting language that the variable will be used in.

## Variables available in a `Pipeline`
Expand Down

0 comments on commit 55fdb95

Please sign in to comment.