swift-island-27000
10/03/2024, 10:21 AMmicroscopic-river-74022
10/03/2024, 10:21 AMmicroscopic-river-74022
10/03/2024, 10:21 AMexec
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:
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.microscopic-river-74022
10/03/2024, 10:21 AMmicroscopic-river-74022
10/03/2024, 10:21 AMswift-island-27000
10/03/2024, 10:27 AMmicroscopic-river-74022
10/03/2024, 10:27 AMmicroscopic-river-74022
10/03/2024, 10:27 AMreplicas
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:
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:
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:
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
.microscopic-river-74022
10/03/2024, 10:27 AMmicroscopic-river-74022
10/03/2024, 10:27 AM