# Setting cache directory for save\_cache conditionally

**URL:** <https://discuss.circleci.com/t/setting-cache-directory-for-save-cache-conditionally/49030>\
**Category:** Build Environment\
**Created:** [August 23, 2023, 3:48pm UTC](https://discuss.circleci.com/t/setting-cache-directory-for-save-cache-conditionally/49030 "2023-08-23T15:48:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wyardley](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/wyardley/32/19231_2.png) [@wyardley](https://discuss.circleci.com/u/wyardley)\
**Post date:** [August 23, 2023, 3:48pm UTC](https://discuss.circleci.com/t/setting-cache-directory-for-save-cache-conditionally/49030/1 "2023-08-23T15:48:39Z")

</div>

I’ve got an orb where I’m trying to support enabling

I’m pretty sure I can’t suppress the entire block using mustache templating, right?

I was trying to use `~` or `null` in the case where the parameter isn’t set, but in that case, it still tries to add `/home/circleci` or `/home/circleci/null` to the saved cache.

```yaml
  - save_cache:
      key: npm-<< parameters.cache_version >>-{{ .Branch }}-{{ checksum "<< parameters.cache_key >>" }}
      paths:
        - <<#parameters.foo_cache>>~/.cache/foo/<</parameters.foo_cache>><<^parameters.foo_cache>>~<</parameters.foo_cache>>
        - << parameters.cache_path >>

```

I tried suppressing the entire block (by putting the `-` inside the `<<#...` bit) just to see if it would work, and while it didn’t throw validation errors, it also didn’t do what I would want / expect. And I think `when:` and other conditions only work for, e.g., steps, right?

There’s also no way to make a list parameter, right (for passing into `paths:`)?

---

<div class="post-metadata">

**Author:** ![rit1010](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/rit1010/32/34738_2.png) [@rit1010](https://discuss.circleci.com/u/rit1010)\
**Post date:** [August 23, 2023, 5:04pm UTC](https://discuss.circleci.com/t/setting-cache-directory-for-save-cache-conditionally/49030/2 "2023-08-23T17:04:37Z")

</div>

I think that it is still the case that past posts from you are still about the only place that mustache templating gets raised in the forums and CirclCI staff have not touched upon the feature much beyond the following tech note

> **[Using Mustache Conditionals in Config File](https://support.circleci.com/hc/en-us/articles/4417604103835-Using-Mustache-Conditionals-in-Config-File)**
>
> Conditionally Set Values
> When using parameters and pipeline values in your config.yml file, it is possible to conditionally set a value. 
> To check if a value is true or a non-empty string:
> \<\<...

The real unknown is where within the config.yml file processing path is the Mustache Conditionals step performed as the circleci DSL has 2 forms of parameter - those fully defined/known at the start (signified by having a name starting with “pipeline.” or “pipeline.parameters.” and those only fully defined during execution (signified by having a name starting with “parameters”).

I can only guess, but much of the processing of the config.yml treats these 2 forms of parameter very differently with certain processing only performed at the time that config.yml is first loaded. Mustache Conditional processing is likely to be performed only at this stage.

This issue of parameter types is also likely to be limiting your ability to use a parameter as a list for paths: such a configuration may work if you were using a pipeline or pipeline.parameters type.

---

<div class="post-metadata">

**Author:** ![Henna](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/henna/32/29402_2.png) [@Henna](https://discuss.circleci.com/u/Henna)\
**Post date:** [August 23, 2023, 9:10pm UTC](https://discuss.circleci.com/t/setting-cache-directory-for-save-cache-conditionally/49030/3 "2023-08-23T21:10:47Z")

</div>

Hi @wyardley ,

You can achieve this by using a [conditional step](https://circleci.com/docs/reusing-config/#defining-conditional-steps) with [logic statements](https://circleci.com/docs/configuration-reference/#logic-statement-examples).

I hope that’s useful information. Let me know if any questions come up!

---

<div class="post-metadata">

**Author:** ![wyardley](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/wyardley/32/19231_2.png) [@wyardley](https://discuss.circleci.com/u/wyardley)\
**Post date:** [August 25, 2023, 2:36am UTC](https://discuss.circleci.com/t/setting-cache-directory-for-save-cache-conditionally/49030/4 "2023-08-25T02:36:54Z")

</div>

> [@Henna](#):
>
> You can achieve this by using a [conditional step](https://circleci.com/docs/reusing-config/#defining-conditional-steps) with [logic statements](https://circleci.com/docs/configuration-reference/#logic-statement-examples).

@Henna thanks  
I considered that (mentioned above), but I don’t think I can use that in this context - let me know if I’m wrong there.

---

<div class="post-metadata">

**Author:** ![wyardley](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/wyardley/32/19231_2.png) [@wyardley](https://discuss.circleci.com/u/wyardley)\
**Post date:** [August 30, 2023, 5:45pm UTC](https://discuss.circleci.com/t/setting-cache-directory-for-save-cache-conditionally/49030/5 "2023-08-30T17:45:41Z")

</div>

I realized last night that in theory, it could be possible to do using steps conditionally, but you’d have to do it in a much messier / less terse way. Something like (untested)

```yaml
steps:
  - when:
      condition: << parameters.foo_cache >>
      steps:
        save_cache:
          key: npm-<< parameters.cache_version >>-{{ .Branch }}-{{ checksum "<< parameters.cache_key >>" }}
          paths:
            - ~/.cache/foo/
            - << parameters.cache_path >>
  - unless:
      condition: << parameters.foo_cache >>
      steps:
        save_cache:
          key: npm-<< parameters.cache_version >>-{{ .Branch }}-{{ checksum "<< parameters.cache_key >>" }}
          paths:
            - << parameters.cache_path >>

```
