st2 icon indicating copy to clipboard operation
st2 copied to clipboard

Environment variables from file in Actions

Open jesvilnie opened this issue 10 months ago • 2 comments

Having the possibility to define environment variables in the Actions is crucial in most situations, but it would be really nice to have the possibility to specify these variables from a file, so they can be reused across multiple actions without the need of replicate the list of env vars in each action.

It would be something like this:

---
name: "my_script"
runner_type: "remote-shell-cmd"
description: "Remote CMD with envs form file"
enabled: true
env_file: "/path/to/my/env/file"

Or as a parameter:

---
name: "my_script"
runner_type: "remote-shell-cmd"
description: "Remote CMD with envs form file"
enabled: true
parameters:
    env_file: 
       type: "string"
       default: "/path/to/my/env/file"

jesvilnie avatar Apr 05 '24 09:04 jesvilnie

I think we can write an application package, put the environment variable configuration in the package configuration, and then use this configuration for all the actions in this package.

chain312 avatar Apr 07 '24 12:04 chain312

A workaround to get this functionality would be to provide a json file with the environment variables that is read in an action prior to calling core.remote and then pass the contents via the env parameter.

For example (not tested)

/path/to/action_env.json

{
  "username": "user1",
  "url": "https://example.com/",
  "project_id": "abcd2134"
}

Load the environment file. StackStorm will automatically deserialise JSON content from stdout. The deserialised JSON can be published for use in subsequent actions.

  load_env:
    action: core.local
    input:
      - cmd: cat /path/to/action_env.json
    next:
      - do: run_remote_cmd
        when: <% succeeded() %>
        publish:
          env_kw: <%  result().stdout %>

  run_remote_cmd:
    action: core.remote
    input:
        hosts: somehost.example.com
        env: <% ctx().env_kw %>
        cmd: "echo $USERNAME $URL $PROJECT_ID"

nzlosh avatar May 02 '24 10:05 nzlosh