how can i set a variable only when i run a "garden...
# 🤖|ask-kapa
g
@microscopic-river-74022 how can i set a variable only when i run a "garden test" script with garden >13?
m
Please stand by @gentle-umbrella-55872 as I search the knowledge base. I normally take about 20 seconds or so. :wave:
---------------------- In Garden, you can use the
${command.name}
template string to access the currently running Garden CLI command. This can be used to change some variables based on whether you're running
garden test
or some other specific command. Here's an example of how you might use it:
Copy code
yaml
my-variable: ${command.name == 'test' ? 'value when test' : 'other value'}
In this example,
my-variable
will be set to
'value when test'
when you're running
garden test
, and
'other value'
otherwise. Please note that this will currently always resolve to
"workflow"
when running Workflows, as opposed to individual workflow step commands. This may be revisited at a later time, but currently all configuration is resolved once for all workflow steps. For more information, you can refer to the Garden documentation on Project template context (), Environment template context (), and Project Output template context (). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
message has been deleted
g
@microscopic-river-74022 how can i print all current variables (command, this, …)
m
Let me check the knowledge base for you. One moment! :books:
---------------------- In Garden, you can use various template strings to access different types of variables. Here are some examples: -
${{command.*}}
to access information about the currently running command and its arguments. Reference () -
${{this.*}}
to access information about the current context (e.g., the current module, test, task, etc.). -
${{variables.*}}
to access a map of all variables defined in the project configuration, including environment-specific variables. Reference () -
${{modules.<module-name>.var.*}}
to access a map of all variables defined in a specific module. Reference () To print all these variables, you can use the
garden get config
command, which will output the configuration for your project, including all the variables. Here's an example:
Copy code
bash
garden get config
This will print the configuration in the console. If you want to print it to a file, you can redirect the output like this:
Copy code
bash
garden get config > config.txt
This will create a
config.txt
file with the configuration. Please note that the actual command to print the configuration may vary depending on your specific setup and the Garden version you're using. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
message has been deleted
g
@microscopic-river-74022 and how get i the variables which are available when i run "garden test"?
m
Let me check the knowledge base for you. One moment! :books:
---------------------- When you run a
garden test
command, the variables that are available are the ones defined in your project configuration, and any environment-specific variables. You can also use template strings to access information about the currently running command and its arguments, the current context, and more. Here's an example of how you might define variables in your project configuration:
Copy code
yaml
apiVersion: garden.io/v1
kind: Project
name: my-project
variables:
  # This variable is referenced in the action configs, and overridden in the local environment below
  service-replicas: 3
environments:
  - name: local
    variables:
      # We only want one replica of each service when developing locally
      service-replicas: 1
  - name: staging
providers:
  - name: local-kubernetes
    environments: ["local"]
  - name: kubernetes
    environments: ["staging"]
    ...
In this example, the
service-replicas
variable is defined at the project level and can be accessed in all the project's action configurations. It's also overridden in the
local
environment. To access these variables in your tests, you can use the
${var.*}
syntax. For example,
${var.service-replicas}
would access the
service-replicas
variable. To see the current configuration, including all variables, when running a
garden test
command, you can use the
garden get config
command. This will output the configuration for your project, including all the variables.
For more information, you can refer to the Garden documentation on Project template context (), Environment template context (), and Project Output template context (). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
message has been deleted
g
@microscopic-river-74022 and how print i the variables which are available when i run "garden test"?
m
Let me check the knowledge base for you. One moment! :books:
---------------------- To print the variables available when you run
garden test
, you can use the
garden get config
command. This command will output the configuration for your project, including all the variables. Here's an example:
Copy code
bash
garden get config
This will print the configuration in the console. If you want to print it to a file, you can redirect the output like this:
Copy code
bash
garden get config > config.txt
This will create a
config.txt
file with the configuration. Please note that the actual command to print the configuration may vary depending on your specific setup and the Garden version you're using. For more information, you can refer to the Garden documentation on Project template context (), Environment template context (), and Project Output template context (). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
message has been deleted