0

I have a PowerShell as below:

echo "Hello-World"
$MyVariable = "Proceed"
echo $MyVariable

What I want to do :

If the MyVariable is "Proceed" Only and only then Agent Job2 Should run

I have used PowerShell task and Given Variable name as MyVariableOutput enter image description here

I have done below configuration at Agent Job2 level

enter image description here

Can you please let me know how can I put these condition:

Only If the Powershell Script at Agent Job1 Produce the Proceed as the value of MyVariable Agent Job2 will run.

Note: Agent Job1 and Agent Job2 Are the part of the same release pipeline

Eddie
  • 581
  • 3
  • 11
  • 20

1 Answers1

1

If you prefer to configure your pipeline with GUI, you must run script to add the MyVariable as Variables instead of a temporary variable by calling this api. Because after the agent job1 finished, the variable you just defined with $MyVariable = "Proceed" will not transfer to the next agent job. The agent job1 and agent job2 are independent with each other.


In agent job1:

Configure 2 powershell tasks.

(1) The first one is used to define a variable with value and set it as output variable:

echo "Hello-World"
echo "##vso[task.setvariable variable=MyVariable;isOutput=true]Proceed"
echo $MyVariable

Do not forget specify its reference name MyVariableOutput in this task.

(2) The second job is used to add this output variable into Variables, then the agent job2 could access it:

$connectionToken="{token}"
$urlget = "https://vsrm.dev.azure.com/{org}/{project}/_apis/release/releases/$(Release.ReleaseId)?api-version=5.1"
$base64AuthInfo = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$getdef = Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method GET -ContentType application/json -Uri $urlget 
##Write-Host Pipeline = $($getdef | ConvertTo-Json -Depth 100)
$MyVariable=@"
    {
      "value": "$(MyVariableOutput.MyVariable)"
    }
"@
$getdef.variables | add-member -Name "MyVariable" -value (Convertfrom-Json $MyVariable) -MemberType NoteProperty -Force -PassThru

$getdef = $getdef | ConvertTo-Json -Depth 100
$getdef | clip
$urlput = "https://vsrm.dev.azure.com/{org}/{project}/_apis/release/releases/$(Release.ReleaseId)?api-version=5.1"
$putdef = Invoke-RestMethod -Uri $urlput -Method PUT -Body $getdef -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

The above script will create a variable MyVariable which its value is Proceed.


In agent job2, configure the condition as the shown below:

eq(variables['MyVariable'],'Proceed')

You can see the agent job2 can be run successfully since it has detect the value of MyVariable is Proceed.

enter image description here

Mengdi Liang
  • 17,577
  • 2
  • 28
  • 35
  • Thanks for the reply. If the value of MyVariable is Proceed, why the Agent Job2 skipped? Agent Job2 needs to be executed if it get the value as proceed – Eddie Dec 16 '19 at 14:16
  • @Eddie Forgive my mistake since my brain is still in Monday start-up sequence. Have updated then content and the scripts. Please check and free to correct me. – Mengdi Liang Dec 16 '19 at 15:48
  • Thanks :-) It worked. Just wondering, Can we do this without setting up the environment variable withing the single-agent i.e both tasks are the part of the same agent. – Eddie Dec 17 '19 at 04:29
  • @Eddie, if just pass the variable within tasks not agent jobs, don't need apply such complex scripts. Just pass the output variable as normal method: . – Mengdi Liang Dec 17 '19 at 07:23
  • I have one more query regarding the same here https://stackoverflow.com/questions/59370481/how-restrict-the-agent-job-to-run-only-if-specific-job-succeeded – Eddie Dec 17 '19 at 10:07