【问题标题】:Drone CI - How to set pipeline env var to result of CLI outputDrone CI - 如何将管道环境变量设置为 CLI 输出的结果
【发布时间】:2021-12-21 19:34:13
【问题描述】:

我认识到在管道步骤中我可以运行一个简单的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,它接受$(...) 的输出作为一个。我希望无人机插件不运行命令,而只使用输出。

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

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

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

【问题讨论】:

    标签: drone.io


    【解决方案1】:

    将命令输出写入脚本文件可能是执行所需操作的更好方法,因为文件系统更改会在各个步骤之间持久化

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

    来自 Drone 的Docker pipeline documentation

    工作区

    Drone 会自动创建一个临时卷,称为您的工作区,它会在其中克隆您的存储库。工作区是管道中每个步骤的当前工作目录。

    因为工作空间是一个卷,所以文件系统更改在管道步骤之间保持不变。换句话说,各个步骤可以使用文件系统进行通信和共享状态。

    工作区卷是短暂的。它们在管道启动时创建,在管道完成后销毁。

    【讨论】:

      【解决方案2】:

      如果在环境期间不能执行命令。

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

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

      然后在命令块中,

      commands:
            - eval $MY_ENV_VAR
      

      值得一试

      【讨论】:

        猜你喜欢
        • 2021-05-27
        • 1970-01-01
        • 2021-04-17
        • 2019-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-12
        • 1970-01-01
        相关资源
        最近更新 更多