---
title: Update a variable in a release
description: "What we need to do is create a variable in the release definition and set its scope to \"Release\": ![Update a variable in a release](http://ariste.info/wp-content/uploads/2019/10/2019-10-27-11_14_57-AXZ-Dev-Release-Daily-16.30-Pipelines.png) Then, **for each stage**, we need to enable this checkbox in the agent job: ![Update…"
url: "https://ariste.info/dynamics365almguide/update-a-variable-in-a-release/"
updated: "2024-08-25"
category: Legacy Guide / Automation
---

# Update a variable in a release

What we need to do is create a variable in the release definition and set its scope to "Release":

![Update a variable in a release](http://ariste.info/wp-content/uploads/2019/10/2019-10-27-11_14_57-AXZ-Dev-Release-Daily-16.30-Pipelines.png)

   Then, **for each stage**, we need to enable this checkbox in the agent job:

![Update a variable in a release](http://ariste.info/wp-content/uploads/2019/10/2019-10-27-11_26_29-AXZ-Dev-Release-Daily-16.30-Pipelines.png)

   I explain later why we're enabling this. We now only need to update this variable after uploading the DP to LCS. Add an inline Powershell step after the upload one and do this:

```
# Populate & store value to update pipeline
$assetId= "$(GoldenUpload.FileAssetId)"
Write-Output ('##vso[task.setvariable variable=localAsset]{0}' -f $assetId)

#region variables
$ReleaseVariableName = 'axzfileid'
$releaseurl = ('{0}{1}/_apis/release/releases/{2}?api-version=5.0' -f $($env:SYSTEM_TEAMFOUNDATIONSERVERURI), $($env:SYSTEM_TEAMPROJECTID), $($env:RELEASE_RELEASEID)  )
#endregion

#region Get Release Definition
Write-Host "URL: $releaseurl"
$Release = Invoke-RestMethod -Uri $releaseurl -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
#endregion

#region Output current Release Pipeline
#Write-Output ('Release Pipeline variables output: {0}' -f $($Release.variables | #ConvertTo-Json -Depth 10))
#endregion

#Update axzfileid with new value
$release.variables.($ReleaseVariableName).value = $assetId

#region update release pipeline
Write-Output ('Updating Release Definition')
$json = @($release) | ConvertTo-Json -Depth 99
$enc = [System.Text.Encoding]::UTF8
$json= $enc.GetBytes($json)
Invoke-RestMethod -Uri $releaseurl -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" }
#endregion
```

   You need to change the following:

- Line 2: $assetId= "$(GoldenUpload.FileAssetId)". Change $(GoldenUpload.FileAssetId) for your output variable name.
- Line 6: $ReleaseVariableName = 'axzfileid'. Change axzfileid for your Release variable name.

   And you're done. This script uses Azure DevOps' REST API to update the variable value with the file id, and we enabled the OAuth token checkbox to allow the usage of this API without having to pass any user credentials. This is not my idea obviously, I've done [this thanks to this post from Stefan Stranger's blog](https://stefanstranger.github.io/2019/06/26/PassingVariablesfromStagetoStage/).   Now, in the deploy stages you need to retrieve your variable's value in the following way:

![Update a variable in a release](http://ariste.info/wp-content/uploads/2019/10/2019-10-27-11_21_02-AXZ-Dev-Release-Daily-16.30-Pipelines.png)

   **Don't forget the ( ) or it won't work!**   And with these small changes you can have a release like this:

![Update a variable in a release](http://ariste.info/wp-content/uploads/2019/10/pipelines.png)

   With a single DP upload to LCS and multiple deployments using the file uploaded in the first stage. With approvals, and delays, and emails, and everything!

#### Subscribe!

Receive an email when a new post is published
