k1LoW / runn

runn is a package/tool for running operations following a scenario.
https://runn.run
MIT License
417 stars 30 forks source link

Unable to Use `vars` in `runners.endpoint` #932

Closed souchan2000 closed 2 weeks ago

souchan2000 commented 3 months ago

I've run into an issue when trying to use vars in the endpoint of runners in the following script: env/local.json

{
  "endpoint": "http://localhost:3000/api"
}

env/production.json

{
  "endpoint": "https://example.com/api"
}

example.yml

desc: example
vars:
  env: 'json://env/${ENVIRONMENT}.json'
runners:
  service:
    endpoint: '{{ vars.env.endpoint }}'
steps:
  - dump: vars.env.endpoint
  - service:
      /users/me:
        get:
          test: current.res.status == 200

The results obtained from execution:

$ ENVIRONMENT=local runn run example.yml --debug

Run "dump" on "example".steps[0]
http://localhost:3000/api

Run "service" on "example".steps[1]
-----START HTTP REQUEST-----
GET %7B%7B%20vars.env.endpoint%20%7D%7D/users/me HTTP/1.1

-----END HTTP REQUEST-----
F

1) example.yml 7d163e60ed7a29276077adb17f2b6c37f8239391
  Failure/Error: http request failed on "example".steps[1]: Get "%7B%7B%20vars.env.endpoint%20%7D%7D/users/me": unsupported protocol scheme ""
  Failure step (example.yml):
   9   - service:
  10       /users/me:
  11         get:
  12           test: current.res.status == 200

1 scenario, 0 skipped, 1 failure

Specifically, using '{{ vars.env.endpoint }}' directly in the endpoint of runners.service doesn't seem to work as expected.

The expected behaviour is that vars.env.endpoint is correctly expanded in service.endpoint and it becomes possible to set the endpoint dynamically based on env.

If there is a solution to this problem, or an alternative way to dynamically set the endpoint using vars, I would appreciate your advice.

k1LoW commented 3 months ago

Values in the vars: section cannot be used in the runners: section of the same runbook.

Variables can be used if they are in the runners: section of the runbook loaded by the include runner.

k1LoW commented 2 months ago

Hi @souchan2000.

From v0.113.1, runners can be defined in the middle of a step.

https://github.com/k1LoW/runn?tab=readme-ov-file#runner-runner-define-runner-in-the-middle-of-steps

souchan2000 commented 2 months ago

Sorry for the late reply, I'll try using the information you suggested. Thank you.