Azure Pipeline Part 11 – Functions

Azure Pipeline Part 11 – Functions

Azure Pipeline Part 11 – Functions

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

Today we are going to discuss different in-built functions available in Azure Pipeline with examples. You can use function to evaluate an expression on your pipeline to make some logical decision.

Azure Pipeline Part 11 – Functions

Prerequisites:

Setup:

Azure Pipeline – Functions

and():

Evaluates to True if all parameters are True. Min parameters: 2. Max parameters: N. Example: and(eq(variables.letters, ‘ABC’), eq(variables.numbers, 123))

coalesce():

Evaluates the parameters in order, and returns the value that does not equal null or empty-string. Min parameters: 2. Max parameters: N. Example: coalesce(variables.couldBeNull, variables.couldAlsoBeNull, ‘literal so it always works’)

contains():

Evaluates True if left parameter String contains right parameter. Min parameters: 2. Max parameters: 2. Casts parameters to String for evaluation. Performs ordinal ignore-case comparison. Example: contains(‘ABCDE’, ‘BCD’) (returns True).

containsValue():

Evaluates True if the left parameter is an array, and any item equals the right parameter. Also evaluates True if the left parameter is an object, and the value of any property equals the right parameter. Min parameters: 2. Max parameters: 2. If the left parameter is an array, convert each item to match the type of the right parameter. If the left parameter is an object, convert the value of each property to match the type of the right parameter. The equality comparison for each specific item evaluates False if the conversion fails. Ordinal ignore-case comparison for Strings. Short-circuits after the first match

convertToJson():

Take a complex object and outputs it as JSON. Min parameters: 1. Max parameters: 1.

counter():

This function can only be used in an expression that defines a variable. It cannot be used as part of a condition for a step, job, or stage. Evaluates a number that is incremented with each run of a pipeline. Parameters: 2. prefix and seed. Prefix is a string expression. A separate value of counter is tracked for each unique value of prefix. The prefix should use UTF-16 characters. Seed is the starting value of the counter.

endsWith():

Evaluates True if left parameter String ends with right parameter. Min parameters: 2. Max parameters: 2. Casts parameters to String for evaluation. Performs ordinal ignore-case comparison. Example: endsWith(‘ABCDE’, ‘DE’) (returns True)

eq():

Evaluates True if parameters are equal. Min parameters: 2. Max parameters: 2. Converts right parameter to match type of left parameter. Returns False if conversion fails. Ordinal ignore-case comparison for Strings. Example: eq(variables.letters, ‘ABC’).

format():

Evaluates the trailing parameters and inserts them into the leading parameter string. Min parameters: 1. Max parameters: N. Example: format(‘Hello {0} {1}’, ‘John’, ‘Doe’).

ge():

Evaluates True if left parameter is greater than or equal to the right parameter. Min parameters: 2. Max parameters: 2. Converts right parameter to match type of left parameter. Errors if conversion fails.

Ordinal ignore-case comparison for Strings. Example: ge(5, 5) (returns True).

gt():

Evaluates True if left parameter is greater than the right parameter. Min parameters: 2. Max parameters: 2. Converts right parameter to match type of left parameter. Errors if conversion fails. Ordinal ignore-case comparison for Strings. Example: gt(5, 2) (returns True)

in():

Evaluates True if left parameter is equal to any right parameter. Min parameters: 1. Max parameters: N. Converts right parameters to match type of left parameter. Equality comparison evaluates False if conversion fails. Ordinal ignore-case comparison for Strings. Short-circuits after first match. Example: in(‘B’, ‘A’, ‘B’, ‘C’) (returns True).

join():

Concatenates all elements in the right parameter array, separated by the left parameter string. Min parameters: 2. Max parameters: 2. Each element in the array is converted to a string. Complex objects are converted to empty string. If the right parameter is not an array, the result is the right parameter converted to a string.

le():

Evaluates True if left parameter is less than or equal to the right parameter. Min parameters: 2. Max parameters: 2. Converts right parameter to match type of left parameter. Errors if conversion fails.

Ordinal ignore-case comparison for Strings. Example: le(2, 2) (returns True)

length():

Returns the length of a string or an array, either one that comes from the system or that comes from a parameter. Min parameters: 1. Max parameters 1. Example: length(‘fabrikam’) returns 8

lower():

Converts a string or variable value to all lowercase characters. Min parameters: 1. Max parameters 1. Returns the lowercase equivalent of a string. Example: lower(‘FOO’) returns foo.

lt():

Evaluates True if left parameter is less than the right parameter. Min parameters: 2. Max parameters: 2. Converts right parameter to match type of left parameter. Errors if conversion fails. Ordinal ignore-case comparison for Strings. Example: lt(2, 5) (returns True)

ne():

Evaluates True if parameters are not equal. Min parameters: 2. Max parameters: 2. Converts right parameter to match type of left parameter. Returns True if conversion fails. Ordinal ignore-case comparison for Strings. Example: ne(1, 2) (returns True)

not():

Evaluates True if parameter is False. Min parameters: 1. Max parameters: 1. Converts value to Boolean for evaluation. Example: not(eq(1, 2)) (returns True)

notIn()

Evaluates True if left parameter is not equal to any right parameter. Min parameters: 1. Max parameters: N. Converts right parameters to match type of left parameter. Equality comparison evaluates False if conversion fails. Ordinal ignore-case comparison for Strings. Short-circuits after first match. Example: notIn(‘D’, ‘A’, ‘B’, ‘C’) (returns True)

or():

Evaluates True if any parameter is True. Min parameters: 2. Max parameters: N. Casts parameters to Boolean for evaluation. Short-circuits after first True. Example: or(eq(1, 1), eq(2, 3)) (returns True, short-circuits)

replace():

Returns a new string in which all instances of a string in the current instance are replaced with another string. Min parameters: 3. Max parameters: 3. replace(a, b, c): returns a, with all instances of b replaced by c. Example: replace(‘https://www.tinfoilsecurity.com/saml/consume’,’https://www.tinfoilsecurity.com’,’http://server’) (returns http://server/saml/consume)

startsWith():

Evaluates True if left parameter string starts with right parameter. Min parameters: 2. Max parameters: 2. Casts parameters to String for evaluation. Performs ordinal ignore-case comparison. Example: startsWith(‘ABCDE’, ‘AB’) (returns True)

upper():

Converts a string or variable value to all uppercase characters. Min parameters: 1. Max parameters 1. Returns the uppercase equivalent of a string. Example: upper(‘bah’) returns BAH

xor():

Evaluates True if exactly one parameter is True. Min parameters: 2. Max parameters: 2. Casts parameters to Boolean for evaluation. Example: xor(True, False) (returns True)

Azure Pipeline – Functions Example:

Azure Pipeline – Job Status Check Functions:

You can use the following status check functions as expressions in conditions, but not in variable definitions.

always():

Always evaluates to True (even when canceled). Note: A critical failure may still prevent a task from running. For example, if getting sources failed.

canceled():

Evaluates to True if the pipeline was canceled.

failed():

For a step, equivalent to eq(variables[‘Agent.JobStatus’], ‘Failed’). For a job: With no arguments, evaluates to True only if any previous job in the dependency graph failed. With job names as arguments, evaluates to True only if any of those jobs failed.

succeeded():

For a step, equivalent to in(variables[‘Agent.JobStatus’], ‘Succeeded’, ‘SucceededWithIssues’). For a job: With no arguments, evaluates to True only if all previous jobs in the dependency graph succeeded or partially succeeded. If the previous job succeeded but a dependency further upstream failed, succeeded(‘previousJobName’) will return true. When you just use dependsOn: previousJobName, it will fail because all of the upstream dependencies were not successful. To only evaluate the previous job, use succeeded(‘previousJobName’) in a condition. With job names as arguments, evaluates to True if all of those jobs succeeded or partially succeeded. Evaluates to False if the pipeline is canceled.

succeededOrFailed():

For a step, equivalent to in(variables[‘Agent.JobStatus’], ‘Succeeded’, ‘SucceededWithIssues’, ‘Failed’). For a job: With no arguments, evaluates to True regardless of whether any jobs in the dependency graph succeeded or failed. With job names as arguments, evaluates to True whether any of those jobs succeeded or failed. This is like always(), except it will evaluate False when the pipeline is canceled.

Azure Pipeline – Job Status Check Functions Example:

Clean up:

Hope you have enjoyed this article. To know more about Azure DevOps, please refer below official documentation

https://docs.microsoft.com/en-us/azure/devops/?view=azure-devops

Leave a Reply

Close Menu