0

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

David
  • 3
  • 4
  • Hi David, and welcome to Stack Overflow! Can I ask, what kind of task is task 1? Depending on what kind of task it is, it might have defined an [output variable](https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#use-output-variables-from-tasks) with the value you need. – Vince Bowdren May 06 '22 at 16:18
  • 1
    Hi Vince, thank you very much! It is a bult-in kubectl action. I've just added it to the thread description to give more details. Would you have any suggestion on how to refer the LoadBalancer IP created on task 1, applying it at `--service-url` parameter on task 2? – David May 06 '22 at 16:56

1 Answers1

0

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:

Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56