can I populate variables from an exec action?
# 🤖|ask-kapa
s
@microscopic-river-74022 can I populate variables from an exec action?
m
Please stand by @swift-island-27000 as I search the knowledge base. I normally take about 20 seconds or so. :wave:
---------------------- Yes, you can populate variables from an
exec
action in Garden. You can specify a
variables
field and reference those in the
exec
and
gardenCommand
fields using
${var.*}
, similar to action variables. These variables are resolved before any other parts of the action configuration and take precedence over group-scoped variables (if applicable) and project-scoped variables, in that order. They may reference group-scoped and project-scoped variables, and generally can use any template strings normally allowed when resolving the action. However, please note that project variables are not available, since the Garden project is not resolved ahead of resolving the custom command. Here is an example of how you can use variables in an
exec
action:
Copy code
yaml
kind: Run
type: exec
name: prep-run
spec:
  command: [echo, "my run output"]
---
kind: Deploy
name: my-deploy
dependencies: [run.prep-run]
spec:
  env:
    PREP_TASK_OUTPUT: ${actions.run.prep-run.outputs.log}  # <- resolves to "my task output"
In this example, the output from
prep-run
is copied to an environment variable for
my-deploy
. Note that you currently need to explicitly declare
prep-run
as a dependency for this to work.
For more details, you can refer to the following documentation: - Using variables in custom commands () - Variables in exec Test () - Variables in exec Build () - Variables in exec Run () - Variables in exec Deploy () - Action Runtime outputs () ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
message has been deleted
s
@microscopic-river-74022 I want to parse a file and populate variables like replicas into a deployment manifest.
m
Let me check the knowledge base for you. One moment! :books:
---------------------- Yes, you can populate variables like
replicas
into a deployment manifest in Garden. You can use Garden's template strings to reference variables in your Kubernetes manifests. Here's an example of how you can set the
replicas
in your manifest:
Copy code
yaml
# In manifests/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: ${var.appName}
  labels:
    app: ${var.appName}
spec:
  replicas: ${var.replicas}
In this example,
${var.replicas}
will be replaced with the value of the
replicas
variable. You can also use the
jsonEncode
templating function to set non-primitive values like maps and arrays. Here's an example:
Copy code
yaml
# In manifests/deployment.yaml
kind: Deployment
spec:
  replicas: ${var.replicas}
  template:
    spec:
      containers:
        - name: ${var.appName}
          args: ${jsonEncode(var.containerArgs)} # <--- We need to use the jsonEncode helper function when templating non-primitive values
          env: ${jsonEncode(var.env)}
In this example,
${jsonEncode(var.containerArgs)}
and
${jsonEncode(var.env)}
will be replaced with the JSON-encoded values of the
containerArgs
and
env
variables, respectively. You can also use conditional expressions and arithmetic operators in your template strings. For example, you can set the number of replicas depending on the environment:
Copy code
yaml
kind: Deploy
type: container
spec:
  replicas: ${environment.name == 'dev' ? 1 : 3} # <-- Set replicas depending on environment
In this example, if the
environment.name
is
dev
, then
replicas
will be set to
1
. Otherwise, it will be set to
3
.
For more information, you can refer to the following documentation: - Variables and Templating in Garden () - Deploying a Kubernetes Manifest with Garden () - Using Garden Template Strings in Kubernetes Manifests () ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
message has been deleted