在Linux中使用Jenkins時, 在jenkinsFile中添加了sh 'mvn --version'
命令,但Jenkins在build時報錯以下:java
> git rev-list --no-walk 75df912a72c7971f61d5c48b80b05bdc1243f86d # timeout=10 [Pipeline] } [Pipeline] // stage [Pipeline] withEnv [Pipeline] { [Pipeline] stage [Pipeline] { (build) [Pipeline] sh + mvn --version /var/lib/jenkins/workspace/SpringbootDeployment_master@tmp/durable-65e0b685/script.sh: line 1: mvn: command not found [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline GitHub has been notified of this commit’s build result ERROR: script returned exit code 127 Finished: FAILURE
仔細查了一下, 發現了問題的緣由. 我對於java或maven的路徑的環境變量是放在/etc/profile中的, 而/etc/profile只有在用戶登陸的時候纔會被load,Jenkins在運行命令時,使用的是Non-login的方式,而這種方式在運行命令時,/etc/profile是不會被load進來的,因此jenkins只能在當前路徑下尋找可執行文件.node
解決方式:
在Jenkins的設置中能夠設置全局變量, jenkins主頁面->Manage Jenkins->Configure System->Global Properties 中, 將Environment variables複選框選中,會出來List of variables, 填入如下內容:git
PATH+EXTRA
value: $M2_HOME/bin注意最後標紅的 PATH+EXTRA
, 這表示PATH=EXTRA:$PATH, 即擴展當前的PATH變量.
設置以後build成功.apache