Substitution

Updated 2 years ago by Admin

Drone provides the ability to expand, or substitute, repository and build metadata to facilitate dynamic pipeline configurations.

  • Example commit substitution:
    kind: pipeline
    type: docker
    name: default

    steps:
    - name: publish
    image: plugins/docker
    settings:
    tags: ${DRONE_COMMIT}
    repo: octocat/hello-world
  • Example tag substitution:
    steps:
    - name: publish
    image: plugins/docker
    settings:
    tags: ${DRONE_TAG}
    repo: octocat/hello-world

Please see the environment Reference for a list of parameters that can be used for substitution. Please note that some parameters in this list are unavailable for substitution, such as step name, step number, as well as parameters that store statuses and timestamps.


String Operations

Drone provides partial emulation for bash string operations. This can be used to manipulate string values prior to substitution.

  • Example variable substitution with substring:
    steps:
    - name: publish
    image: plugins/docker
    settings:
    tags: ${DRONE_COMMIT_SHA:0:8}
    repo: octocat/hello-world
  • Example variable substitution strips v prefix from v1.0.0:
    steps:
    - name: publish
    image: plugins/docker
    settings:
    tags: ${DRONE_TAG##v}
    repo: octocat/hello-world

Drone emulates the below string operations. Drone makes a best-effort to emulate these operations however we do not promise perfect emulation.

${parameter^}
${parameter^^}
${parameter,}
${parameter,,}
${parameter:position}
${parameter:position:length}
${parameter#substring}
${parameter##substring}
${parameter%substring}
${parameter%%substring}
${parameter/substring/replacement}
${parameter//substring/replacement}
${parameter/#substring/replacement}
${parameter/%substring/replacement}
${#parameter}
${parameter=default}
${parameter:=default}
${parameter:-default}

Escaping

Parameter expressions are evaluated before the yaml is parsed. If you do not want the system to evaluate an expression it must be escaped.

1  kind: pipeline
2 type: docker
3 name: default
4
5 steps:
6 - name: build
7 commands:
8 - echo $GOOS
9 - echo $${GOARCH}
10 - go build
11 - go test

Common Problems

Parameter substitution occurs before the yaml is parsed. If the substitution results in an invalid yaml file you will receive a parsing error:

yaml: unmarshal errors:
cannot unmarshal !!map into string

This can be resolved by quoting parameters to ensure special / reserved characters are escaped:

1  kind: pipeline
2 type: docker
3 name: default
4
5 steps:
6 - name: notify
7 image: plugins/slack
8 settings:
9 channel: team
10 message: "${DRONE_COMMIT}"


How did we do?