2

I have an Az Powershell script (let's just call it ```DeployApps.ps1```) which I run from the commandline, having initialised a couple of variables which get passed into the script as parameters. The script will basically deploy the named application to one or more environments which are subject to the initialisation of the environment variable.

The variable declaration can be depicted as follows:

$ApplicationName = 'HelloWorldApp'
$Environment = @('DEV','SIT','UAT','PROD')

I have to emphasise though that the $Environment variable can be initialised to one or more values and so even if set to say 'DEV' alone, this should still be perfectly fine and the script will run as expected.

The ApplicationName variable can also be initialised to one of a number of applications and not just the HelloWorldApp.

From the command line, the script execution itself can then be depicted as follows:

DeployApps.ps1 $ApplicationName $Environment

I now need to replicate all of the above into an Azure Pipeline (YAML). The Powershell script itself is versioned in my Azure Repo and so calling or referencing it from an Azure Powershell/Cli Task in my pipeline is fairly easy and straightforward, as depicted below.

enter image description here

My Task/Challenge
What I'd now like to do is replicate in the pipeline, exactly how I execute the script from the commandline. By this, I mean being able to set the two variables to the required random values, before or when the pipeline is run. In other words, I can set the two variables which then get passed into the script execuction.

Any ideas or advice on how to accomplish this?

hitman126
  • 699
  • 1
  • 12
  • 43

1 Answers1

0

You can use variable groups (Library) and stages for each scenario. Variable groups are used to store value and secrets. They can be passed into a single YAML pipeline or multiple ones as well as one job or different ones. You will need to create a variable group, initialize all your variables and then link it to your pipeline. You will be able to execute your pipeline (all stages) or a single stage to perform what you want.

Link: https://learn.microsoft.com/en-us/azure/devops/pipelines/library/variable-groups?view=azure-devops&tabs=classic#create-a-variable-group

If you are looking for a more dynamic option, you can use runtime parameters in your YAML file. A parameter must contain a name, a data type, and an assigned default value when running your pipeline. You will have to set the trigger to none in your pipeline if you want to use runtime parameters to manually initialize them each time you run the pipeline.

Link: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/runtime-parameters?view=azure-devops&tabs=script#use-parameters-in-pipelines

Nadine Raiss
  • 591
  • 1
  • 4
  • 17
  • I don't think you've taken into account one key fact that I mentioned @Nadine Raiss - the number of parameters will not be static or fixed and so I don't think variable groups will work in that scenario. You have to remember that the pipeline could run with the Environment variable set to one, two, three, four or more values at any given time. Would a variable group cater for that? – hitman126 Nov 10 '21 at 07:11
  • I updated my answer based on your comment. I believe runtime parameters will be a good option for your use case. – Nadine Raiss Nov 14 '21 at 12:28