Variables are a crucial component of Azure DevOps that provide a great deal of flexibility. You can define your own variables using libraries that you can import into your pipelines, making them available across all pipelines within your organization.
However, this isn't the only way to use variables in Azure DevOps. You can also define key-value pairs within your pipeline definitions, which will be available in the scope of your pipeline. Additionally, you can set key-value pairs in specific pipeline tasks to use these variables only within that task.
Sometimes, you may need to store a value during pipeline execution and use it in the pipeline's next step. However, if you simply export the variable, you'll find that the value isn't available in the next step, no matter what you did before. This is because output variables exist only within the scope of the task.
To store a value during pipeline execution and make it available in the next step, you can store it in a variable and then use an ad-hoc script to publish it to the output variables. For example, you can store the hostname in a variable called MY_VAR
then use the ##vso
command to publish it. To accomplish this, you can use a Command Line Task to publish the output variable, then use a Bash Script task to retrieve the value from the previous task.
It's important to note that you need to define a reference name as a namespace to ensure that you can access the correct variable in the next task.
In the Command Line Task, the raw log output will display the following information:
In the Bash Script Task, where you call the output variable from the previous task, you'll notice that calling MY_OUTPUT_VAR retrieves the same value as MY_VAR, indicating that you've successfully stored MY_VAR as an output variable.
Furthermore, when accessing the variable in the next task, you must use the reference name and then the variable defined in the previous task.
In conclusion, using variables in Azure DevOps can provide great flexibility and customization for your pipeline. You can define your own variables and make them available across all pipelines within your organization, as well as define key-value pairs within your pipeline definitions or specific pipeline tasks. To store a value during pipeline execution and use it in the pipeline's next step, you can store it in a variable and then use an ad-hoc script to publish it to the output variables. Just remember to define a reference name as a namespace to ensure that you can access the correct variable in the next task.