Env Interpolation

by bradrydzewski

Drone provides the ability to substitute environment variables at runtime. This gives us the ability to use dynamic build or commit details in our pipeline configuration.

Example commit substitution:

pipeline:
  docker:
    image: plugins/docker
-   tags: ${DRONE_COMMIT_SHA}
+   tags: 553c2077f0edc3d5dc5d17262f6aa498e69d6f8e

Example tag substitution:

pipeline:
  docker:
    image: plugins/docker
-   tags: ${DRONE_TAG}
+   tags: v1.0.0

Drone also emulates bash string operations. This gives us the ability to manipulate the strings prior to substitution. Example use cases might include substring and stripping prefix or suffix values.

Example variable substitution with substring:

pipeline:
  docker:
    image: plugins/docker
-   tags: ${DRONE_COMMIT_SHA:0:8}
+   tags: 553c2077

Example variable substitution strips v prefix from v.1.0.0:

pipeline:
  docker:
    image: plugins/docker
-   tags: ${DRONE_TAG##v}
+   tags: 1.0.0

List of all emulated bash string operations:

OPERATION DESC
${param} parameter substitution
${param,} parameter substitution with lowercase first char
${param,,} parameter substitution with lowercase
${param^} parameter substitution with uppercase first char
${param^^} parameter substitution with uppercase
${param:pos} parameter substitution with substring
${param:pos:len} parameter substitution with substring and length
${param=default} parameter substitution with default
${param##prefix} parameter substitution with prefix removal
${param%%suffix} parameter substitution with suffix removal
${param/old/new} parameter substitution with find and replace