2

I have a powershell script in my devops repository called functionapp.ps1 located in a folder called Deploy.

I have the following code line of code in my yml file :

  - task: PowerShell@2
    inputs:
      filePath: '$(Pipeline.Workspace)/Deploy/functionapp.ps1'

When the stages are being run and it arrives to this task I get the following error:

##[error]Invalid file path 'D:\a\1\Deploy\functionapp.ps1'. A path to a .ps1 file is required.

I tried using filePath: '$(System.DefaultWorkingDirectory)/Deploy/functionapp.ps1'

I ended up having the same error. Can someone please tell me what is the issue here ?

8
  • You should use command line task to show the value of the default value '$(System.DefaultWorkingDirectory)', and the folder structure in the repo. Commented Jan 4, 2022 at 5:38
  • @LeoLiu-MSFT I tried using the following filePath: '$(System.DefaultWorkingDirectory)/ExportServiceFunctionApp/Deploy/functionapp.ps1' but still it doesn't work. Commented Jan 4, 2022 at 7:56
  • Just as I asked, you need use command line task to show the value of the default value '$(System.DefaultWorkingDirectory)', and the folder structure in your repo. Commented Jan 4, 2022 at 9:29
  • @LeoLiu-MSFT I tested the command line task. It shows that the working directory have the following name: D:\a\1\s I have no idea from where a\1\s come from. What should I change in this case in the powershell filepath ? Commented Jan 4, 2022 at 10:28
  • 1
    Yes, that'll be it. You need to do a fresh checkout in each job. Commented Jan 5, 2022 at 11:07

2 Answers 2

3

The default directory where your source code files are downloaded is C:\agent_work\s and can be referenced from the build in variable $(Build.SourcesDirectory)

As a result you will need to use the below filepath on the powershell task:

filePath: '$(Build.SourcesDirectory)/Deploy/functionapp.ps1'

You could also use

 filePath: '$(Pipeline.Workspace)/s/Deploy/functionapp.ps1'

If the checkout step for the self (primary) repository has no custom checkout path defined, or the checkout path is the multi-checkout default path $(Pipeline.Workspace)/s/<RepoName> for the self repository, the value of this variable will revert to its default value, which is $(Pipeline.Workspace)/s

A list with the build in variables can be found from the below URL:

https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you very much for your answer. I changed the filepath as you mentioned as the first way it also gave me the following error: ##[error]Invalid file path 'D:\a\1\s\Deploy\functionapp.ps1'. A path to a .ps1 file is required.
Try also to set your working folder on the powershell script. workingDirectory: '$(Pipeline.Workspace)/s' . Also make sure that the file name is correct. You can make a directory listing using a test powershell before the execution of the actual powershell.
The working directory ends with the following folder ExportServiceFunctionApp. However, the Deploy mentioned above is not included in the folder. So basically in the repository I have 3 folders: ExportServiceFunctionApp, ExportServiceTests and the Deploy Folder which contains the powershell script.
Do you think that I should put the powershell script in the same folder as the working Directory ?
I tried all these ways and nothing worked. It keeps on giving me the same error. Any clue what should I do ?
|
0

You can check the existing file structures in your pipeline by running these lines of code -

targetType: 'inline'
script: |
 echo '$(System.DefaultWorkingDirectory)'
 dir

if you do not get any file structures then you can add this line before the path reference -

- checkout: self
  fetchDepth: 0

you have no checkout step which is equivalent to checkout: self, your source code is checked out into a directory called s located as a subfolder of (Agent.BuildDirectory). If (Agent.BuildDirectory) is C:\agent_work\1, your code is checked out to C:\agent_work\1\s.

fetchDepth: 0

Disable shallow fetch which is by-default.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.