How do I make a requirement be conditional?
# 🤖|ask-kapa
s
@microscopic-river-74022 How do I make a requirement be conditional?
m
Please stand by @salmon-scientist-57815 as I search the knowledge base. I normally take about 15 seconds or so. :wave:
---------------------- To make a requirement conditional in Garden, you can use template strings with conditional logic. There are a few ways to achieve this: 1. Using if/else conditional objects: You can use the
$if
,
$then
, and
$else
keys to conditionally set values. This method is often clearer and easier to work with compared to using conditional template strings. Here's an example:
Copy code
yaml
kind: Deploy
spec:
  command:
    $if: ${this.mode == "sync"}
    $then: [npm, run, watch]
    $else: [npm, start]
In this case, the
command
is set conditionally based on the mode of the action. [Using-garden > Variables and templating > Template string overview > If/else conditional objects]() 2. Using multi-line if/else blocks in strings: For more complex conditional logic, especially in multi-line strings, you can use if/else blocks. The syntax is:
Copy code
${if <expression>}<content>[${else}]<alternative content>${endif}
Here's an example:
Copy code
yaml
variables:
  some-script: |
    #!/bin/sh
    echo "Hello, I'm a bash script!"

    ${if environment.name == "dev"}
    echo "-> debug mode"
    DEBUG=true
    ${else}
    DEBUG=false
    ${endif}
This allows you to conditionally include or exclude entire blocks of content. [Using-garden > Variables and templating > Template string overview > Multi-line if/else blocks in strings]() 3. Using the
skip
option in workflow steps: If you're working with workflows, you can use the
skip
option to conditionally skip entire steps. For example:
Copy code
yaml
steps:
  - skip: "${environment.name != 'prod'}"
This will skip the step if the environment name is not 'prod'. [Reference > Workflow Configuration > Configuration Keys >
steps[].skip
]() By using these methods, you can make various aspects of your Garden configuration conditional, including requirements, based on different variables or conditions in your project. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
message has been deleted
8 Views