如何解决nektos/act运行时的mvn: command not found问题
解决
mvn: command not found错误(nektos/act运行JavaDoc生成Workflow) 我在参与GitHub项目贡献时,编写了用于测试JavaDoc文件生成的GitHub Action Workflow,使用nektos/act工具运行时出现mvn: command not found错误,项目地址为https://github.com/MathieuSoysal/Javadoc-publisher.yml,相关信息如下:
错误日志
[Test Actions/Test with Java 11] 🚀 Start image=catthehacker/ubuntu:act-latest [Test Actions/Test with Java 11] 🐳 docker pull image=catthehacker/ubuntu:act-latest platform= username= forcePull=false [Test Actions/Test with Java 11] 🐳 docker create image=catthehacker/ubuntu:act-latest platform= entrypoint=["/usr/bin/tail" "-f" "/dev/null"] cmd=[] [Test Actions/Test with Java 11] 🐳 docker run image=catthehacker/ubuntu:act-latest platform= entrypoint=["/usr/bin/tail" "-f" "/dev/null"] cmd=[] [Test Actions/Test with Java 11] ☁ git clone 'https://github.com/GuillaumeFalourd/assert-command-line-output' # ref=v2 [Test Actions/Test with Java 11] ⭐ Run Pre GuillaumeFalourd/assert-command-line-output@v2 [Test Actions/Test with Java 11] ☁ git clone 'https://github.com/actions/setup-node' # ref=v2 [Test Actions/Test with Java 11] ✅ Success - Pre GuillaumeFalourd/assert-command-line-output@v2 [Test Actions/Test with Java 11] ⭐ Run Main actions/checkout@v3 [Test Actions/Test with Java 11] 🐳 docker cp src=/workspaces/Javadoc-publisher.yml/. dst=/workspaces/Javadoc-publisher.yml [Test Actions/Test with Java 11] ✅ Success - Main actions/checkout@v3 [Test Actions/Test with Java 11] ⭐ Run Main ./ [Test Actions/Test with Java 11] ⭐ Run Main actions/checkout@v3 [Test Actions/Test with Java 11] 🐳 docker cp src=/workspaces/Javadoc-publisher.yml/. dst=/workspaces/Javadoc-publisher.yml [Test Actions/Test with Java 11] ✅ Success - Main actions/checkout@v3 [Test Actions/Test with Java 11] ☁ git clone 'https://github.com/actions/setup-java' # ref=v3 [Test Actions/Test with Java 11] ⭐ Run Main actions/setup-java@v3 [Test Actions/Test with Java 11] 🐳 docker cp src=/home/codespace/.cache/act/actions-setup-java@v3/ dst=/var/run/act/actions/actions-setup-java@v3/ [Test Actions/Test with Java 11] 🐳 docker exec cmd=[node /var/run/act/actions/actions-setup-java@v3/dist/setup/index.js] user= workdir= [Test Actions/Test with Java 11] ❓ ::group::Installed distributions | Trying to resolve the latest version from remote | Resolved latest version as 11.0.17+8 | Trying to download... | Downloading Java 11.0.17+8 (Adopt-Hotspot) from https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.17%2B8/OpenJDK11U-jdk_x64_linux_hotspot_11.0.17_8.tar.gz ... [Test Actions/Test with Java 11] 💬 ::debug::Downloading https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.17%252B8/OpenJDK11U-jdk_x64_linux_hotspot_11.0.17_8.tar.gz [Test Actions/Test with Java 11] 💬 ::debug::Destination /tmp/8533ad34-250e-4794-b6bd-f014648ce8b2 [Test Actions/Test with Java 11] 💬 ::debug::download complete | Extracting Java archive... [Test Actions/Test with Java 11] 💬 ::debug::Checking tar --version [Test Actions/Test with Java 11] 💬 ::debug::tar (GNU tar) 1.30%0ACopyright (C) 2017 Free Software Foundation, Inc.%0ALicense GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.%0AThis is free software: you are free to change and redistribute it.%0AThere is NO WARRANTY, to the extent permitted by law.%0A%0AWritten by John Gilmore and Jay Fenlason. | [command]/usr/bin/tar xz --warning=no-unknown-keyword -C /tmp/4f3b749d-13d5-497d-87d7-45e30a9927cc -f /tmp/8533ad34-250e-4794-b6bd-f014648ce8b2 [Test Actions/Test with Java 11] 💬 ::debug::Caching tool Java_Adopt_jdk 11.0.17-8 x64 [Test Actions/Test with Java 11] 💬 ::debug::source dir: /tmp/4f3b749d-13d5-497d-87d7-45e30a9927cc/jdk-11.0.17+8 [Test Actions/Test with Java 11] 💬 ::debug::destination /opt/hostedtoolcache/Java_Adopt_jdk/11.0.17-8/x64 [Test Actions/Test with Java 11] 💬 ::debug::finished caching tool | Java 11.0.17+8 was downloaded | Setting Java 11.0.17+8 as the default | Creating toolchains.xml for JDK version 11 from adopt | Writing to /root/.m2/toolchains.xml | | Java configuration: | Distribution: adopt | Version: 11.0.17+8 | Path: /opt/hostedtoolcache/Java_Adopt_jdk/11.0.17-8/x64 | [Test Actions/Test with Java 11] ❓ ::endgroup:: [Test Actions/Test with Java 11] ❓ ##[add-matcher]/run/act/actions/actions-setup-java@v3/.github/java.json | Creating settings.xml with server-id: github | Writing to /root/.m2/settings.xml [Test Actions/Test with Java 11] ✅ Success - Main actions/setup-java@v3 [Test Actions/Test with Java 11] ⚙ ::set-output:: distribution=Adopt-Hotspot [Test Actions/Test with Java 11] ⚙ ::set-output:: path=/opt/hostedtoolcache/Java_Adopt_jdk/11.0.17-8/x64 [Test Actions/Test with Java 11] ⚙ ::set-output:: version=11.0.17+8 [Test Actions/Test with Java 11] ⭐ Run Main Generate Javadoc [Test Actions/Test with Java 11] 🐳 docker exec cmd=[bash --noprofile --norc -e -o pipefail /var/run/act/workflow/1-composite-2.sh] user= workdir= | /var/run/act/workflow/1-composite-2.sh: line 2: mvn: command not found [Test Actions/Test with Java 11] ❌ Failure - Main Generate Javadoc [Test Actions/Test with Java 11] exitcode '127': command not found, please refer to https://github.com/nektos/act/issues/107 for more information [Test Actions/Test with Java 11] ☁ git clone 'https://github.com/JamesIves/github-pages-deploy-action' # ref=v4.4.0 [Test Actions/Test with Java 11] ❌ Failure - Main ./ [Test Actions/Test with Java 11] exitcode '127': command not found, please refer to https://github.com/nektos/act/issues/107 for more information [Test Actions/Test with Java 11] ⭐ Run Post ./ [Test Actions/Test with Java 11] ⭐ Run Post actions/setup-java@v3 [Test Actions/Test with Java 11] 🐳 docker exec cmd=[node /var/run/act/actions/actions-setup-java@v3/dist/cleanup/index.js] user= workdir= [Test Actions/Test with Java 11] ✅ Success - Post actions/setup-java@v3 [Test Actions/Test with Java 11] ✅ Success - Post ./ [Test Actions/Test with Java 11] 🏁 Job failed
GitHub Workflow配置
name: Test Actions on: [pull_request, push] jobs: test: runs-on: ubuntu-latest name: Test with Java 11 steps: - uses: actions/checkout@v3 - uses: ./ # Uses an action in the root directory with: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} javadoc-branch: javadoc-test java-version: 11 target-folder: javadoc - uses: GuillaumeFalourd/assert-command-line-output@v2 with: command_line: ls -lha contains: javadoc expected_result: PASSED
问题原因
GitHub官方的ubuntu-latest Runner预装了Maven,但nektos/act默认使用的catthehacker/ubuntu:act-latest是精简版镜像,没有预装Maven;同时项目根目录的action.yml中仅配置了Java环境,未安装Maven,导致运行mvn命令时找不到该工具。
解决方法
方法1:修改GitHub Action,添加Maven安装步骤
在项目根目录的action.yml中,在actions/setup-java@v3步骤之后,新增安装Maven的步骤:
steps: - uses: actions/checkout@v3 - name: Set up Java uses: actions/setup-java@v3 with: java-version: ${{ inputs.java-version }} distribution: 'adopt' - name: Install Maven run: | sudo apt-get update sudo apt-get install -y maven # 原有的Generate Javadoc等后续步骤...
方法2:运行act时使用预装Maven的完整镜像
nektos/act提供了包含完整开发工具链的镜像,运行命令时指定该镜像即可:
act -P ubuntu-latest=ghcr.io/catthehacker/ubuntu:full-latest
内容的提问来源于stack exchange,提问作者Rasbypy
相关产品推荐
相关产品推荐

