KBEC-00461 - How Can I Escape the $[ ] Notation?

Article ID:360052962192
2 minute readKnowledge base
On this page

Problem

As mentioned in Using property values, $[ ] is the notation to access a property in a job step. This is very convenient to access a property as described in the document. However, in some cases, you may want to defer the expansion of a property. For example, you want to find out which pipeline triggered a (finished) job. We can get this information by using a command similar to the following commands:

C:\Users\ec>ectool expandString '$[/myPipelineRuntime]' --jobId e0266225-56e6-11
ea-8a8f-662420524153
'zd179839_2_20200224172018'
C:\Users\ec>ectool expandString '$[/myPipelineRuntime/flowRuntimeId]' --jobId e0
266225-56e6-11ea-8a8f-662420524153
'df9e94c0-56e6-11ea-8409-662420524153'

But these ectool command will fail if you run them directly in a job step because the CloudBees CD server tries to expand $[/myPipelineRuntime] before running the step. So in this case, we want to defer the expansion of the property $[/myPipelineRuntime]. How?

Solution

There are various ways to defer the expansion.

We can create some properties and use these properties in the command. We expect to get the correct/expected commands after the CloudBees CD server expands these properties. For example, in this case, create the following properties in the procedure (You can create it anywhere that is convenient for you):

  • LSB with value [

  • RSB with value ]

  • abc with value [/myPipelineRuntime]

Then use these properties as below in an "ectool" command in a job step:

ectool expandString '$$[/myProcedure/LSB]/myPipelineRuntime/flowRuntimeId$[/myProcedure/RSB]' --jobId "6de9ad01-54a1-11ea-83b5-92c63c012778"
echo '$$[/myProcedure/abc]'
ectool expandString '$$[/myProcedure/abc]' --jobId "6de9ad01-54a1-11ea-83b5-92c63c012778"

and as below in a job step that expects "ec-groovy" code:

import com.electriccloud.client.groovy.ElectricFlow
import com.electriccloud.client.groovy.apis.model.*

ElectricFlow ef = new ElectricFlow()
def result = ef.expandString(
                value: '$$[/myProcedure/LSB]/myPipelineRuntime/flowRuntimeId$[/myProcedure/RSB]',
                jobId: '6de9ad01-54a1-11ea-83b5-92c63c012778')
println(result.value)
def result2 = ef.expandString(
                value: '$$[/myProcedure/abc]',
                jobId: '6de9ad01-54a1-11ea-83b5-92c63c012778')
println(result2.value)

When using groovy, another simpler way is to separate the notation. For example, we can use '$' + '[/myProcedure/abc]' for $$[/myProcedure/abc].