You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure DevOps Pipeline部署Bicep文件时提示找不到指定文件

解决Azure DevOps Pipeline中Bicep文件找不到的问题

问题场景

配置了用于部署Bicep文件的Azure DevOps Pipeline,执行文件列表任务时明明能看到目标文件存在,但运行az deployment group create命令时却报错找不到文件路径。

Pipeline代码如下:

name: "BRCi.HMRC.Scaffolding"
trigger: none
pool:
  vmImage: "ubuntu-latest"
stages:
  - stage: DeployTest
    variables:
      env: Test
    jobs:
      - deployment: deploy
        environment: Test
        strategy:
          runOnce:
            deploy:
              steps:
                - checkout: self
                - task: CmdLine@2
                  displayName: List files2
                  inputs:
                    script: |
                      echo "Structure of work folder of this pipeline:"
                      tree $(Pipeline.Workspace) 
                - task: AzureCLI@2
                  displayName: Deploy Template
                  inputs:
                    azureSubscription: brci-devops-sc-tst-BRCi.HMRC
                    scriptType: pscore
                    scriptLocation: inlineScript
                    inlineScript: az deployment group create `
                      --resource-group brci-hmrc2-rg-tst
                      --template-file $(Pipeline.Workspace)/Bicep/Main.bicep `
                      --parameters $(Pipeline.Workspace)/Bicep/Hmrc.Parameters.Test.json

报错信息:

ERROR: An error occurred reading file. Could not find a part of the path '/home/vsts/work/1/Bicep/Main.bicep'.

原因及解决方案

1. 路径引用错误

在Azure DevOps的Deployment Job中,checkout: self默认会把代码拉取到$(Build.SourcesDirectory)路径(对应Linux代理下的/home/vsts/work/1/s),而非直接放在$(Pipeline.Workspace)(/home/vsts/work/1)下。你看到的文件实际路径应该是/home/vsts/work/1/s/Bicep/Main.bicep,之前的路径引用少了s目录。

2. PowerShell多行命令换行符缺失

使用PowerShell Core(scriptType: pscore)时,多行命令需要在每行末尾(最后一行除外)添加反引号`,否则命令会被截断,导致参数传递不完整。

修正后的Pipeline代码

name: "BRCi.HMRC.Scaffolding"
trigger: none
pool:
  vmImage: "ubuntu-latest"
stages:
  - stage: DeployTest
    variables:
      env: Test
    jobs:
      - deployment: deploy
        environment: Test
        strategy:
          runOnce:
            deploy:
              steps:
                - checkout: self
                - task: CmdLine@2
                  displayName: Verify file paths
                  inputs:
                    script: |
                      echo "Pipeline workspace structure:"
                      tree $(Pipeline.Workspace) 
                      echo "Build sources directory structure:"
                      tree $(Build.SourcesDirectory)
                - task: AzureCLI@2
                  displayName: Deploy Template
                  inputs:
                    azureSubscription: brci-devops-sc-tst-BRCi.HMRC
                    scriptType: pscore
                    scriptLocation: inlineScript
                    inlineScript: az deployment group create `
                      --resource-group brci-hmrc2-rg-tst `
                      --template-file $(Build.SourcesDirectory)/Bicep/Main.bicep `
                      --parameters $(Build.SourcesDirectory)/Bicep/Hmrc.Parameters.Test.json

额外验证步骤

修改后的CmdLine任务会同时打印$(Pipeline.Workspace)和$(Build.SourcesDirectory)的目录结构,帮你确认文件的实际存放位置,避免路径误解。

内容的提问来源于stack exchange,提问作者Rob Bowman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 10:06:47