- 1 year ago
Setting Windows env vars via azure-pipelines.yml + script
To set environment variables in a Windows environment using azure-pipelines.yml
and a script, you can use the following steps:
Open your
azure-pipelines.yml
file and create a PowerShell or Batch script step.Inside the script step, set the environment variables using the appropriate syntax for the scripting language you are using.
Use the
echo
command to verify that the environment variables have been set correctly.Make sure the script step runs on a Windows agent by specifying the
vmImage
orpool
parameter with a Windows-based agent.
Here's an example of setting environment variables using a PowerShell script step:
yamltrigger:
- main
pool:
vmImage: 'windows-latest'
steps:
- script: |
# PowerShell script to set environment variables
$env:MY_VAR = "Hello, World!"
Write-Host "Environment variable MY_VAR set to: $env:MY_VAR"
displayName: 'Set Environment Variables'
In the example above, the environment variable MY_VAR
is set to "Hello, World!" using the PowerShell script. The Write-Host
statement is used to verify that the environment variable has been set correctly.
Similarly, you can use a Batch script to set environment variables:
yamltrigger:
- main
pool:
vmImage: 'windows-latest'
steps:
- script: |
REM Batch script to set environment variables
SET MY_VAR=Hello, World!
echo Environment variable MY_VAR set to: %MY_VAR%
displayName: 'Set Environment Variables'
Note that the changes to environment variables made in the script step will only affect the current pipeline run. If you want to set environment variables for the entire build/release pipeline, you'll need to use pipeline variables or variable groups in Azure Pipelines.
Remember to adjust the script and variable names to match your specific use case.