Azure pipeline: how to pass value between attempts of the same step?

Kelly Zhao 65 Reputation points Microsoft Employee
2024-03-21T03:59:10.5166667+00:00

Hi I have an Azure pipeline defined by a yaml file. I need to pass a value between attempts of the same step, which means, when the first build failed at the step and I triggered a rerun, I need to use some value from the first attempt in the second attempt.

I have tried something like this but doesn't work.

    `- task: PowerShell@2`
````          inputs:`

`            targetType: 'inline'`

`            script: |`

`              if ($env:SYSTEM_JOBATTEMPT -eq 1)`

`              {`

`                  Write-Host "First attempt"`

`                  $value = "a"`

`                  Write-Host "##vso[task.setvariable variable=somevariable;isOutput=true]$value"`

`                  exit 1`

`              }`

`              else{`

`                  Write-Host "Second attempt, value is $($somevariable)"`

`                  $value = "b"`

`                  Write-Host "##vso[task.setvariable variable=somevariable;isOutput=true]$value"`

`              }`

The error looks like this  
`somevariable : The term 'somevariable' is not recognized as the name of a`

`cmdlet, function, script file, or operable program. Check the spelling of the`

`name, or if a path was included, verify that the path is correct and try again.`  
  
Could anyone please help?  
  
Thank you!
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
39,088 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Martin Therkelsen 1,405 Reputation points MVP
    2024-03-21T09:48:06.11+00:00

    Hi,

    I don't think it will be a good practice to create a pipeline like this. The inputs to the pipeline should be coming from the logic that can be used at each run.

    If you need the output you could change that external environment with a task from the pipelines first run. For instance, change an App Service setting to false if it fails the run, then the next run will detect that the app setting is not false instead of true.

    If you explain a bit more about what kind of output you need to get, maybe I can help some more with the configuration.

    0 comments No comments

  2. ManoharLakkoju 690 Reputation points Microsoft Vendor
    2024-03-21T11:37:34.1866667+00:00

    @Kelly Zhao
    Welcome to Microsoft Q&A Platform, thanks for posting your query here. Azure DevOps related queries/issues are currently not supported on this Microsoft Q&A platform.

    I would request you to please post your queries in dedicated forums as in below links:

    https://developercommunity.visualstudio.com/spaces/21/index.html

    https://developercommunity.visualstudio.com/t/get-unique-id-from-devops-organization/756710

    https://stackoverflow.com/questions/tagged/azure-devops

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.