Noobie2021
Noobie2021

Reputation: 371

Powershell script not recognizing file in Devops Pipeline

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 ?

Upvotes: 2

Views: 12027

Answers (2)

Alimur Razi Rana
Alimur Razi Rana

Reputation: 480

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.

Upvotes: 0

GeralexGR
GeralexGR

Reputation: 3582

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

Upvotes: 3

Related Questions