Hey one question my friend; when using variables ...
# 🤖|ask-kapa
b
Hey @microscopic-river-74022 one question my friend; when using variables interpolation syntax in my projet.garden.yml file I see:
Copy code
Could not find key var. Available keys: command, datetime, git, local, project and secrets.
I'm basically trying to build a variable that contains other 3 like:
Copy code
POSTGRES_SCHEMALESS_CONNECTION_STRING: postgres://${var.POSTGRES_USERNAME}:${var.POSTGRES_PASSWORD}@postgres-edge-api-cc:5432/${var.POSTGRES_DATABASE}
m
Please stand by @bright-policeman-43626 as I search the knowledge base. I normally take about 20 seconds or so. :wave:
---------------------- In Garden, you can use the
${var.*}
syntax to reference variables in your configuration files. However, the error message you're seeing suggests that the keys you're trying to reference (
POSTGRES_USERNAME
,
POSTGRES_PASSWORD
,
POSTGRES_DATABASE
) are not available. The available keys for variable interpolation in Garden are context-dependent. For example, in the Project configuration context, you have access to keys like
local.*
,
project.*
, etc. You can find more details about the available keys in different contexts in the Template Strings Reference (). In your case, it seems like you're trying to use project variables. You can define these in the
variables
field in your project configuration, or in a varfile. Here's an example of how you might define these variables in your
project.garden.yml
file:
Copy code
yaml
kind: Project
name: my-project
variables:
  POSTGRES_USERNAME: my-username
  POSTGRES_PASSWORD: my-password
  POSTGRES_DATABASE: my-database
Once these variables are defined, you should be able to reference them in your
POSTGRES_SCHEMALESS_CONNECTION_STRING
variable as you're trying to do. If you're still having trouble, make sure that the variables are defined in the correct context and that there are no typos in the variable names. If the variables are defined in a different context (e.g., a specific module or environment), they may not be available in the project context. Also, note that the order of precedence for variables is as follows (from highest to lowest):
1. Individual variables set with
--var
CLI flags. 2. The module/action-level varfile (if configured). 3. Module/action variables set in
module.variables
. 4. The environment-specific varfile (defaults to
garden.<env-name>.env
). 5. The environment-specific variables set in
environment[].variables
. 6. Configured project-wide varfile (defaults to
garden.env
). 7. The project-wide
variables
field. You can find more information about using variables in Garden in the Variables and Templating guide (). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
message has been deleted
b
@microscopic-river-74022 when using
apiVersion: garden.io/v0
can I use a combination of actions and modules?
m
Let me check the knowledge base for you. One moment! :books:
---------------------- Yes, you can use a combination of actions and modules in the same project when using
apiVersion: garden.io/v0
. This is designed to make it easier to convert projects piece by piece from modules to actions. Internally, Garden converts modules into actions as follows: - The build step of a module (if any) becomes a Build action. - Services become Deploy actions. - Tests become Test actions. - Tasks become Run actions. This means that converting your project to the actions config can be performed gradually by starting from the end of the dependency tree. However, there are some caveats: - Modules cannot depend on actions - Modules cannot reference actions - Actions can reference and depend on modules, by referencing the actions that are generated from modules. You can find more information about this in the Migrating to Bonsai guide (). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
message has been deleted
b
@microscopic-river-74022 that is nice; however: I get the following:
Copy code
"apiVersion" is missing in the Project config. Assuming "garden.io/v0" for backwards compatibility with 0.12. The "apiVersion"-field is mandatory when using the new action Kind-configs. A detailed migration guide is available at https://docs.garden.io/guides/migrating-to-bonsai
Action kinds are only supported in project configurations with "apiVersion: garden.io/v1". A detailed migration guide is available at https://docs.garden.io/guides/migrating-to-bonsai
m
Let me check the knowledge base for you. One moment! :books:
---------------------- The error message you're seeing is indicating that the
apiVersion
field is missing in your Project configuration. This field is mandatory when using the new action Kind-configs. In Garden Bonsai (0.13), the
apiVersion
field in your project configuration (
kind: Project
) should be set to
garden.io/v1
. This is a change from Garden Acorn (0.12), where the
apiVersion
field defaulted to
garden.io/v0
. Here's an example of how you might update your project configuration:
Copy code
yaml
apiVersion: garden.io/v1
kind: Project
name: my-project
# rest of your configuration...
Once you've updated the
apiVersion
to
garden.io/v1
, you should be able to use action kinds in your project configuration. However, note that once you start using
apiVersion: garden.io/v1
, team members can no longer use Acorn (
0.12
) as it does not recognize
apiVersion: garden.io/v1
. Therefore team members are forced to update to Bonsai (
0.13
). For more information on migrating to Bonsai and using the new action Kind-configs, you can refer to the Migrating to Bonsai guide (). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
message has been deleted
b
Can you now convert this module to action, in a similar fashion of what you did with the redis one my boi @microscopic-river-74022 ?
Copy code
kind: Module
description: Postgres container for storing db
type: container
name: postgres
image: postgres:13-alpine
include: [./modules.garden.yml]
services:
  - name: postgres
    volumes:
      - name: data
        containerPath: /db-data
    ports:
      - name: postgres
        containerPort: 5432
    env:
      POSTGRES_DATABASE: ${var.POSTGRES_DATABASE}
      POSTGRES_USERNAME: ${var.POSTGRES_USERNAME}
      POSTGRES_PASSWORD: ${var.POSTGRES_PASSWORD}
    healthCheck:
      command: [psql, -w, -U, "${var.POSTGRES_USERNAME}", -d, "${var.POSTGRES_DATABASE}", -c, "SELECT 1"]
m
Let me check the knowledge base for you. One moment! :books:
---------------------- Sure, here's how you might convert your Postgres module to actions:
Copy code
yaml
# Build action for the Postgres container
---
apiVersion: garden.io/v1
kind: Build
type: container
name: postgres
description: Postgres container for storing db
image: postgres:13-alpine
include: [./modules.garden.yml]

# Deploy action for the Postgres service
---
apiVersion: garden.io/v1
kind: Deploy
type: container
name: postgres
description: Postgres service for storing db
build: postgres
spec:
  volumes:
    - name: data
      containerPath: /db-data
  ports:
    - name: postgres
      containerPort: 5432
  env:
    POSTGRES_DATABASE: ${var.POSTGRES_DATABASE}
    POSTGRES_USERNAME: ${var.POSTGRES_USERNAME}
    POSTGRES_PASSWORD: ${var.POSTGRES_PASSWORD}
  healthCheck:
    command: [psql, -w, -U, "${var.POSTGRES_USERNAME}", -d, "${var.POSTGRES_DATABASE}", -c, "SELECT 1"]
In this conversion: - The
kind: Module
configuration is split into two separate actions: a
kind: Build
action for building the Postgres container, and a
kind: Deploy
action for deploying the Postgres service. - The
services
field from the module configuration is moved to the
spec
field in the
kind: Deploy
action. - The
image
field from the module configuration is moved to the
kind: Build
action. - The
apiVersion
field is added to both actions, with the value
garden.io/v1
. Remember to replace
${var.POSTGRES_DATABASE}
,
${var.POSTGRES_USERNAME}
, and
${var.POSTGRES_PASSWORD}
with the actual values or define them in the
variables
field in your project configuration.
For more information on converting modules to actions, you can refer to the Migrating to Bonsai guide (). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
message has been deleted