Gitlab CI/CD runner : mvn command not found
Asked Answered
M

7

10

Maven is well installed on my gitlab-runner server. When executing mvn clean directly on my repo it works, when running my pipeline using Gitlab UI got this error :

bash: line 60: mvn: command not found

ERROR: Job failed: exit status 1

I notice that I tried to fix the problem by adding the before_script section in the .gitlab-ci.yml file :

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

I add also the line :

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

on the config.toml file.

the problem still persist, my executor is : shell.

Any advice!

Margotmargrave answered 27/9, 2018 at 11:19 Comment(0)
M
3

I managed to fix the problem using this workaround:

  script:
    - $MAVEN_HOME/bin/mvn clean
Margotmargrave answered 27/9, 2018 at 13:45 Comment(0)
N
5

Just add the maven docker image, add below line as first line:

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

refer: https://docs.gitlab.com/ee/ci/examples/artifactory_and_gitlab/

Neurosis answered 1/1, 2019 at 12:31 Comment(0)
M
3

I managed to fix the problem using this workaround:

  script:
    - $MAVEN_HOME/bin/mvn clean
Margotmargrave answered 27/9, 2018 at 13:45 Comment(0)
C
3

There is an easier way:
Making changes in ~/.bash_profile not ~/.bashrc.


According to this document:

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

This document saying:

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

So it should not be ~/.bashrc, you can also try ~/.profile which It can hold the same configurations, which are then also accessible by other shells


In my scenario I do following things:

1. Set gitlab-runner's user password.

passwd gitlab-runner

2. Login gitlab-runner.

su - gitlab-runner

3. Make changes in .bash_profile

Chadwell answered 10/9, 2021 at 1:22 Comment(1)
This works for my use case. Just adding $HOME/.local/bin to the path and it wasn't working in .bashrc. Switched to .bash_profile and it works a charm.Origin
D
0

Add maven to PATH:

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

$ export M2=$M2_HOME/bin

$ export PATH=$M2:$PATH 

You can include these commands in $HOME/.bashrc

Deandre answered 27/9, 2018 at 11:56 Comment(0)
C
0

I hope you had figure out your question. I met the same question when I build my ci on my server.

I use the shell as the executer for my Runner. here are the steps to figure out.

1 check the user on the runner server

if you had install maven on the runner server successfully, maybe it just successful for the root, u can check the real user for the ci process.

job1:
  stage: test
  script:  whoami

if my case, it print gitlab-runner, not the root

2 su the real user, check mvn again

In this time, it print error as same as the Gitlab ci UI.

3 install maven for the real user. run the pipeline again.

Copolymerize answered 1/3, 2019 at 6:13 Comment(0)
W
0

For anyone experiencing similar issues, it might be a good idea to restart the gitlab runner ".\gitlab-runner.exe restart". Especially after fiddling with environmental variables.

Willow answered 2/2, 2021 at 16:17 Comment(0)
F
0

You can also use as per below in the .gitlab-ci.yml

before_script:
  - export PATH=$PATH:/opt/apache-maven-3.8.1/bin
Formation answered 16/2, 2022 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.