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

Azure DevOps流水线报错:无效文件路径'/home/vsts/work/1/s'求解决

问题

我正在搭建一条Azure DevOps流水线,流程如下:

  • 使用Snyk进行代码安全扫描
  • 通过Composer验证依赖
  • 构建Docker镜像并推送至ACR
  • 后续计划用ArgoCD或FluxCD实现GitOps模式,变更时从Azure Repo拉取新镜像

但在Install dependencies任务和Docker build任务中持续出现「无效文件路径'/home/vsts/work/1/s'」的错误,排查无果。以下是当前YAML配置及报错信息:

trigger:
- main

pr:
- main

pool:
  vmImage: 'ubuntu-latest'

variables:
  ACRServiceConnection: 'ACRServiceConEH'

jobs:
- job: security
  displayName: 'Security'
  steps:
  - checkout: self
  - task: SnykSecurityScan@1
    inputs:
      serviceConnectionEndpoint: 'SnykConnection'
      testType: 'code'
      codeSeverityThreshold: 'high'
      failOnIssues: false
      projectName: '$(SNYK_PROJECT)'
      organization: '$(SNYK_ORG)'

- job: composer
  dependsOn: security
  displayName: 'Composer Validation'
  steps:
  - checkout: self
  - task: Bash@3
    inputs:
      script: |
        targetDirectory=$(System.DefaultWorkingDirectory)/my_project_directory
        if [ ! -d "$targetDirectory" ]; then
          mkdir -p "$targetDirectory"
        fi
        cd "$targetDirectory"
        composer install --prefer-dist --no-progress
    displayName: 'Install dependencies'

  - task: Bash@3
    inputs:
      script: composer validate --strict
    displayName: 'Validate composer.json and composer.lock'

- job: build
  displayName: 'Build'
  dependsOn: security
  pool:
    vmImage: 'ubuntu-latest'
  steps:
  - checkout: self

  - task: Bash@3
    inputs:
      script: |
        docker build . --file Dockerfile --tag $(imageName):$(Build.SourceVersion)
    displayName: 'Docker build'

  - task: Docker@2
    inputs:
      containerRegistry: $(ACRServiceConnection)
      repository: $(imageName)
      command: 'push'
      tags: '$(Build.SourceVersion)'
    displayName: 'Docker push'

报错信息:

Starting: Install dependencies
Task : Bash
Description : Run a Bash script on macOS, Linux, or Windows
Version : 3.236.1
Author : Microsoft Corporation
##[error]Invalid file path '/home/vsts/work/1/s'.
Finishing: Install dependencies

解决方案

针对「无效文件路径'/home/vsts/work/1/s'」的错误,可从以下几个方向排查修复:

  1. 确认代码检出正常

    • 确保checkout: self任务有权限访问Azure Repo,可在任务中添加persistCredentials: true,或者检查流水线服务账户的仓库读写权限。如果仓库为空或检出失败,会直接导致工作目录/home/vsts/work/1/s不存在。
    • 可在checkout任务中添加clean: true,清理残留文件避免干扰:
      - checkout: self
        clean: true
      
  2. 修正工作目录相关脚本

    • Composer任务中,先确认基准工作目录存在,再处理子目录,避免切换到不存在的路径:
      if [ ! -d "$(System.DefaultWorkingDirectory)" ]; then
        echo "基准工作目录不存在"
        exit 1
      fi
      targetDir=$(System.DefaultWorkingDirectory)/my_project_directory
      mkdir -p "$targetDir"
      cd "$targetDir" || exit 1
      composer install --prefer-dist --no-progress
      
    • Docker build任务中,指定完整的Dockerfile路径,避免相对路径错误:
      docker build $(System.DefaultWorkingDirectory) --file $(System.DefaultWorkingDirectory)/Dockerfile --tag $(imageName):$(Build.SourceVersion)
      
  3. 补全缺失的变量定义
    当前YAML中未声明$(imageName)变量,未定义的变量会导致命令解析异常,间接引发路径错误,需在variables块中添加:

    variables:
      ACRServiceConnection: 'ACRServiceConEH'
      imageName: 'your-acr-repo/your-image-name' # 替换为实际ACR仓库的镜像名
    
  4. 验证作业隔离环境
    每个Job在独立虚拟机中运行,确保每个Job内的checkout任务都完整执行,若某Job的虚拟机初始化异常,也可能导致工作目录缺失。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 18:15:54