How to add condition to a stage in azure-pipeline.yaml properly

I have a pipeline that creates a chocolatey package based on the repo.

  • In the first stage I create the package, check if the package created and copy the nupkg to the destination. That works perfectly as it is.
  • In the second stage I’d like to run a Vagrant test that tests the choco package but only when the file created. So based on the previous file exist check i’d add the condition to the whole stage.

In the first stage the condition on the CopyFiles works just perfectly, but when I add the same condition on the 2nd stage it just skips every single time. Without the condition it starts and writes the “test”.

So far the azure-pipeline.yaml’s interesting part is the following. The “*****” is mostly for anonimity.

variables:
  solution: *****'
  buildConfiguration: 'Release'
  buildPlatform: 'Any CPU'
  chocoSource: '*****'
  localChocoFolder: '.chocolateypack'
  binaryInputFolder: *****'
  localFolder: '$(Build.SourcesDirectory)Package'
  dropFolder: *****'
  vagrantDropFolder: *****'
  fileExists: true
  
stages:
- stage: BuildCommands
  displayName: Build Commands stage
  jobs: 
  - job: BuildCommands
    timeoutInMinutes: 120
    displayName: Build commands job 
    pool:
     name: 'Bugzing'

    *****
    *****
    *****
    *****
    *****

    - task: PowerShell@2
      displayName: 'Check for existing files in target folder'
      inputs:
        targetType: 'inline'
        script: |         
          # Access pipeline variables
          $localFolder = "${env:localFolder}"
          $dropFolder = "${env:dropFolder}"

          # Get the list of local files
          $localFiles = Get-ChildItem -Path $localFolder -Recurse -File

          foreach ($localFile in $localFiles) {
            $relativePath = $localFile.FullName.Substring($localFolder.Length)
            $targetFilePath = Join-Path $dropFolder $relativePath

            if (Test-Path $targetFilePath) {
              Write-Host "##vso[task.logissue type=warning]File already exists in target folder: $targetFilePath"
              Write-Host "##vso[task.complete result=SucceededWithIssues;]"
              exit 0
            }
          }
          
          Write-Host "##vso[task.setvariable variable=fileExists]false"

    - task: CopyFiles@2
      condition: and (succeeded(), eq(variables['fileExists'], 'false'))
      displayName: 'Copy installer'
      inputs:
        SourceFolder: '$(localFolder)'
        Contents: '**'
        TargetFolder: '$(dropFolder)'
       
- stage: VagrantTest
  displayName: Test Package With Vagrant
  dependsOn: BuildCommands
  condition: and(succeeded(), eq(dependencies.BuildCommands.outputs['BuildCommands.fileExists'], 'false'))
  jobs: 
  - job: BuildCommands
    timeoutInMinutes: 120
    displayName: Build commands job 
    pool:
     name: 'Bugzing'
     
    steps:
    - task: PowerShell@2
      displayName: stage 2 echo test
      inputs:
        targetType: 'inline'
        script: |
          echo "test"

To make your pipeline work as expected, you need to adjust the followings.

  1. If you want to use an output variable in future stages, add the isoutput property to your variable and set the value to true.

    Write-Host "##vso[task.setvariable variable=fileExists;isOutput=true]false"

  2. When you set a variable with the isoutput property, you can reference that variable in later stages with the task name and the stageDependencies syntax.

  • Add a name to your PowerShell@2 task where you create the output variable fileExists in stage BuildCommands as shown below.
    - task: PowerShell@2
      name: setVar
  • Once isoutput=true is added to your output variable, the syntax changes for referencing an output variable in the same job. Change the condition of your CopyFiles@2 task to:

    condition: and (succeeded(), eq(variables['setVar.fileExists'], 'false'))

  • Change the condition of your VagrantTest stage to

    condition: and(succeeded(), eq(dependencies.BuildCommands.outputs['BuildCommands.setVar.fileExists'], 'false')).

You can refer to the following example:

stages:
- stage: BuildCommands
  displayName: Build Commands stage
  jobs: 
  - job: BuildCommands
    timeoutInMinutes: 120
    displayName: Build commands job 
    steps:
    - task: PowerShell@2
      name: setVar
      displayName: 'Check for existing files in target folder'
      inputs:
        targetType: 'inline'
        script: |         
          Write-Host "##vso[task.setvariable variable=fileExists;isOutput=true]false"
    - task: CopyFiles@2
      condition: and (succeeded(), eq(variables['setVar.fileExists'], 'false'))
      displayName: 'Copy installer'
      inputs:
        SourceFolder: '$(localFolder)'
        Contents: '**'
        TargetFolder: '$(dropFolder)'
       
- stage: VagrantTest
  displayName: Test Package With Vagrant
  dependsOn: BuildCommands
  condition: and(succeeded(), eq(dependencies.BuildCommands.outputs['BuildCommands.setVar.fileExists'], 'false'))
  jobs: 
  - job: BuildCommands
    timeoutInMinutes: 120
    displayName: Build commands job 
    steps:
    - task: PowerShell@2
      displayName: stage 2 echo test
      inputs:
        targetType: 'inline'
        script: |
          echo "test"

Result:

See the detailed info about output variable from Set an output variable for use in future stages.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật