Jenkins管道制造商:未找到命令

sqougxex  于 2023-01-01  发布在  Jenkins
关注(0)|答案(3)|浏览(186)

我的测试作为Maven项目运行良好,它从Git中获取代码。
我想在管道中执行相同的操作,所以我为管道项目编写了以下脚本。

pipeline {
    agent any
    options {
        timestamps()
    }
    stages{ 
        stage('Just Testing') {
            steps {
                git "https://github.com/myRepo.git"
                script{
                        sh 'mvn test'
                }
                step([$class : 'Publisher', reportFilenamePattern : "**/testng-results.xml"])
            }
        }
    }   
}

但是当我执行时,我得到mvn:未找到命令错误全局工具配置中正确设置了Java和Maven路径(我可以肯定,因为我能够执行其他Maven项目,只有pipeline失败。)
控制台输出:

Running on Jenkins in /Users/abacker/.jenkins/workspace/parallelTestNG
[Pipeline] {
[Pipeline] timestamps
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Just Testing)
[Pipeline] git
00:03:52  No credentials specified
00:03:52   > git rev-parse --is-inside-work-tree # timeout=10
00:03:52  Fetching changes from the remote Git repository
00:03:52   > git config remote.origin.url https://github.com/myRepo.git # timeout=10
00:03:52  Fetching upstream changes from https://github.com/myRepo.git
00:03:52   > git --version # timeout=10
00:03:52   > git fetch --tags --force --progress -- https://github.com/myRepo.git +refs/heads/*:refs/remotes/origin/* # timeout=10
00:03:54   > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
00:03:54   > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
00:03:54  Checking out Revision e82f48106aa72d0275591926d9cd3226ba0fefd7 (refs/remotes/origin/master)
00:03:54   > git config core.sparsecheckout # timeout=10
00:03:54   > git checkout -f e82f48106aa72d0275591926d9cd3226ba0fefd7 # timeout=10
00:03:54   > git branch -a -v --no-abbrev # timeout=10
00:03:54   > git branch -D master # timeout=10
00:03:54   > git checkout -b master e82f48106aa72d0275591926d9cd3226ba0fefd7 # timeout=10
00:03:54  Commit message: "First commit"
00:03:54   > git rev-list --no-walk e82f48106aa72d0275591926d9cd3226ba0fefd7 # timeout=10
[Pipeline] script
[Pipeline] {
[Pipeline] sh
00:03:54  + mvn test
00:03:54  /Users/abacker/.jenkins/workspace/parallelTestNG@tmp/durable-818e64a8/script.sh: line 1: mvn: command not found
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // timestamps
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

如何解决此问题?

o2gm4chl

o2gm4chl1#

您没有为maven项目声明任何工具。

这将是运行Jenkins. Declarative maven project中的mvn目标的一个很好的起点。

ha5z0ras

ha5z0ras2#

下面的pipeline脚本完成了我的工作。与在jenkins中创建的maven项目不同,您需要添加maven和java作为脚本的工具。MAVEN_HOME -在“管理Jenkins”中配置→“全局工具配置”JAVA_HOME -在“管理Jenkins”中配置→“全局工具配置”

pipeline {
    agent any
    tools { 
      maven 'MAVEN_HOME' 
      jdk 'JAVA_HOME' 
    }
    stages {
      stage ('Build') {
        steps {
          sh 'mvn -B -ntp -Dmaven.test.failure.ignore verify'
        }
      }

    }
}
3hvapo4f

3hvapo4f3#

在管道脚本中,您必须为maven指定如下路径

def mvnHome = tool name: 'maven-3.8.6', type: 'maven'
                    sh "${mvnHome}/bin/mvn package"

处于Maven阶段
我希望它能起作用,因为它对我很有效。谢谢

相关问题