I'm working on automating a release pipeline using Azure DevOps and a Logic App. The goal is to initiate the release pipeline from the Logic App when an HTTP request is received and then to retrieve the output of an environment variable from the Azure DevOps pipeline to use it in the next stage of the Logic App.
Below is a summary of what I am trying to achieve and the issues I am encountering:
Steps:
Initiate Release Pipeline: Use a Logic App to start an Azure DevOps release pipeline.
Retrieve Environment Variable: Extract the output of a specific environment variable from the Azure DevOps pipeline.
Use Environment Variable in Logic App: Use the retrieved environment variable in the subsequent stage of the Logic App.
I have encountered a couple of issues that I'm unable to resolve:
Infinite Loop with Delay: I added a delay to wait for a pipeline stage to complete successfully, but it results in an infinite loop.
JSON Parsing Issue: I tried to retrieve output environment variables of the pipeline using a parse JSON stage, but it is not functioning as expected.
The output variable set using logging command is only available to the next tasks during running time. You can't get it via the REST API, that's why you can't retrieve it in your Parse JSON action.
As a workaround, you can create a variable group in Azure DevOps and use it to store your target variable. See the detailed steps below.
Data
.
Data
with your output variable's value.steps:
- task: PowerShell@2
inputs:
targetType: 'inline'
script: 'Write-Output "##vso[task.setvariable variable=Data;]Test"'
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$header = @{'Authorization' = 'Bearer ' + "$(System.AccessToken)"}
$url="https://dev.azure.com/{Org Name}/_apis/distributedtask/variablegroups/{Variable Group Id}?api-version=7.1-preview.2"
$body = @'
{
"variables": {
"Data": {
"value": "$(Data)"
}
},
"name": "LogicApp",
"variableGroupProjectReferences": [
{
"projectReference": {
"id": "$(System.TeamProjectId)",
"name": "$(System.TeamProject)"
},
"name": "LogicApp",
}
]
}
'@
Invoke-RestMethod -Uri $url -Method PUT -Headers $header -Body $body -ContentType application/json
$token = "{Your PAT}"
$newtoken = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
Write-Host $newtoken
Create a parameter in your Logic app and add the newtoken
you get.
Add a HTTP action after Delay action and call REST API Variablegroups - Get to get the value of the variable.
https://dev.azure.com/{OrgName}/{ProjectName}/_apis/distributedtask/variablegroups/{VariableGroupId}?api-version=7.1-preview.2
GET
Then you can see the variable in the response body of HTTP action, and you can retrieve the variable and use it in the next actions.