Search code examples
variablesazure-devopsoutputazure-pipelinesazure-pipelines-release-pipeline

Getting values from Azure DevOps Release Pipeline Task output


I'm new on Azure DevOps, so I'm struggling to solve a basic problem about variables. Hope anyone can give me a hand with it.

Basically, my release pipeline has a job called "Job 1". It has 2 tasks:

Task 1: Deploy a container in an AKS cluster and expose it via an internal LoadBalancer.

steps:
- task: Kubernetes@1
  displayName: Deployment
  inputs:
    connectionType: 'Azure Resource Manager'
    azureSubscriptionEndpoint: 'MY-SUBSCRIPTION'
    azureResourceGroup: 'MY-RESOURCEGROUP
    kubernetesCluster: 'MY-AKS-CLUSTER'
    namespace: 'MY-NAMESPACE'
    command: apply
    useConfigurationFile: true
    configuration: '$(System.DefaultWorkingDirectory)/_TEST/deployment.yaml'

Note: I checked the option to enable the Output Variables functionality Output Box.

Task 2: Create and configure an API in an API Management, setting that internal LoadBalancer as a backend.

The task 1 gives me the internal LoadBalancer's ipaddress via its output json file. Output json file

I would like to get that ipaddress and use it at Task 2, which runs the following command:

az apim api create --service-name $(apimanagement) -g $(apim-resourcegroup) --api-id $(apiname) --path $(apiname) --display-name $(apiname) --service-url http://_LBIPADDRESS_/openapi.json --subscription-key-required false

My question: How do I make this reference at --service-url parameter?

Any suggestion will be appreciated. Thank you!

Best regards, David


Solution

  • The Kubernetes task has an output variable, in your case called deployment.KubectlOutput, which should have the json data you mentioned. A small script is a good way to get the ip address from that json structure into a single pipeline variable; in this case I am scripting in powershell, but you could use bash if that suits your environment better.

    task: Powershell@2
      displayName: Extract IP Address
      inputs:
        targetType: inline
        script: |
          $outputData = $(deployment.KubectlOutput) | ConvertFrom-Json;
          $ipAddress = $outputData.status.loadBalancer.ingress.ip;
          Write-Host "##vso[task.setvariable variable=_LBIPADDRESS_;isOutput=true;]$ipAddress"
    

    This creates a pipeline variable called LBIPADDRESS with the value you need; then, your next task can reference it just like all the other pipeline variables:

        az apim api create `
          --service-name $(apimanagement) `
          -g $(apim-resourcegroup) `
          --api-id $(apiname) `
          --path $(apiname) `
          --display-name $(apiname) `
          --service-url "http://$(_LBIPADDRESS_)/openapi.json" `
          --subscription-key-required false          
    

    Note: I have drawn on other stack overflow questions which dealt with the Kubernetes task and with output variables; they might be useful to you as well: