drone.io

Drone CI - How to set pipeline env var to result of CLI output


I recognize that within a pipeline step I can run a simple export, like:

    commands:
      - export MY_ENV_VAR=$(my-command)

...but if I want to use this env var throughout the whole pipeline, is it possible to do something like this:

environment:
  MY_ENV_VAR: $(my-command)

When I do this, I get yaml: unmarshal errors: line 23: cannot unmarshal !!seq into map[string]*yaml.Variable which suggests this isn't possible. My end goal is to write a drone plugin that accepts the output of $(...) as one if it's settings. I'd prefer to have the drone plugin not run the command, but just use the output.

I've also attempted to use step dependencies to export an env var, however it's state doesn't carry over between steps:

  - name: export
    image: bash
    commands:
      - export MY_VAR=$(my-command)

  - name: echo
    image: bash
    depends_on:
      - export
    commands:
      - echo $MY_VAR // empty

Solution

  • Writing the command output to a script file might be a better way to do what you want, since filesystem changes are persisted between individual steps.

    ---
    kind: pipeline
    type: docker
    
    steps:
      - name: generate-script
        image: bash
        commands:
          # - my-command > plugin-script.sh
          - printf "echo Fetching Google;\n\ncurl -I https://google.com/" > plugin-script.sh
    
      - name: test-script-1
        image: curlimages/curl
        commands:
          - sh plugin-script.sh
    
      - name: test-script-2
        image: curlimages/curl
        commands:
          - sh plugin-script.sh
    

    From Drone's Docker pipeline documentation:

    Workspace

    Drone automatically creates a temporary volume, known as your workspace, where it clones your repository. The workspace is the current working directory for each step in your pipeline.

    Because the workspace is a volume, filesystem changes are persisted between pipeline steps. In other words, individual steps can communicate and share state using the filesystem.

    Workspace volumes are ephemeral. They are created when the pipeline starts and destroyed after the pipeline completes.