Can I use environment variable as a value for property in the step item

It it possible to use environment variable as a value for image?
Why do I need this?

  1. I don’t want duplicate code
  2. I want to change image name in one place instead of going to each pipeline and change it everywhere

For example:

environment:
  DOCKER_IMAGE_CACHE_NAME: "drillster/drone-volume-cache@sha256:some-sha"

steps:
- name: Restore cache
  image: "$${DOCKER_IMAGE_CACHE_NAME}"

The error I receive is Error response from daemon: invalid reference format.

None of these worked:

  1. ${DOCKER_IMAGE_CACHE_NAME}
  2. $${DOCKER_IMAGE_CACHE_NAME}
  3. $DOCKER_IMAGE_CACHE_NAME
  4. $$DOCKER_IMAGE_CACHE_NAME

Can I use environment variable as a value for property in the step item

Only variables in this list can be used as substitution parameters in step attributes. User-defined values are not supported. When Drone injects variables in the yaml, they are injected before the yaml is parsed (using something similar to find / replace). In your example, it would require injecting variables after the yaml is parsed which is not possible based on the current design.

This is actually supported using yaml features called anchors and aliases. These features are native to the yaml language; they are not unique to Drone. See the following document (or search google):
https://www.educative.io/blog/advanced-yaml-syntax-cheatsheet#anchors

Here is an example:

definitions: 
  image: &image "alpine"

steps:
- name: foo
  image: *image

- name: bar
  image: *image

Paste the above yaml into the below website, and you can see how it works:
https://www.convertjson.com/yaml-to-json.htm

Also note that Drone supports yaml alternatives, including Starlark and Jsonnet. If your goal is to keep your configuration DRY and anchors and aliases are insufficient, Starlark and Jsonnet are the recommended solution.

https://docs.drone.io/pipeline/scripting/jsonnet/
https://docs.drone.io/pipeline/scripting/starlark/

1 Like