Gitlab CI/CD runner:找不到 mvn 命令

Gitlab CI/CD runner : mvn command not found

Maven 很好地安装在我的 gitlab-runner 服务器上。当直接在我的 repo 上执行 mvn clean 它工作时,当 运行 我的管道使用 Gitlab UI 得到这个错误:

bash: line 60: mvn: command not found

ERROR: Job failed: exit status 1

我注意到我试图通过在 .gitlab-ci.yml 文件中添加 before_script 部分来解决问题:

before_script:    
- export MAVEN_HOME=/usr/local/apache-maven

我还添加了行:

environment = ["MAVEN_HOME=/usr/local/apache-maven"]

config.toml 文件上。

问题依旧,我的执行人是:shell.

任何建议!

将 maven 添加到 PATH:

$ export M2_HOME=/usr/local/apache-maven/apache-maven-3.3.9

$ export M2=$M2_HOME/bin

$ export PATH=$M2:$PATH 

您可以在 $HOME/.bashrc

中包含这些命令

我设法使用此解决方法解决了问题:

  script:
    - $MAVEN_HOME/bin/mvn clean

只需添加 maven docker 图像,添加下面一行作为第一行:

image: maven:latest or image: maven:3-jdk-10 or image: maven:3-jdk-9

参考:https://docs.gitlab.com/ee/ci/examples/artifactory_and_gitlab/

我希望你已经弄明白你的问题了。我在服务器上构建 ci 时遇到了同样的问题。

我使用 shell 作为我的 Runner 的执行器。 这是要弄清楚的步骤。

1 检查 运行ner 服务器

上的用户

如果你在 运行ner 服务器上成功安装了 maven,也许只是对 root 成功,你可以检查 ci 进程的真实用户。

job1:
  stage: test
  script:  whoami

如果是我的情况,它会打印 gitlab-runner,而不是 root

2 su真实用户,再次查看mvn

此时,和Gitlab一样打印错误ci UI。

3 为真实用户安装maven。 运行再次管道。

对于遇到类似问题的任何人,重启 gitlab runner“.\gitlab-runner.exe restart”可能是个好主意。特别是在摆弄环境变量之后。

有一个更简单的方法:
~/.bash_profile 而不是 ~/.bashrc 中进行更改。


根据this document

.bashrc it is more common to use a non-login shell

This document 说:

For certain executors, the runner passes the --login flag as shown above, which also loads the shell profile.

所以应该不是~/.bashrc,你也可以试试~/.profile哪个 It can hold the same configurations, which are then also accessible by other shells


在我的场景中,我做了以下事情:

1。设置gitlab-runner的用户密码。

passwd gitlab-runner

2。登录 gitlab-runner.

su - gitlab-runner

3。在 .bash_profile

中进行更改

您也可以在 .gitlab-ci.yml

中按以下方式使用
before_script:
  - export PATH=$PATH:/opt/apache-maven-3.8.1/bin