How to retrieve variable in second task from first task using bash script?

Hi there, I am new to stackstorm. I need help to resolve the problem, I am having.

I am working on one workflow that will get variable information from first task and use that variable in second task using bash script. I see that is possible using python, but I would prefer to do with bash. Here is the workflow.

input:
  - value1
  - value2
  - value3

output:
  - value2: <% ctx().value2 %>
  - value3: <% ctx().value3 %>

tasks:
  task1:
    action: jobname.get_info value1=<% ctx(value1) %>
    next:
      - when: <% succeeded() %>
        publish:
          - stdout: <% result().stdout %>
          - stderr: <% result().stderr %>
          - value2: <% ctx("value2") %>, <% result().result %>
          - value3: <% ctx("value3") %>, <% result().result %>
      - do: task2
      - when: <% failed() %>
        publish: stderr=<% result().stderr %>
        do: fail

  task2:
    action: job.name2 value2=<% ctx(value2) %>
    next:
      - when: <% succeeded() %>
        publish:
          - stdout: <% result().stdout %>
          - stderr: <% result().stderr %>
      - do: task3
      - when: <% failed() %>
        publish: stderr=<% result().stderr %>
        do: fail

I see that the first job gets succeeded without any error, but the second job uses value2 in the execution, so it won’t move forward in execution and errors out.

{
  "output": null,
  "errors": [
    {
      "message": "YaqlEvaluationException: Unable to resolve key 'result' in expression '<% result().result %>' from context.",
      "task_transition_id": "noop__t0",
      "type": "error",
      "route": 0,
      "task_id": "task1"
    },
    {
      "message": "YaqlEvaluationException: Unable to evaluate expression '<% ctx(stdout) %>'. VariableUndefinedError: The variable \"stdout\" is undefined.",
      "type": "error"
    }
  ]
}

Thanks in advance!! :slight_smile:

You have a bug in your YAML. The task1.next block looks like this (represented in JSON) to make it more apparent:

[
  {
    "when": ...,
    "publish": ...
  },
  {
    "do": ...
  },
  {
    "when": ...,
    "publish": ...,
    "do": ...
  }
]

The dictionary for the do item does not have a when key, and the when item does not have a do key.

That is actually valid Orquesta, but it’s likely not what you want.

What that does is publish some values to the workflow context (when task1 is successful). If task1 fails, it transitions to the builtin fail task. And because the do block doesn’t have a when key, Orquesta will always try to transition to task2 regardless of whether task1 succeeds or fails - that’s not what you want.

I think you want the workflow to publish stdout, stderr, value2, and value3 to the workflow context and to transition to task2 only when task1 succeeds.

So to do that, you want to do the following (represented in JSON):

[
  {
    "when": ...,
    "publish": ...,
    "do": ...
  },
  {
    "when": ...,
    "publish": ...,
    "do": ...
  }
]

Or to represent that in YAML:

      - when: <% succeeded() %>
        publish:
          - stdout: <% result().stdout %>
          - stderr: <% result().stderr %>
          - value2: <% ctx("value2") %>, <% result().result %>
          - value3: <% ctx("value3") %>, <% result().result %>
        do: task2
      # ^ no longer a list item on its own, it's now in the
      #   same list item as the `when` and `publish` keys
      - when: <% failed() %>
        publish: stderr=<% result().stderr %>
        do: fail