0

I've looked at various examples of setting variables and conditions for tasks in a DevOps pipeline but I can't quite seem to grok it for the usecase I have.

I want a PowerShell task to set a variable to true or false, then use that variable as a condition for another task which will create an artifact.

What I has so far, from reading around online is this:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      #Some logic here
      
      if ($logicEvaluatedToTrue) {
          Write-Host "Artifacts will be created"
          Write-Host "##vso[task.setvariable variable=requiresRelease;isSecret=false;isOutput=true;]$true"
      } 
      else {
          Write-Host "No artifacts will be created"
      }
    failOnStderr: true

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
       #Both of these do not contain any values
       Write-Host $env:requiresRelease
       Write-Host $requiresRelease
    failOnStderr: true

# this just evaluates to Expanded: and(True, eq(Null, 'true'))
- task: PublishPipelineArtifact@1
  condition: and(succeeded(), eq(variables.requiresRelease, 'true'))
  inputs:
    targetPath: 'snip1.zip'
    artifact: 'snip1'
    publishLocation: 'pipeline'

# this just evaluates to Expanded: and(True, eq(Null, 'true')) too
- task: PublishPipelineArtifact@1
  condition: and(succeeded(), eq(variables['requiresRelease'], 'true'))
  inputs:
    targetPath: 'snip2.txt'
    artifact: 'snip2'
    publishLocation: 'pipeline'

I've tried setting a variable first like this:

variables:
  #existing vars first
  requiresRelease: false

But when I do that, the evaluation of the condition becomes Expanded: and(True, eq('false', 'true')) . Any help you have would be appreciated.

0

1 Answer 1

1

You need to remove the isOutput=true when you assign the value:

Write-Host "##vso[task.setvariable variable=requiresRelease;isSecret=false;]$true"

For print the value use the following syntax:

Write-Host $(requiresRelease)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! Do you have any insight or links for why the isOutput=true was wrong?
When you use isOutput=true you can refer the variable with the task name + variable name. see here: learn.microsoft.com/en-us/azure/devops/pipelines/process/… and here: learn.microsoft.com/en-us/azure/devops/pipelines/scripts/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.