Azure Pipelines引用模板库仓库脚本出现ENOENT错误求助
我正尝试将azure-pipelines.yml中的步骤迁移至流水线模板库仓库(repo-lib)以实现复用,当前架构包含主仓库(repo-a)与共享流水线模板仓库(repo-lib)。
repo-a的azure-pipelines.yml配置:
resources: repositories: - repository: pipeline-templates name: repo-lib extends: template: pipeline-templates/some-lib/azure-pipelines.yml@pipeline-templates
repo-lib的文件结构:
pipeline-templates/some-lib/scripts/script.ps1 pipeline-templates/some-lib/azure-pipelines.yml pipeline-templates/some-lib/some-step.yml
some-step.yml的步骤配置:
steps: - task: PowerShell@2 inputs: filepath: 'pipeline-templates/some-lib/scripts/script.ps1'
流水线运行时出现错误:
##[error]ENOENT: no such file or directory, stat '/home/vsts/work/1/s/pipeline-templates/some-lib/scripts/script.ps1'
尝试过多种路径写法(如../、./、省略pipeline-templates等),均出现类似错误。希望直接调用模板库仓库内的脚本,而非从主仓库引用,但Azure Pipelines似乎一直在主仓库目录中查找脚本。
问题根源在于:Azure Pipelines默认只会克隆主仓库(repo-a)到代理的工作目录(/home/vsts/work/1/s/),共享模板仓库(repo-lib)的代码不会自动拉取到这个目录,所以脚本路径无法被找到。
有两种可行的解决方式:
方式一:显式拉取共享仓库代码
在some-step.yml中先添加checkout步骤拉取模板仓库,再使用对应路径执行脚本:
steps: # 拉取共享模板仓库到指定目录 - checkout: pipeline-templates path: 'repo-lib' # 自定义拉取后的目录名称,简化路径 - task: PowerShell@2 inputs: filepath: 'repo-lib/pipeline-templates/some-lib/scripts/script.ps1'
如果不想自定义路径,也可以用默认路径,此时脚本路径为pipeline-templates/pipeline-templates/some-lib/scripts/script.ps1(第一个pipeline-templates是拉取后的目录名,对应仓库资源的名称)。
方式二:使用任务的target参数指定脚本来源仓库(推荐)
针对Azure DevOps Cloud或2020及以上版本的Server,可以直接通过target参数指定脚本所在的共享仓库,无需手动拉取:
steps: - task: PowerShell@2 inputs: filePath: 'pipeline-templates/some-lib/scripts/script.ps1' target: repository: pipeline-templates
这个方式会让任务直接从模板仓库加载脚本,无需处理本地路径问题。
内容的提问来源于stack exchange,提问作者Michiel

