为什么在此声明性jenkins管道脚本中找不到mvn命令?

时间:2019-02-01 08:56:30

标签: maven jenkins jenkins-pipeline

我从https://jenkins.io/doc/book/pipeline/syntax/#tools那里得到了这个例子

管道脚本:

pipeline {
    agent any
    tools {
        maven 'apache-maven-3.3.9' 
    }
    stages {
        stage('Example') {
            steps {
                sh 'mvn --version'
            }
        }
    }
}

apache-maven-3.3.9在我的全局工具配置中定义。

脚本的输出为

Started by user *************
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on SRV-CI-MADE-UNIX in /home/jenkins/workspace/*************
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Tool Install)
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Example)
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ mvn --version
/home/jenkins/workspace/*************@tmp/durable-d4209a18/script.sh: line 1: mvn: command not found
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
[BFA] Scanning build for known causes...
[BFA] No failure causes found
[BFA] Done. 0s
ERROR: script returned exit code 127
Finished: FAILURE

为什么找不到mvn命令?

我知道显然还有其他方法,但这是Jenkins文档本身的示例...

1 个答案:

答案 0 :(得分:0)

这是正确的示例(我也对其进行了测试),但是如果您只有master节点,那么它可以正常工作。

我认为您刚刚在Global Tool Configuration的{​​{1}}节点上配置了Maven工具,但是我看到您在master节点上执行了工作,并且对于该节点还提供正确的Maven配置。

因此,要解决此问题,我想您需要执行以下步骤:

  1. 在为其配置了Maven工具的管道配置(例如SRV-CI-MADE-UNIXagent any)中提供正确的代理(而不是master);
  2. SRV-CI-MADE-UNIX中配置的目录中的指定节点上安装Maven或在Global Tool Configuration中为此节点添加特定的Maven工具位置