In Azure Pipelines, configuration variables represent a necessary functionality for your releases: it’s possible to save some values indeed, that might be useful in build or deploy stage to configure application during the release.
The hidden mechanism is defined as variable substitution, and it is used to configure files as .config or .json. The use of these variables in a pipeline definition is simple: all it takes is to use the name associated to the variable, enclosed to the instruction
When we use the deployment groups in Azure Pipelines to associate more than one target node to a release, all the nodes will receive the same configuration already present in variables.
During a consulting to one of our clients on Azure Pipelines, the need to configure some variables with different values based on target node arises. There are different contexts, where a similar situation may occur again: logging, audit and advanced application configuration. Before we go into detail of the solution, we should understand how deployment groups and related variables work.
What are deployment groups in Azure Pipelines
Deployment groups are a very simple way to organize nodes on which you will deploy an application in Azure Pipelines. Their configuration is really fast and it’s based on the installation of an agent: it is only needed, in fact, to run a registration script, preset in Azure Pipeline on the target machine (image 1). Once registered, when an artifact is ready after a build, an agent will get it to install to the target machine.
1. Installation of an agent in deployment groups
During the execution of the registration script, you will be asked for some information. Among them, the agent name to be used for the machine: this value will be used to identify variables related to release stages.
When the installation finishes, the agent is rightly configured and it’s possible to display the status in the deployment group page, as in image 2:
2. Agent status
We are finally ready to see how to configure variables in our deployment group.
What are variables in Azure Pipelines
Variables are a convenient way to save information that will be used in build or release stage. It’s possible to define them in the section “Library” (image 3) of Azure DevOps, using a key to identify them and they can be of two types: “in clear text” or secret (used to save sensitive information).
3. Library in Azure DevOps
Variables can also be included in a group, in order to have a good organization. Once the group has been created, you only need to add variables to it (image 4).
4. Variables groups in Azure DevOps
As we can see in image 5, groups can be connected to a release in variables section (1) clicking on Variable groups (2) and then Link variable group (3). Once the group (4) of variable to use has been selected, it’s even possible to set the purpose, to which variables must be associated: to release or to specified target environment (5).
5. Connect a variables group to a release
As revealed in advance, configurations present in this variables group will be accurately reported for every target present in the deployment group. But what if we don’t need this result? In next paragraph, we discover how to configure specific variables for a single node.
Configure one variable per node
We consider as example this situation: I should deploy a web application and, for one release pipeline, I have 3 target nodes associated to my deployment group. I would like that, for some configurations, their value is specific for the target node and not same as all nodes. How to do that? Unfortunately, to the present day, there’s nothing “ready to use” in Azure Pipelines to manage a situation like this. But the solution is quite simple to implement. This is what we have to do:
- Define a variable with a key that contains the name of the agent present in the deployment group;
- Create, inside your build, a Script Powershell task, to manually substitute the value present in a configuration file.
For the first step, you only need to define variables as following:
6. Variabili per host
Please note: it’s important that the value associated to Host1, Host2 e Host3 corresponds to agent name present in the deployment group. For example, if the name of the agent is PRODSERVER, my variable will be called PRODSERVER.MyCustomVariable. It is necessary to pay attention to those agent name that have a dash (“-“), because the script may cause trouble during the execution stage.
To recover the value contained in the variable during release stage, you only need to add a Powershell script of inline type, with following instructions:
# Stampa il valore corrente Write-Output "$($(Agent.Name).MyCustomVariable)" # Leggi il tuo file di configurazione e sostituisci il valore con $($(Agent.Name).MyCustomVariable) # ... # Salva il file con la nuova configurazione # ...
Specifically, the command
does nothing but recover the agent name, on which you are making the release and extract the value contained in the variable PRODSERVER.MyCustomVariable. With this simpe script, we are ready to go!
Have a nice deploy!