how can I correctly compare an environment variabl...
# 🤖|ask-kapa
m
Please stand by @flat-state-47578 as I search the knowledge base. I normally take about 20 seconds or so. :wave:
---------------------- Based on the provided extracts, Garden does not seem to support conditional logic directly within the template strings. Therefore, you cannot directly compare an environment variable to a static value within a template string to change the value of the build attribute of a deploy action. However, you can define environment-specific variables using the
environment[].variables
key in your project configuration. This allows you to provide different values for different environments. Here's an example:
Copy code
yaml
kind: Project
variables: 
  log-level: info
environments: 
  - name: local 
    variables: 
      log-level: debug 
  - name: remote
In this example, the
log-level
variable will have the value
debug
in the
local
environment and
info
in the
remote
environment. You can then reference these variables in your actions using the
${var.<key>}
syntax. For more complex conditional logic, you might need to handle that in your application code or in the scripts that you run as part of your build or deploy actions. For more information, you can refer to the Garden documentation on variables and templating (). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
f
@microscopic-river-74022 this page suggests that ternaries in template strings are possible: https://docs.garden.io/misc/faq#how-do-i-use-different-dockerfiles-for-different-environments
m
Let me check the knowledge base for you. One moment! :books:
---------------------- I apologize for the confusion. You're correct, Garden does support ternary expressions in template strings. You can use them to conditionally set values based on other variables. Here's an example:
Copy code
yaml
kind: Deploy
type: container
spec: 
  replicas: "${environment.name == 'prod' ? 3 : 1}"
In this example, the
replicas
attribute of the deploy action will be set to
3
if the
environment.name
is
prod
, and
1
otherwise. You can use a similar approach to conditionally set the
build
attribute of a deploy action based on an environment variable. For example:
Copy code
yaml
kind: Deploy
type: container
spec: 
  build: "${environment.name == 'prod' ? 'build-prod' : 'build-dev'}"
In this example, the
build
attribute will be set to
'build-prod'
if the
environment.name
is
prod
, and
'build-dev'
otherwise. For more information, you can refer to the Garden documentation on variables and templating (). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
f
@microscopic-river-74022 how could I ensure that
build
is left undefined depending on the environment variable? E.g.:
Copy code
build: "${local.env.GARDEN_DEV_SERVICE == "my-service" ? 'my-service' : null }"
m
Let me check the knowledge base for you. One moment! :books:
---------------------- In Garden, you can use ternary expressions in template strings to conditionally set values based on environment variables. However, setting a value to
null
might not be the best approach as it could lead to unexpected behavior or errors. If you want to conditionally disable the build process for a specific service, you could use the
disabled
field instead. Here's an example:
Copy code
yaml
kind: Build
name: my-service
type: container
disabled: "${local.env.GARDEN_DEV_SERVICE != 'my-service'}"
In this example, the build process for
my-service
will be disabled if
GARDEN_DEV_SERVICE
is not equal to
'my-service'
. For more information, you can refer to the Garden documentation on variables and templating (). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------