Is it possible to use Logic statements when setting parameters in jobs?

Is it possible to use logic statements when setting a parameter in a step of a job definition? Like this:

parameters:
  pipeline-modified:
    type: boolean
    default: false
  extranet-portal-modified:
    type: boolean
    default: false

commands:
  tag_deployment_head_on_master:
    parameters:
      tag_name:
        description: Name of the tag
        type: string
      project_modified:
        description: A boolean parameter indicating if the project needs to be tagged
        type: boolean
    steps:
      ...

jobs:
  set_deployment_tags:
    steps:
      - checkout
      - tag_deployment_head_on_master:
          tag_name: head-extranet-portal-build
          project_modified:
            or: [<< pipeline.parameters.pipeline-modified >>, << pipeline.parameters.extranet-portal-modified >>]

The parameter project_modified is calculated as an or of two pipeline parameters of type boolean

Hi @robmosca-travelperk – great question!

We don’t have the ability to evaluate the logic statement with how you set up your example. Technically I think you could utilize some bash logic to work out the conditional logic there and just pass along the value. However, there is another approach with just using a step-level conditional logic and only executing the tag_deployment_head_on_master command if it’s true. Sample:

    steps:
      - checkout
      - when:
          condition:
            or:
              - equal: [ true, << pipeline.parameters.pipeline-modified >> ]
              - equal: [ true, << pipeline.parameters.extranet-portal-modified >> ]
          steps:
            - tag_deployment_head_on_master:
                tag_name: head-extranet-portal-build

Here is a full config example, if you execute the following in a job without modifying either of the pipeline parameters it will not execute the tag_deployment_head_on_master command:

version: 2.1

parameters:
  pipeline-modified:
    type: boolean
    default: false
  extranet-portal-modified:
    type: boolean
    default: false

commands:
  tag_deployment_head_on_master:
    parameters:
      tag_name:
        description: Name of the tag
        type: string
        default: ''
      project_modified:
        description: A boolean parameter indicating if the project needs to be tagged
        type: boolean
        default: false
    steps:
      - run: echo "hi"

jobs:
  set_deployment_tags:
    docker:
      - image: cimg/base:stable
    steps:
      - checkout
      - when:
          condition:
            or:
              - equal: [ true, << pipeline.parameters.pipeline-modified >> ]
              - equal: [ true, << pipeline.parameters.extranet-portal-modified >> ]
          steps:
            - tag_deployment_head_on_master:
                tag_name: head-extranet-portal-build

workflows:
  build-logic:
    jobs:
      - set_deployment_tags

However, if you trigger via the API and set the parameters or simply set one to true in the config, you’ll find the command is executed. Would the above work for you?

-Nick

Thanks Nick, this is actually really useful. I think I will go for evaluating the boolean condition in the bash script, but the approach using the when clause is also good and something I didn’t think about.

Thanks!

1 Like