azureazure-devopsazure-pipelinesazure-logic-appsazure-logic-app-standard

Initiate Azure DevOps Release Pipeline from Logic App and Retrieve Environment Variable Output from the pipeline


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:

  1. Initiate Release Pipeline: Use a Logic App to start an Azure DevOps release pipeline.

  2. Retrieve Environment Variable: Extract the output of a specific environment variable from the Azure DevOps pipeline.

  3. Use Environment Variable in Logic App: Use the retrieved environment variable in the subsequent stage of the Logic App.

Logic App

I have encountered a couple of issues that I'm unable to resolve:

  1. Infinite Loop with Delay: I added a delay to wait for a pipeline stage to complete successfully, but it results in an infinite loop.

  2. 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.


Solution

  • 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.

    1. Create a variable group in Azure DevOps and add a variable named Data. enter image description here
    2. Call REST API Variablegroups - Update in your pipeline and update the value of 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
    
    
    1. Create a PAT with Variable Groups (Read) scope at least. Run the following PowerShell scripts locally to get base64string of the PAT.
    $token = "{Your PAT}"
    $newtoken = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
    Write-Host $newtoken
    
    1. Create a parameter in your Logic app and add the newtoken you get. enter image description here

    2. Add a HTTP action after Delay action and call REST API Variablegroups - Get to get the value of the variable.

      • URI: https://dev.azure.com/{OrgName}/{ProjectName}/_apis/distributedtask/variablegroups/{VariableGroupId}?api-version=7.1-preview.2
      • Method: GET
      • Add a Header: "Authorization" : "Basic @{parameters('PAT')}"

      enter image description here

    3. 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. enter image description here