Reputation: 6958
I'm porting some Jenkins builds to Azure Pipelines. One of the port sets involves an arbitrary list of environment variables that constantly changes. In Jenkins I just run an injection script to run on a list file that is within the code base's source and the build references those variables in whatever scripts or build-steps need them. Some of the variables are hard-coded while others reference environment variables of the build itself.
I looked at the Define variables documentation but it either doesn't show a way to accomplish this, or I somehow missed it. How do I go about doing this besides resorting to something like Setx
? I have no way of knowing which build will use which variables so hard-coding them is pointless. The only constant is, I know to look for a root-level file called env-vars.txt
.
Upvotes: 0
Views: 1509
Reputation: 6958
There is an Azure Pipelines build Task module available on the VS Marketplace that can do this called Json to Variable, by Jeff Przylucki. I can either request the repo's devs to update their env-vars.txt into a JSON or run an existing tool to convert Key=Value pairs to JSONs.
Task that will read a JSON file present on your build agent, from a build or a release, and will Generate Build/Release Variables to be used in other steps.
Json to Variable: https://marketplace.visualstudio.com/items?itemName=OneLuckiDev.json2variable
Upvotes: 0
Reputation: 698
The only constant is, I know to look for a root-level file called env-vars.txt
Assuming this is just a list of key:value pairs, you can read them in something like this.
If you don't want to touch the format of the file you could parse it as per @shamrai-aleksander answer and set the variables that way.
variables:
- template: env-vars.yml
env-vars.yml
variables:
somevariable:value
Upvotes: 0
Reputation: 16018
You can use the logging commands to use job-scope variables: https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands?view=azure-devops&tabs=bash.
Then use them in your steps: Environment variables
System and user-defined variables also get injected as environment variables for your platform. When variables are turned into environment variables, variable names become uppercase, and periods turn into underscores. For example, the variable name any.variable becomes the variable name $ANY_VARIABLE.
Example:
Step 1
- powershell: |
Write-Host "##vso[task.setvariable variable=my.var1]var value 1"
Write-Host "##vso[task.setvariable variable=my.var2]var value 2"
Write-Host "##vso[task.setvariable variable=my.var3]var value 3"
Write-Host "##vso[task.setvariable variable=my.var4]var value 4"
displayName: 'Set vars'
'
Step 2
- powershell: |
Write-Host "Var1:" $Env:MY_VAR1
Write-Host "Var2:" $Env:MY_VAR2
Write-Host "Var3:" $Env:MY_VAR3
Write-Host "Var4:" $Env:MY_VAR4
displayName: 'Read vars'
The result of step 2
Upvotes: 1