skip to Main Content

Is there a variable available for an Azure devops pipeline task step name, highlighted below?

pipeline image screenshot

1

I’ve looked through the predefined variables at https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=classic but none of them work/are suitable.

2

Answers


  1. From your description, you want to use Pipeline variable to set the Task Group name.

    I am afraid that there is no such pipeline variable can achieve this requirement.

    The Pipeline variable will only be expanded when the pipeline is running. Then the pipeline variable will update the task name.

    When you set a variable in the task definition, it cannot be expanded directly.

    The workaround is that you can hardcode the task name in task definition. In this case, it will show the expected task name.

    For more detailed info, you can refer to this doc: Define variables

    Login or Signup to reply.
  2. Since you are using the classic build pipeline, you can call the build definition REST API to get the task steps names and then set the task names as pipeline variables using logging commands – SetVariable. After that you can use the defined variables in the subsequent tasks in pipeline.

    To do that by following below steps:

    1.Enable the option "Allow scripts to access OAuth token" in Agent job. (Reference the following screenshot for the task steps involved in this sample)
    enter image description here

    2.Add a PowerShell task to set task step variables by running the
    following script: (In this sample we defined the variable taskcount and TaskStep$i)

    $url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/definitions/$env:SYSTEM_DEFINITIONID"
    $result= Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}` 
    $tasks = $result.process.phases.steps.displayName
    $count = $tasks.count
        
    # Set task count variable
    Write-Host "##vso[task.setvariable variable=taskcount]$count"
        
    # Set task step variables
    For($i=0;$i -lt $tasks.Length; $i++) {
      Write-host $($tasks[$i])
      Write-Host ("##vso[task.setvariable variable=TaskStep$i]$($tasks[$i])")
    }
    

    enter image description here

    3.Add another PowerShell task and run below command to check the
    defined TaskStep variables:

    gci  env:* | Where-Object Name -like TaskStep* | sort-object name
    

    enter image description here
    enter image description here

    4.Use the variables in the subsequent tasks in pipeline. Print variable value for example using PowerShell task:

    Write-Host "TaskCount:" $(taskcount)
    Write-Host "TaskStep0:" $(TASKSTEP0)
    Write-Host "TaskStep1:" $(TASKSTEP1)
    Write-Host "TaskStep2:" $(TASKSTEP2)
    Write-Host "TaskStep3:" $(TaskStep3)
    Write-Host "TaskStep4:" $(TaskStep4)
    Write-Host "TaskStep5:" $(TaskStep5)
    Write-Host "TaskStep6:" $(TaskStep6)
    Write-Host "TaskStep7:" $(TaskStep7)
    Write-Host "TaskStep8:" $(TaskStep8)
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search