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

使用GitHub Actions+Maven编译代码失败,报目录不存在错误

GitHub Actions编译环节目录不存在问题解决

问题描述

通过YAML配置GitHub Actions工作流,setup环节执行正常,但编译环节失败,报错信息:

Error: An error occurred trying to start process '/usr/bin/bash' with working directory '/home/runner/work/Project/Project/article'. No such file or directory

问题原因

GitHub Actions中每个Job运行在独立的Runner实例,不同Job之间的文件系统完全隔离。setup Job里通过actions/checkout将代码检出到article目录,但这个操作仅对当前Job的Runner有效。compile和test Job是全新的Runner环境,未执行代码检出,因此不存在article目录,导致报错。

解决方案

方案1:在需要代码的Job中添加检出步骤

在compile和test Job里分别加入actions/checkout步骤,指定检出路径为article,保证Job环境中存在目标目录:

修改后的compile Job:

compile:
  needs: setup
  runs-on: ubuntu-latest
  steps:
    - name: Checkout branch 🛎️
      uses: actions/checkout@v3
      with:
        path: 'article'
    - name: Compile code with maven 🛠️
      working-directory: 'article'
      run: mvn compile

修改后的test Job:

test:
  needs: compile
  runs-on: ubuntu-latest
  steps:
    - name: Checkout branch 🛎️
      uses: actions/checkout@v3
      with:
        path: 'article'
    - name: Run tests with maven 🧪
      working-directory: 'article'
      run: mvn test

方案2:合并Job减少重复操作

如果setup、compile、test不需要并行执行,可以将三个步骤合并到同一个Job中,避免重复检出代码,提升效率:

name: Article module development-CI

on:
  workflow_call:
  pull_request:
    branches:
      - "dev"
    paths:
      - 'article/**'
    types: [closed]

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout branch 🛎️
        uses: actions/checkout@v3
        with:
          path: 'article'
      - name: Set up JDK 18 🏗️
        uses: actions/setup-java@v3
        with:
          java-version: '18'
          distribution: 'temurin'
          cache: maven
      - name: Compile code with maven 🛠️
        working-directory: 'article'
        run: mvn compile
      - name: Run tests with maven 🧪
        working-directory: 'article'
        run: mvn test

原配置代码参考

主工作流YAML

name: Article module development-CI

on:
  workflow_call:
  pull_request:
    branches:
      - "dev"
    paths:
      - 'article/**'
    types: [closed]

jobs:
  setup:
    uses: ./.github/workflows/setup-jdk.yml
    with:
      working-directory: 'article'

  compile:
    needs: setup
    runs-on: ubuntu-latest
    steps:
      - name: Compile code with maven 🛠️
        working-directory: 'article'
        run: mvn compile

  test:
    needs: compile
    runs-on: ubuntu-latest
    steps:
      - name: Run tests with maven 🧪
        working-directory: 'article'
        run: mvn test

复用的setup工作流YAML

name: Setup JDK and Checkout Code

on:
  workflow_call:
    inputs:
      java-version:
        description: 'Java version'
        required: false
        default: '18'
        type: string
      working-directory:
        description: 'Working directory'
        required: false
        default: '.'
        type: string

jobs:
  setup:
    runs-on: ubuntu-latest
    outputs:
      working-directory: ${{ inputs.working-directory }}
    steps:
      - name: Checkout branch 🛎️
        uses: actions/checkout@v3
        with:
          path: ${{ inputs.working-directory }}

      - name: Set up JDK ${{ inputs.java-version }} 🏗️
        uses: actions/setup-java@v3
        with:
          java-version: ${{ inputs.java-version }}
          distribution: 'temurin'
          cache: maven

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 13:33:45