Mvn 未被识别为 Github 上的命令 Windows 自托管运行器上的操作

Mvn is not recognized as a command on Github Actions on Windows a self hosted runner

我设置了一个简单的 Github Actions 工作流,只是为了检查我的自托管 Windows 虚拟机是否能够识别安装的 java/git/maven 版本。

虚拟机中的版本

Java 和 Git 被正确识别但是检查 maven 版本的步骤失败:

mvn -version

失败并显示以下日志:

mvn : The term 'mvn' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the 
spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\action-runner\_work\_tempb2e64a-9586-4937-b61c-90e0bb0a5d9c.ps1:2 char:1
+ mvn -version
+ ~~~
    + CategoryInfo          : ObjectNotFound: (mvn:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : CommandNotFoundException
 
Error: Process completed with exit code 1.

下面我是如何在机器上设置环境变量的:

Environment Variable Screenshot

在机器上 mvn 命令被识别没有任何问题,问题只发生在 运行 Github 工作流 yaml 时。 在 .yaml 脚本下面也有不同的尝试从脚本本身设置环境变量:

name: CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]


  workflow_dispatch:


jobs:
  build:
    runs-on: [self-hosted, selenium-1]

    steps:

      - uses: actions/checkout@v3

      # Attempt 1: FAILS with mvn not recognized log
      - name: Check Maven version
        run: mvn -version

      # Attempt 2: DOES NOT FAIL but it also doesn't do anything (tried other mvn commands and nothing happened)
      - name: Check Maven Version
        run: C:\action-runner\apache-maven-3.8.5\bin\mvn -version

      # Attempt 3 set Maven as env variable with powershell and FAILS with mvn not recognized log
      - name: Set maven path as Env variable
        run: $Env:GITHUB_ENV += ';C:\action-runner\apache-maven-3.8.5\bin'

      # after Attempt 3 launched the mvn command to check if it worked but still failed
      - name: Check Maven version
        run: mvn -version

      # Attempt 4 try to set PATH variable once in the workflow with following log:
      # Error: Unable to process file command 'env' successfully.
      # Error: Invalid environment variable format 'C:\action-runner\apache-maven-3.8.5\bin'
      - name: set PATH Maven
        run: echo "C:\action-runner\apache-maven-3.8.5\bin" >> $env:GITHUB_ENV

提前感谢任何提出解决方案的人。

我只是 运行 加入了 python。我已经在 windows 安装程序设置中为所有用户安装了它。我会尝试为所有用户安装,然后该服务应该在范围内有 maven。

通过在 .yml 工作流文件中添加 GitHub Marketplace 中的此操作解决:

https://github.com/stCarolas/setup-maven https://github.com/marketplace/actions/setup-maven