Dynamic parameters and conditional steps

Hi circleci team!
I’m trying to have some steps of a job being conditionally executed depending on whether an entry exists in the cache or not. My idea was to use the recently added dynamic parameters.

Based on this thread, I tried the following:

my-job:
  executor: my-executor
  steps:
    - initial_setup_step
    - restore_cache:
        keys:
          - myCacheKey
        name: Trying to restore cache
    - run:
        name: Check whether an entry was found in the cache
        command: |
          COMMAND_RESULT=$(some command here)
          if [ -z "COMMAND_RESULT" ]; then
             echo "export BUILD_NEEDED=\"true\"" >> $BASH_ENV
          else
             echo "export BUILD_NEEDED=\"false\"" >> $BASH_ENV
          fi
    - parameters:
        build_needed:
          type: string
          default: "echo ${BUILD_NEEDED}"
    - when:
        condition:
          equal: [ "true", << parameters.build_needed >> ]
        steps:
          - run:
              name: Build
              command: |
                ...
    - other_step
         ...

The config checker complains:

Error calling job: 'my-job'
Arguments referenced without declared parameters: build_needed

Did I miss something obvious?

Thanks!

Rafael

I think you have missread that message thread and combined what the poster asked for and the replies saying what is possible. This part is not valid

    - parameters:
        build_needed:
          type: string
          default: "echo ${BUILD_NEEDED}"

As noted in the second post - “Environment variables such as CIRCLE_BRANCH are not available when your config.yml file is compiled, and as such you won’t be able to use them directly as a parameter”

What you can do is restructure you script so that it is broken down into commands which in just about every other language would be called a function. These express their inputs as parameters and the value of an environment variable can be passed to them.

Indeed, thanks!

I need to stick a large qualifier on that last statement - you do not pass the value of the environment variable to the parameter, but instead the name of the environment variable. This allows you to select which variable can be used in the called function, but still with the limitations of the circleci language where parameters and env’s can not be mixed.

This limitation gets even more complicated as it results in strange results when env variables encapsulated in parameters are expanded in a run command.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.