如何在CircleCI中将布尔环境变量传递给“何时”步骤?

时间:2019-05-23 18:43:18

标签: bash circleci

我想做些什么

commands:
  send-slack:
    parameters:
      condition:
        type: env_var_name
    steps:
      - when:
          # only send if it's true
          condition: << parameters.condition >>
          steps:
            - run: # do some stuff if it's true
jobs:
  deploy:
    steps:
      - run:
          name: Prepare Message
          command: |
            # Do Some stuff dynamically to figure out what message to send
            # and save it to success_message or failure_message
            echo "export success_message=true" >> $BASH_ENV
            echo "export failure_message=false" >> $BASH_ENV
      - send-slack:
          text: "yay"
          condition: success_message

      - send-slack:
          text: "nay"
          condition: failure_message
    ```

1 个答案:

答案 0 :(得分:0)

基于this文档,您不能将环境变量用作CircleCI中的条件。这是因为when逻辑是在处理配置时(即,在作业实际运行和设置环境变量之前)完成的。作为替代方案,我会将逻辑添加到单独的运行步骤(或相同的初始步骤)中。

jobs:
  deploy:
    steps:
      - run:
          name: Prepare Message
          command: |
            # Do Some stuff dynamically to figure out what message to send
            # and save it to success_message or failure_message
            echo "export success_message=true" >> $BASH_ENV
            echo "export failure_message=false" >> $BASH_ENV
      - run:
          name: Send Message
          command: |
            if $success_message; then 
                # Send success message
            fi 
            if $failure_message; then 
                # Send failure message
            fi 


Here是CircleCI讨论板上的相关故障单。