Last week, when looking into how to configure the Microsoft Teams reporter for Playwright on Azure DevOps in combination with the Playwright Azure Reporter, I ran into a problem where a variable that set in the Node.js task was not available in the subsequent tasks.
In the reporter’s code, I noticed that the variable gets set using process.env.VARIABLE_NAME = 'value'
. The issue with this code is that the environment variable will only be accessible in the current task. Future tasks will not be able to use it.
The Azure DevOps documentation states that you can set the variable with task.setvariable
in a script, but you run this from bash or PowerShell.
In this post, I will show you how to set a variable in an Azure DevOps pipeline using a Node.js script.
Using the azure-pipelines-task-lib package
While looking for a solution, I found Microsoft’s azure-pipelines-task-lib package. This package provides a set of functions you can use in your Node.js scripts to interact with the Azure DevOps pipeline.
To use the package, you need to install it as a dependency in your project:
|
|
Setting a variable
After installing the package, you can use it in your Node.js script/tasks:
|
|
With this script, you can set the variable VARIABLE_NAME
which is available in the subsequent tasks.
In your Azure DevOps YAML pipeline, you can use the script like this:
|
|
Setting an output variable
The difference between setting a variable and an output variable is that the output variable is available for the future jobs in the pipeline.
|
|
In the YAML pipeline, you can use the output variable like this:
|
|
I hope this post helps you to set variables in your Azure DevOps pipeline using Node.js scripts.