Skip to content

Commit 349d6aa

Browse files
docs: Move and improve Devops documentation (#447)
* move docs * unique name * fix links * relocate docs * chapters * add `ArmOutputs: $(ArmOutputs) # only needs to be set for Linux agents` * Update docs/preview/03-Features/powershell/azure-devops.md Co-authored-by: Frederik Gheysels <frederik.gheysels@telenet.be> --------- Co-authored-by: Frederik Gheysels <frederik.gheysels@telenet.be>
1 parent fef5d3e commit 349d6aa

2 files changed

Lines changed: 87 additions & 95 deletions

File tree

docs/preview/02-Guidelines/setting-arm-outputs-to-azure-devops-variable-group.md

Lines changed: 0 additions & 95 deletions
This file was deleted.

docs/preview/03-Features/powershell/azure-devops.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,92 @@ In Azure DevOps, below permissions need to be set on your variable group in orde
120120
- Project Collection Build Service (`<your devops org name>`) - Administrator
121121
- `<your devops project name>` Build Service (`<your devops org name>`) - Administrator
122122

123+
### Guideline
124+
125+
In ARM and Bicep templates it is possible to specify [output parameters](https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/outputs), this enables you to return values from the deployed resources.
126+
127+
To enable maximum re-use of these output parameters within your environment we developed [this script](#setting-arm-outputs-to-azure-devops-variable-group) which is available in the `Arcus.Scripting.DevOps` PowerShell module. It allows you to store those output parameters in an Azure DevOps variable group. This helps you in making sure certain parameters are available throughout your Azure DevOps environment.
128+
129+
For example, think of a use-case where your vital infrastructure components are deployed in a separate Azure DevOps pipeline and need to be referenced from other components. Storing the necessary information such as identifiers, locations or names of these components in an Azure DevOps variable group allows you to easily use these values from other components.
130+
131+
#### Example
132+
##### Specify Output Parameters
133+
So how does this work in practice? Let's take an example where we will deploy a very basic Application Insights instance and specify the `Id` and `ConnectionString` of the Application Insights instance as output parameters.
134+
135+
Our Bicep template looks like this:
136+
``` bicep
137+
param location string = resourceGroup().location
138+
139+
resource applicationInsight 'microsoft.insights/components@2020-02-02' = {
140+
name: 'myAppInsights'
141+
location: location
142+
kind: 'other'
143+
properties: {
144+
Application_Type: 'other'
145+
}
146+
}
147+
148+
output ApplicationInsights_Id string = applicationInsight.id
149+
output ApplicationInsights_ConnectionString string = reference(applicationInsight.id, '2020-02-02').ConnectionString
150+
```
151+
152+
This Bicep template will deploy the Application Insights instance and place the `Id` and `ConnectionString` in the output parameters.
153+
154+
##### Updating The Variable Group
155+
Now all we need to do is execute our [script](#setting-arm-outputs-to-azure-devops-variable-group) which will update the Azure DevOps variable group.
156+
157+
From an Azure DevOps pipeline this can be done like so:
158+
``` powershell
159+
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
160+
Install-Module -Name Arcus.Scripting.DevOps -AllowClobber
161+
162+
Set-AzDevOpsArmOutputsToVariableGroup -VariableGroupName 'myVariableGroup'
163+
```
164+
165+
##### Combining It All In A Pipeline
166+
Now that we have walked through both steps, let's take a look on how to combine all this into an Azure DevOps pipeline.
167+
For this we use YAML and define two tasks, the first will deploy our Application Insights instance and the second will update our Azure DevOps variable group.
168+
169+
``` yaml
170+
- task: AzureResourceGroupDeployment@3
171+
displayName: 'Deploy Bicep template'
172+
inputs:
173+
azureResourceManagerConnection: 'myServiceConnection'
174+
subscriptionId: 'mySubscriptionId'
175+
resourceGroupName: 'myResourceGroup'
176+
location: 'West Europe'
177+
csmFile: 'applicationInsights.bicep'
178+
csmParametersFile: 'applicationInsights.parameters.json'
179+
deploymentOutputs: ArmOutputs
180+
181+
- task: PowerShell@2
182+
displayName: 'Update Variable Group'
183+
env:
184+
system_accesstoken: $(System.AccessToken)
185+
ArmOutputs: $(ArmOutputs) # only needs to be set for Linux agents
186+
inputs:
187+
targetType: 'inline'
188+
script: |
189+
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
190+
Install-Module -Name Arcus.Scripting.DevOps -AllowClobber
191+
192+
Set-AzDevOpsArmOutputsToVariableGroup -VariableGroupName 'myVariableGroup' -ArmOutputsEnvironmentVariableName 'ArmOutputs' -UpdateVariablesForCurrentJob
193+
```
194+
195+
There are a few things worth noting. First of all we define `deploymentOutputs: ArmOutputs` during the `AzureResourceGroupDeployment@3` task. This means that the output parameters we specified in our Bicep template will be placed in a variable called `ArmOutputs`, this is then referenced during the execution of our script with `-ArmOutputsEnvironmentVariableName 'ArmOutputs'`.
196+
197+
Secondly we use `-UpdateVariablesForCurrentJob` as a parameter when calling the script. This means that the output parameters from the Bicep file are also available as pipeline variables in the current running job. While not necessary in our example here, if you need to deploy another Bicep template that needs output parameters from an earlier deployed Bicep template this is the way to do it.
198+
199+
Finally we use `system_accesstoken: $(System.AccessToken)` in the `Powershell@2` task, this is necessary because we need to use the security token used by the running build.
200+
> Please note that the `ArmOutputs` variable is not available 'as is' when executing the Powershell task on a Linux agent. When using a Linux agent, you have to explicitly add that variable in the `env:` section of the Powershell task.
201+
#### Closing Up
202+
Using this setup we are able to deploy a Bicep template and update an Azure DevOps variable group with the specified output parameters!
203+
204+
> ⚠ Before running your pipeline, make sure the variable group already exists in Azure DevOps and the permissions below are set:
205+
> - Project Collection Build Service (`<your devops org name>`) - Administrator
206+
> - `<your devops project name>` Build Service (`<your devops org name>`) - Administrator
207+
208+
123209
## Setting ARM outputs to Azure DevOps pipeline variables
124210

125211
Sets the ARM outputs as variables to an Azure DevOps pipeline during the execution of the pipeline.
@@ -228,3 +314,4 @@ Example of how to use this function in an Azure DevOps pipeline:
228314
229315
Save-AzDevOpsBuild -ProjectId $project -BuildId $buildId
230316
```
317+

0 commit comments

Comments
 (0)