Search code examples
azureazure-devopsazure-pipelinesazure-pipelines-yamlazure-pipelines-tasks

Set variable on azure task to pass another task of Azure pipeline


I am able to set a variable on powershell or bash script and pass the variable to another task by using ##vso[task.setvariable variable=abc;]xyz. But couldn't find any documentation to set variable on azure tasks like azure webapp deploy task or SqlAzureDacpacDeployment task. I want to catch the error by passing the variable value. Is there any effective way to catch the azure task error log for the next task?

- task: SqlAzureDacpacDeployment@1
  displayName: 'Insertion SQL Task'
  inputs:
    azureSubscription: 'Org (xxxxx-xxxx-xxxx-xxxx-xxxx)'
    ServerName: 'tcp:abc.database.windows.net'
    DatabaseName: test_db
    SqlUsername: '$(user)'
    SqlPassword: $(pass)
    deployType: SqlTask
    SqlFile: 'SQL/test.sql'
  enabled: true

- task: AzureWebApp@1
  displayName: 'Azure Web App Deploy: $(AppName)'
  inputs:
    azureSubscription: 'Org (xxxxx-xxxx-xxxx-xxxx-xxxx)'
    appType: webApp
    ResourceGroupName: 'Test'
    appName: '$(AppName)'
    package: '$(Build.ArtifactStagingDirectory)\app/*.zip'
    deploymentMethod: zipDeploy
  enabled: true

Solution

  • You will need to set a name for the task.

    For Azure SQL Database Deployment task, you can use the SqlDeploymentOutputFile output variable name.

    enter image description here

    - task: SqlAzureDacpacDeployment@1
      displayName: 'Insertion SQL Task'
      name: sqlInsert
      inputs:
        ...
      enabled: true
    
    - script: echo "$(sqlInsert.SqlDeploymentOutputFile)"
    

    The Azure Web App task does not provide the same mechanism. You could always call the Pipelines - Get Logs API to get what you need:

    pool:
      vmImage: ubuntu-latest
    
    steps:
    - powershell: Write-Host "Hello World"
      name: 'HelloWorld'
    
    - powershell: |
        Write-Host "This is a script that could use $env:SYSTEM_ACCESSTOKEN"
        
        # Construct the REST URL to obtain Build ID
        $uri = "$(System.CollectionUri)$(System.TeamProject)/_apis/pipelines/$(System.DefinitionId)/runs/$(Build.BuildId)/logs?api-version=6.0-preview.1"
        Write-Host "$uri"
    
        $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "system", $env:SYSTEM_ACCESSTOKEN)))
        $logs = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
      env:
        SYSTEM_ACCESSTOKEN: $(System.AccessToken)