How to use variables in forge?

I want to use variables in manifest.yaml file.
I have added below code in manifest.yaml

environment:
  variables:
    - appKey
    - connectBaseUrl
.....
remotes:
  - key: connect
    baseUrl: ${connectBaseUrl}
......
app:
  id: ari:cloud:ecosystem::app/xxxxxxxxxx
  connect:
    key: ${appKey}
    remote: connect

I am using --environment shivtest

I am setting up variables

forge variables set appKey xxxxx.xxxx
forge variables set connectBaseUrl https://xxxxxxxx.com

when i try to use

forge lint --environment shivtest

Gives below error:
0:0 error manifest.yml failed to parse content - could not find environment variable ‘appKey’ valid-yaml-required
Error: Command failed due to validation error.
Rerunning the command with --verbose may give more details.

Is this a correct way of doing thing?

I wanted to set different keys and connect urls for different environment.

Initially i tried using placeholder and then replacing using custom deploy.js script, however forge lint gives error in that case as well.

Hi @shiv ,

My guess is that your variables should be set in your shell environment when you run forge lint.

F.

I’d try it as with tunnel: https://developer.atlassian.com/platform/forge/environments-and-versions/#forge-tunnel

previously I tried

set appKey = xxxxx.xxxx
set connectBaseUrl = https://xxxxxxxx.com

in windows command prompt.
It’s not working. however now when I removed the spaces set connectBaseUrl=https://xxxxxxxx.com
It’s working now.. Thanks

I’m in the process of migrating a Connect app to Connect-on-Forge, and I have a question about using variables in manifest.yml.

As per the OP, I want to use a variable for remotes.baseUrl, so I currently have this in my manifest:

remotes:
  - key: connect
    baseUrl: ${BASE_URL}
    operations:
      - compute
environment:
  variables:
    - BASE_URL

…and I have the variable set in my shell when executing forge CLI commands, e.g.

BASE_URL=https://example.com forge lint

So far, so good.

However looking at the reference documentation for variables, I noticed this section:

Aspect Manifest Variables (This Page) Runtime Variables
When available During CLI commands (build-time) App execution (runtime)
How to set export VAR=value in terminal forge variables set VAR value
How to access ${VAR} in manifest.yml process.env.VAR in app code
Use cases App IDs, module configuration API tokens, secrets, runtime config

According to that, it would seem(?) that setting variables using forge variables set VAR value is only for runtime use (e.g. accessed via process.env in the app code) and shouldn’t be needed for manifest variables?

So my question is:

When I run forge deploy to deploy my app to different environments (and later, forge install to install the app), do I need to have these variables set in the environment with forge variables set …?

Edit: Also just adding to this….if I remove the environment key from my manifest and run forge lint, it doesn’t complain. Is the following statement from the reference docs still true?

An environment variable must first be declared in environment.variables before it can be used elsewhere in the manifest file:

do I need to have these variables set in the environment with forge variables set …

No, that’s a separate mechanism for setting variables to be accessed by the app code at runtime rather than interpolated into the manifest - e.g. via process.env.

Is the following statement from the reference docs still true?

Yes. Without declaring the environment variable it won’t be interpolated, the variable reference will be treated as a literal value - i.e. your base url will be set to “${BASE_URL}”