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.
-
If you want to use an output variable in future stages, add the
isoutput
property to your variable and set the value totrue
.Write-Host "##vso[task.setvariable variable=fileExists;isOutput=true]false"
-
When you set a variable with the
isoutput
property, you can reference that variable in later stages with the taskname
and thestageDependencies
syntax.
- Add a name to your
PowerShell@2
task where you create the output variablefileExists
in stageBuildCommands
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 yourCopyFiles@2
task to:condition: and (succeeded(), eq(variables['setVar.fileExists'], 'false'))
-
Change the condition of your
VagrantTest
stage tocondition: 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.