Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Task custom condition: does a given file exist?

Tags:

azure-devops

What I'm trying to do

I have a task to execute that only makes sense if a certain config file exists. So I want to put a custom condition on the task that returns true if that file exists. I imagine a syntax like this would make sense:

condition: exists('$(projectPath)\myconfigfile.xml')

This seems like a reasonable use case for custom conditions.

What Microsoft documents

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops#functions

Microsoft says the following functions are always available: and, coalesce, contains, endsWith, eq, format, ge, gt, in, le, lt, ne, not, notIn, or, startsWith, xor. And furthermore:

"Depending on context, other functions may be available..."

My actual question

I'm finding this a little frustrating. What "other functions" are there? How can I research them? And in particular, is there one that takes a filename and tells me if that file exists?

like image 956
catfood Avatar asked Feb 28 '19 18:02

catfood


2 Answers

Daniel's answer is correct so just to provide an example:

Create a PowerShell task to set the pipeline variable:

$fileExists = Test-Path -Path "$(System.DefaultWorkingDirectory)/file.txt"
Write-Output "##vso[task.setvariable variable=FileExists]$fileExists"

Create a secondary task that uses the variable in a custom condition:

eq(variables['FileExists'], True)
like image 79
jhhwilliams Avatar answered Nov 06 '22 11:11

jhhwilliams


There is no built in condition or function that operates off of the presence or absence of a file.

You can write and run a script that sets a variable, then check the contents of the variable.

like image 37
Daniel Mann Avatar answered Nov 06 '22 12:11

Daniel Mann