Do expressions support ternary operators to change their returned value? #26738
-
Something along these lines…
|
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 5 replies
-
Hi @atrauzzi , Expressions don’t support ternary operators. You could create a feedback for this feature in the Feedback form for GitHub Actions . As a workaround , you could add another step to set prerelease as true or false before Create Release step. In my example , I use set-output command to set an output variable in the front step, then use it in Create Release step .
|
Beta Was this translation helpful? Give feedback.
-
Is a ternary operator or substitute even necessary in this case?
atrauzzi:
In other cases, the data type might not be right for you or undesired coercion might occur. In conjunction with comparison operators, GitHub will cast them to numbers ( You can cast booleans to strings explicitly should it be necessary:
I verified that the cast works like so:
Because JSON has native boolean types, Finally, I want to point out that there is a fake ternary technique which works on GitHub:
The only limitation is that the second AND condition (here: Other techniques involving arrays or objects aren’t possible because expressions in Actions do not support array and object literals (yet). |
Beta Was this translation helpful? Give feedback.
-
I stand corrected: It is possible to use array/object based techniques via
The condition evaluates to a boolean, which is implicitly cast to a number and used as array index, where |
Beta Was this translation helpful? Give feedback.
-
The fake ternary operator doesn’t work for secrets: # works
value: ${{ github.ref != 'refs/heads/master' && 'true' || 'false' }}
# doesn't work
value: ${{ github.ref != 'refs/heads/master' && secrets.ValueA || secrets.ValueB }} |
Beta Was this translation helpful? Give feedback.
-
Interesting. Are there secrets simple strings or like JSON/YAML objects? If you want to use different secrets, then using GitHub environments might be what you want. |
Beta Was this translation helpful? Give feedback.
Hi @atrauzzi ,
Expressions don’t support ternary operators. You could create a feedback for this feature in the Feedback form for GitHub Actions .
As a workaround , you could add another step to set prerelease as true or false before Create Release step. In my example , I use set-output command to set an output variable in the front step, then use it in Create Release step .
name: Create Release
id: create-release
uses: actions/create-release@v1