Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/reference/command/readstate.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,20 @@ if the result is not `null` and not an empty string, otherwise it invokes the `e
----

NOTE: `read-state` will run the `then` commands even if `${{use_https}}` is `"false"` because that is not empty and not null. Use link:./js.adoc['js'] if you want to perform boolean operations

If you need to evaluate two variables or more you can use one of the following approaches:

* use `js` which lets you write a JavaScript function that invokes the `then` if the return is truth, otherwise it invokes the else
* use `${{=` to start a JavaScript evaluation and perform the boolean operation there
e.g.

[source,yaml]
----
- read-state: ${{= ${{abort_on_failure:true}} && "${{START_JOB_RESULT:}}" !== 'SUCCESS' }}
then:
- sh: echo Failure
else:
- sh: echo Success
Comment on lines +25 to +29
Copy link
Member

@lampajr lampajr Mar 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using read-state, isn't better using js directly to evalute boolean expressions?

- js: ${{abort_on_failure:true}} && "${{START_JOB_RESULT:}}" !== 'SUCCESS' 
  then:
  - sh: echo Failure
  else:
  - sh: echo Success

I remember @willr3 mentioned this in the past, therefore I'd like to get his advice here 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that information came from him

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To get it working as you are proposing I think you are missing a regex, am I right?

- read-state: ${{= ${{abort_on_failure:true}} && "${{START_JOB_RESULT:}}" !== 'SUCCESS' }}
- regex: true
  then:
  - sh: echo Failure
  else:
  - sh: echo Success

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both work, a boolean operation probably makes more sense as a js command because it means we no longer need the regex but I think having both examples is useful. Perhaps we steer users toward js but show that it can also be accomplished in read-state?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can document how to do it with JavaScript as well. WDYT?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's a good idea

----

The expression above will return true or false based on the desired boolean expression. Note the `"` around `START_JOB_RESULT` so that the JavaScript evaluation sees it as a string literal and not the name of a variable.
Loading