Drone CI - 如何将管道环境变量设置为 CLI 输出的结果

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

我认识到在管道步骤中我可以 运行 一个简单的 export,例如:

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

...但是如果我想在整个管道中使用这个环境变量,是否可以这样做:

environment:
  MY_ENV_VAR: $(my-command)

当我这样做时,我得到 yaml: unmarshal errors: line 23: cannot unmarshal !!seq into map[string]*yaml.Variable,这表明这是不可能的。我的最终目标是编写一个无人机插件,它接受 $(...) 的输出,如果它是 settings。我更希望使用无人机插件而不是 运行 命令,而只是使用输出。

我还尝试使用步骤依赖项来导出环境变量,但是它的状态不会在步骤之间延续:

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

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

如果在环境期间无法执行命令

也许你可以在“环境”块中定义一个“命令字符串”,例如:

environment:
  MY_ENV_VAR: 'echo "this is command to execute"'  # note the single quote

然后在命令块中,

commands:
      - eval $MY_ENV_VAR

值得一试

将命令输出写入脚本文件可能是执行您想要的操作的更好方法,因为文件系统更改在各个步骤之间保持不变

---
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

来自无人机的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.