Azure Devops: Using dynamic variable names in customCondition - azure

I have created a pipeline variable dynamically using powershell script with variable name
$ReleaseVariableName = "[$(Release.EnvironmentName)]_should_run_regression"
Now in next Job, I want to add a customCondition to check if value of above variable is "True".
I have tried couple of steps like using join,
eq(join('_', #(variables['Release.EnvironmentName'], 'should_run_regression')), 'True')
but that doesn't work since I cannot declare array using # in custom condition. So is there a way to use dynamic variable names in azure custom conditions?
PS:
I am working with Classic UI so YAML solutions don't work.
The step where I am using this custom condition is agentless job, so I can't use powershell scripts to read dynamic variable and set a Job level variable
I have an alternative of creating different variables for each stage but that looks awful

Related

how can I use the agent user defined capabilities in my azure pipelines.yml file as a variable?

Within our pipeline's we would like to set a variable based on some user defined capabilities. For example, agent-1 may store all python versions under "C:/Python" whereas agent-2 may store all python versions under "C:/Documents/Python" and a script may need to know of all the contents stemming from this folder. So, to fix this, we set some user capabilities of where it's stored.
Agent 1: PYTHON_DIR = C:/Python
Agent 2: PYTHON_DIR = C:/Documents/Python
We would like to extract these from in our azure-pipelines.yml for use in future script steps.
We initially tried using the syntax:
variables:
PYTHON_EXE: $(PYTHON_DIR)\Python38\...\python.exe
but this simply echos out as
$(PYTHON_DIR)\Python38\...\python.exe even after an agent reboot.

Reference group variable in Azure pipeline

I tried to find online, but all the options I tried are not working for me.
I have an Azure pipeline that runs a Python script, and inside of it I need to fetch a group variable value, this variable contains the banckend Url. I tried multiple ways, but still not working. Could anyone shed a light here?
This is the way I am trying to add the value with $(ENVIRONMENT), but I also tried $(env:ENVIRONMENT), $(Environment), $(env:Environment).
Thank you in advace.
On this Azure Pipelines page, you'll see a TAB called Variables. Click in this tab, select Variables Group and then Link Variable Group. In this way, you can select what variable group you'll use.
To use a variable from a variable group, you need to reference a key from the variable group, for example:
Example of variable group Development with the key/values:
- name: URL
value: http://localhost
- name: ENVIRONMENT
value: DEV
In your pipeline:
echo $(URL)
echo $(ENVIRONMENT)
Using the Python task:
import os
URL = os.environ.get('URL')
print (URL)
I recommend you change this pipeline from the Classic mode to Yaml mode; the pipeline will be more modular and reusable.
Microsoft doc: https://learn.microsoft.com/en-us/azure/devops/pipelines/library/variable-groups?view=azure-devops-2022&tabs=yaml

Return a value from node.js script in azure pipeline?

In Azure Pipelines, I see that you can access the environment variables from scripts in node.js during a pipeline run. However, I want to actually return a value and then capture/use it.
Does anyone know how to do this? I can't find any references on how to do this in documentation.
For consistency's sake it'd be nice to use node scripts for everything and not go back and forth between node and bash.
Thanks
Okay I finally figured this out. Azure documentation is a bit confusing on the topic, but my approach was what follows. In this example, I'm going to make a rather pointless simple script that sets a variable whose value is the name of the source branch, but all lower case.
1) Define your variable
Defining a variable can be done simply (though there is a lot of depth to how variables are used and I suggest consulting Azure documentation on variable creation for more). However, at the top of your pipeline yaml file you can define it as such:
variables
lowerCaseBranchName: ''
This creates an empty variable for use across your jobs. We'll use this variable as our example.
2) Create your script
"Returning a value" from your script simply means outputting it via node's stdout, the output of which will be consumed by the task to set it as a pipeline variable.
An important thing to remember is that any environment variables from the pipeline can be used within node, they are just reformatted and moved under node's process.env global. For instance, the commonly used Build.SourceBranchName environment variable in azure pipelines is accessible in your node script via its alias process.env.BUILD_SOURCEBRANCHNAME. This uppercase name transformation should be uniform across all environment variables.
Here's an example node.js script:
const lowerCaseBranchName = process.env.BUILD_SOURCEBRANCHNAME.toLowerCase();
process.stdout.write(lowerCaseBranchName);
3) Consume the output in the relevant step in azure pipelines
To employ that script in a job step, call it with a script task. Remember that a script task is, in this case, a bash script (though you can use others) that runs node as a command as it sets the value of our variable:
- script: |
echo "##vso[task.setvariable variable=lowerCaseBranchName]$(node path/to/your/script)"
displayName: 'Get lower case branch name'
Breaking down the syntax
Using variable definition syntax is, in my opinion extremely ugly, but pretty easy to use once you understand it. The basic syntax for setting a variable in a script is the following:
##vso[task.setvariable variable=SOME_VARIABLE_NAME]SOME_VARIABLE_VALUE
Above, SOME_VARIABLE_NAME is the name of our variable (lowerCaseBranchName) as defined in our azure pipeline configuration at the beginning. Likewise, SOME_VARIABLE_VALUE is the value we want to set that variable to.
You could do an additional line above this line to create a variable independently that you can then use to set the env variable with, however I chose to just inline the script call as you can see in the example above usign the $() syntax.
That's it. In following tasks, the environment variable lowerCaseBranchName can be utilized using any of the variable syntaxes such as $(lowerCaseBranchName),
Final result
Defining our variable in our yaml file:
variables
lowerCaseBranchName: ''
Our nodejs script:
const lowerCaseBranchName = process.env.BUILD_SOURCEBRANCHNAME.toLowerCase();
process.stdout.write(lowerCaseBranchName);
Our pipeline task implementation/execution of said script:
- script: |
echo "##vso[task.setvariable variable=lowerCaseBranchName]$(node path/to/your/script)"
displayName: 'Get lower case branch name'
A following task using its output:
- script: |
echo "$(lowerCaseBranchName)"
displayName: 'Output lower case branch name'
This will print the lower-cased branch name to the pipline console when it runs.
Hope this helps somebody! Happy devops-ing!

Can a global expression be created in Azure Data Factory?

I have certain expressions used by multiple steps in Azure Data Factory.
For instance, I get the start time of a pipeline often:
#formatDateTime(pipeline().TriggerTime, 'yyyy-MM-dd HH:mm:ss.fff')
If I need to change it for some reason, I'd need to update it everywhere.
I know about global parameters; however, is there a way to create global expressions?
Based on my knowledge, currently there is no way to have a global expression similar to Global parameters.
For you have to have a common source of truth or 1 common expression, I would suggest you to create a common pipeline in the ADF and call that pipeline via execute pipeline activity to avoid re writing the same code every where.
You could add a variable to the pipeline and set it to the appropriate value at the start of your pipeline run: https://learn.microsoft.com/en-us/azure/data-factory/control-flow-set-variable-activity
You can then refer to this variable as you do with parameters or change its value on the go.

Azure DevOps Release Pipeline - How to set and pass values in variables in pipeline with Azure Powershell

With Azure Release Pipeline, in a task using the PowerShell Script, I am able to set values of variables and pass to next task using the command
Write-Host '##vso[task.setvariable variable=varResourceExists;isOutput=true;something'
However, when I put this similar command in a task that uses Azure PowerShell, this command is no longer allowed, the task produces a warning:
2019-10-22T00:23:14.3080614Z ##[warning]'##vso[task.setvariable
variable=varResourceExists;isOutput=true;something' contains logging
command keyword '##vso', but it's not a legal command. Please see the
list of accepted commands:
https://go.microsoft.com/fwlink/?LinkId=817296
As a result, the variable varResourceExists cannot be set by my task. I have also tried a conventional PowerShell set value by doing
$varResourceExists = 'something'; # this also does not work
Is there a way I can set this value in Azure Powershell script so that the next task can reference it?
##vso[task.setvariable variable=varResourceExists;isOutput=true;something is not correct syntax. You're missing a closing ].
It should be ##vso[task.setvariable variable=varResourceExists;isOutput=true;]something
Here is how I solved my topic. In the pipeline Azure PowerShell task, I can have code such as Write-Host '##vso[task.setvariable variable=varResourceExists;isOutput=true;]False';
In the Output Variable option, I set a Reference Name "step1":
Output Variable
Then in the next step, I can do a conditional check using a Custom Condition:
Custom Condition
I can also reference the variable in my code such as Write-Host "The step1.varResourceExists says: $(step1.varResourceExists)";

Resources