Azure构建管道无法在dotnet核心项目中找到文件。

huangapple go评论51阅读模式
英文:

Azure Build Pipeline cannot locate a file in dotnet core project

问题

我有一个PowerShell脚本在我的项目源代码文件夹中。我正在尝试执行该文件,以在构建管道结束时进行第三方调用。当我查看源代码库时,文件位于以下文件夹中:

ProjectName\Scripts\pwsh\PowerShell1.ps1

构建管道有以下任务:

  • task: PowerShell@2
    displayName: 第三方调用
    inputs:
    filePath: '$(Build.SourcesDirectory)\ProjectName\Scripts\pwsh\PowerShell1.ps1'
    pwsh: true

我收到以下错误消息:

##[error]无效的文件路径 'D:\a\1\s\ProjectName\scripts\pwsh\PowerShell1.ps1'。需要指定.ps1文件的路径。

据我了解,$(Build.SourcesDirectory) 是项目源代码的根目录,所以我假设它将具有与我的仓库相同的文件夹结构。我不知道从这里该去哪里,因为我无法看到 $(Build.SourcesDirectory) 中的内容以更改我的PowerShell脚本的正确位置。有人可以告诉我需要指向哪里吗?

编辑:管道的完整YAML文件。

trigger:

  • master

pool:
vmImage: 'windows-latest'

variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'

steps:

  • task: UseDotNet@2
    displayName: '使用dotnet 7'
    inputs:
    version: '7.0.x'
    packageType: runtime

  • task: DotNetCoreCLI@2
    inputs:
    command: 'publish'
    publishWebProjects: true
    arguments: '--configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory)'

  • task: VSBuild@1
    inputs:
    solution: '$(solution)'
    platform: '$(buildPlatform)'
    configuration: 'DatabaseBuild'
    maximumCpuCount: true
    msbuildArchitecture: 'x64'

  • task: CopyFiles@2
    inputs:
    SourceFolder: '$(System.DefaultWorkingDirectory)'
    Contents: |
    **/*.dacpac
    TargetFolder: '$(Build.ArtifactStagingDirectory)'

  • task: PublishBuildArtifacts@1
    inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

  • task: PowerShell@2
    displayName: New Relic 部署标记
    inputs:
    filePath: '$(Build.SourcesDirectory)\ProjectName\scripts\pwsh\PowerShell1.ps1'
    arguments: '-ApiKey "$(ApiKey)" -EntityGuid "$(EntityId)" -Version "1.0.0" -Description "Azure CI/CD部署" -User "DevOps"'
    pwsh: true

Azure构建管道无法在dotnet核心项目中找到文件。

英文:

I have a PowerShell script in a folder in the source code for my project. I am trying to execute the file to make a third party call at the end of my build pipeline. When I look at the source repo the file is in the following folder:

ProjectName\Scripts\pwsh\PowerShell1.ps1

The build pipeline has the following task:

- task: PowerShell@2
  displayName: Third Party Call
  inputs: 
    filePath: '$(Build.SourcesDirectory)\ProjectName\Scripts\pwsh\PowerShell1.ps1'
  pwsh: true

I get the following error:

##[error]Invalid file path 'D:\a\s\ProjectName\scripts\pwsh\PowerShell1.ps1'. A path to a .ps1 file is required.

It is my understanding that $(Build.SourcesDirectory) is the root directory of the source of the project, so I assumed it would have the same folder structure as my repo. I don't know where to go from here as I can't see what is in $(Build.SourcesDirectory) to alter the file path to the correct location of my PowerShell script. Can someone advise me where I need to point please?

Edit: full yml file for the pipeline.

trigger:
- master

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: UseDotNet@2
  displayName: 'Use dotnet 7'
  inputs:
    version: '7.0.x'
    packageType: runtime

- task: DotNetCoreCLI@2
  inputs:
    command: 'publish'
    publishWebProjects: true
    arguments: '--configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    platform: '$(buildPlatform)'
    configuration: 'DatabaseBuild'
    maximumCpuCount: true
    msbuildArchitecture: 'x64'

- task: CopyFiles@2
  inputs:
    SourceFolder: '$(System.DefaultWorkingDirectory)'
    Contents: |
      **/*.dacpac
    TargetFolder: '$(Build.ArtifactStagingDirectory)'

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

- task: PowerShell@2
  displayName: New Relic Deployment Marker
  inputs: 
    filePath: '$(Build.SourcesDirectory)\ProjectName\scripts\pwsh\PowerShell1.ps1'
    arguments: '-ApiKey "$(ApiKey)" -EntityGuid "$(EntityId)" -Version "1.0.0" -Description "Deployed by Azure CI/CD" -User "DevOps"'
    pwsh: true

Azure构建管道无法在dotnet核心项目中找到文件。

答案1

得分: 0

默认情况下,构建代理不会创建 ProjectName 文件夹。它只会检出到 $(Build.SourcesDirectory) 的根目录。您可以尝试:

filePath: ''$(Build.SourcesDirectory)\\ProjectName\\Scripts\\pwsh\\PowerShell1.ps1'';

或者只使用相对路径从当前文件夹开始:

filePath: ''./Scripts/pwsh/PowerShell1.ps1'';
英文:

By default, a build agent does not create ProjectName folder. It just checkouts to the root of $(Build.SourcesDirectory). You may try:

filePath: '$(Build.SourcesDirectory)\ProjectName\Scripts\pwsh\PowerShell1.ps1'

or just a relative path from the current folder:

filePath: './Scripts/pwsh/PowerShell1.ps1'

答案2

得分: 0

我将把这个作为答案,因为故事有点长 Azure构建管道无法在dotnet核心项目中找到文件。

在你的YAML中,你没有一个特定的checkout任务或下载artifact任务,这意味着仓库会直接被检出(不使用项目/构件或仓库名称)到源目录中。所以你说的"ProjectName"是什么意思?它是仓库根目录下的一个文件夹吗?还是"scripts"在仓库的根目录下?

你必须让$(Build.SourcesDirectory)后面的路径相对于流水线所使用的仓库的根目录。

其他使用的构件将以它们的相对名称添加到源目录中,例如:

- download: current
  artifact: some-drop

将会位于$(Build.SourcesDirectory)\some-drop\

但对于你来说情况并非如此。

英文:

I am going to make this an answer as the story is a bit long Azure构建管道无法在dotnet核心项目中找到文件。

You do not have a specific checkout task or download artifact task in your YAML, meaning the repository is checked out directly (not with a project/artifact or repository name) into the sources directory. So what do you mean by 'ProjectName'?, is that a folder in the root of the repository? Or is 'scripts' in the root of the repository?

You have to make the path after $(Build.SourcesDirectory) relative from the root in the repository used by the pipeline.

Other artifacts used will be added to the sourcesdirectory with their relative name,

for example:

- download: current
  artifact: some-drop

will be located in $(Build.SourcesDirectory)\some-drop\

But this is not the case for you

huangapple
  • 本文由 发表于 2023年6月22日 14:22:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定