Unable to use the new 2.1 when directive

I’m trying to use the new conditional ‘when’ clause. However when I use it, I receive the following error

step type "when" is not a valid type

I’ve updated my version to 2.1 and I’m trying to use it in conjunction with a cache

version: 2.1
jobs:
  build:
    parallelism: 3
    docker:
       ...
    steps: # a collection of executable commands
       ...
      - run:
          name: Calculate Assets Checksum
          command: find app/assets -type f | sort | xargs md5sum > assets.md5

      - restore_cache:
          keys:
            - rails-fitmo-assets-{{ checksum "assets.md5" }}
            - rails-fitmo-assets-

      - when:
          condition: if [[ ! -d public/assets ]]
          steps:
            - run: echo "assets exists NOW"
            - run: bundle exec rake assets:precompile

      - save_cache:
          key: rails-fitmo-assets-{{ checksum "assets.md5" }}
          paths:
            - public/assets
            - tmp/cache/assets/sprockets


Is version 2.1  not available yet? the docs don't indicate either way so I am assuming 2.1 is available. Am I doing something wrong?

2.1 is still in beta, did you enable config processing in the advanced settings of your project?

In either case, I think this is still wrong. From this doc it looks like when is only used in the context of a parameter being set to true or false, so you probably are not able to run arbitrary bash commands to check for the condition at run time.

condition is a single value that evaluates to true or false at the time the config is processed

This happens before the build runs. The approach should be to make this check into its own step and conditionally run the bundle command. Something like this:

- run:
    name: Conditionally Bundle Exec
    command: |
        if [[ ! -d public/assets ]]; then;
            echo "asserts exists NOW"
            bundle exec rake assets:precompile
        else
            echo "no need to compile"
        fi

Hi Leviaz,

Thanks for the feedback. I’ll give what you have suggested a try.

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