Hello Everybody,
Today I would like to discuss a use case from one of our customers. They have encountered an issue when evaluating ternary operator (? : ) conditions during variable declarations. We have found that this is a difficult problem to resolve.
I would like to discuss ways to address this issue and brainstorm solutions that could potentially helped us solve this problem.
Problem statement:
We want a workflow that can be used both in a a pipeline, and as a standalone workflow for manual triggers. The workflow has a wrap-up step that sends a Slack message with the name of the person that is responsible for the deployment. It is done by using both of these variables:
${pipelineApproval.approvedBy.name} (${deploymentTriggeredBy})
And the workflow/slack script step will fail if the workflow is triggered manually, because then the ${pipelineApproval.approvedBy.name} variable is not set.
(Bad substitution error).
Proposed Solution:
As a workaround, we would need to add a shell script step prior to the notification to check if the variable is present and export the variable with the approver name, so if it comes from a pipeline, it will show the approval name if not, it will show as empty.
Here is the Shell script to set the Approver:
APPROVER=""if [[ ! '${pipelineApproval.approvedBy.name}' =~ "pipelineApproval.approvedBy.name" ]]then APPROVER='${pipelineApproval.approvedBy.name}'fi
And use that:
echo "Approver: ${context.approvalData.APPROVER}"
Note: you would need to enable “Publish output in the context” with the name APPROVER to be referenced further in workflow/pipeline.