Reputation: 7854
I'm creating an Azure Pipeline that will have some complex variables, but all of the variable values are one of four possible values. Is it somehow possible to choose variable values from a list when running the pipeline, or would I need to create an extension to accomplish this?
Upvotes: 1
Views: 684
Reputation: 7854
This doesn't seem to be possible, so I used a workaround.
I included config files alongside the project named in the style config-team1.json
, config-team2.json
, etc. I created a variable that can be edited when the pipeline is run called Config.Name
and defaulted it to team1
. I then created a PowerShell task for the pipeline that checks for an appropriately-named config file and copies it to config.json
. For example, if the variable Config.Name
is set to team84
, the script looks for config-team84.json
and copies it to config.json
.
Two other variables are also used, Config.SourceDirectory
and Config.DestinationDirectory
. These just specify where to look for the config file and where to copy it to.
$SourceFile = "config-$env:CONFIG_NAME.json"
$SourcePath = "$env:CONFIG_SOURCEDIRECTORY\$SourceFile"
$DestinationPath = "$env:CONFIG_DESTINATIONDIRECTORY\config.json"
Write-Host "Verifying that config file $SourceFile exists"
$FileExists = Test-Path -Path $SourcePath -PathType leaf
if (!$FileExists) {
throw "Config file does not exist at $SourcePath, cannot continue."
}
Write-Host "Copying config file"
Copy-Item "$SourcePath" -Destination "$DestinationPath"
This is the end of the Pipelines-specific step, the rest is just additional info for how I'm using this in the actual project.
The project being built is written in .NET Core, so I used Microsoft.Extensions.Configuration.Json. An example config file is:
{
"someKey": "someValue",
"anotherKey": 4,
"yetAnotherKey": true
}
You can get the config data like this:
var configuration = new ConfigurationBuilder()
.AddJsonFile("config.json", false) // false means the file is not optional
.Build();
In the above, configuration
will be of type IConfigurationRoot
.
You can use this configuration object directly:
var someVal = configuration["someKey"];
Or you can create a strongly-typed class for the config if you put the keys inside a parent key:
{
"myConfig": {
"someKey": "someValue",
"anotherKey": 4,
"yetAnotherKey": true
}
}
The C# class will look like this:
public class TestConfiguration
{
public string SomeKey { get; set; }
public int AnotherKey { get; set; }
public bool YetAnotherKey { get; set; }
}
Instantiate and use the class like this:
var testConfig = configuration.GetSection("myConfig").Get<TestConfiguration>();
var someVal = testConfig.SomeKey;
Upvotes: 1
Reputation: 18978
You could make use of powershell script to split the values firstly.
For example, there has a variable names var
, and its value is one
, two
, three
, four
. And during the pipeline, I just want the value one
be used.
Step1:
Write a ps1
file which contain below script to split out those values.
Param(
[string]$a
)
[array]$b = $a.split('.')
[string]$ma = $b[0]
Note: I split the variable based on .
. So I store values as one.two.three.four
.
Step2:
Since the value should be split first before others, here add Powershell
task in the top and configure as below format:
Result:
Step3:
After we split it successfully, based on the scenario you want, this value should also be available for next tasks.
Just add another script in split.ps1
file which set the variable which store the value we want as output variable.
Then configure the reference name in the Powershell
task.
Now, all of the next tasks can call that value by using $(ref.var)
.
Upvotes: 1