Jenkins Pipeline+Maven+Gitlab持续集成构建问题集锦

问题

1、全局配置一定要写正确,之前where git 给的地址时E:Gitcmdgit.exe一直报错,其实Windows真正的git.exe在bin目录下

  如果是Linux中,使用whereis git查看git安装地址,一般都在/usr/bin/git

2、Windows环境的pipeline调用mvn 需要写成这样

 bat 'D:\chromeDownload\apache-maven-3.6.0-bin\apache-maven-3.6.0\bin\mvn install'

node {

    stage('Pull Code'){
        
        git branch: 'master', credentialsId: 'gitlab_account', url: 'http://********.git'
        
    }
    
   stage('Code Check') {
            echo "敬请期待!"
         }

   stage('Unit Test') {
         echo "敬请期待!"
      }
     

   stage('Compile') {
       echo '编译开始'
       bat 'D:\chromeDownload\apache-maven-3.6.0-bin\apache-maven-3.6.0\bin\mvn install'
       
    
   }

   stage('Build Images') {
        echo '敬请期待!'
      
    }

    stage('Backup'){
        echo '旧版本软件包已经备份'
    }

    stage('Deploy'){
       echo '敬请期待!'
    }

    stage('Integration Testing') {
          echo "敬请期待!"
       }

    stage('UI Automated Testing') {
          echo "敬请期待!"
       }

    stage('Send Testing Report') {
          echo "敬请期待!"
       }

}

Linux下脚本

node {


    stage('Pull Code'){
        
        git branch: 'master', credentialsId: 'git_account', url: 'http://********.git'
        
    }
    
   stage('Code Check') {
            echo "代码检查!"
         }

   stage('Unit Test') {
         echo "单元测试 !"
      }
     

   stage('Compile') {
        echo '编译开始'
        sh "/var/tools/apache-maven-3.3.9/bin/mvn install"
   }

   stage('Build Images') {
        echo '敬请期待!'
      
    }

    stage('Backup'){
        echo '旧版本软件包已经备份'
    }
    
    stage('Delete Old war'){
        echo "删除老的包"
    }
    

    stage('Deploy'){
       echo '部署!'
    }

    stage('Integration Testing') {
          echo "敬请期待!"
       }

    stage('UI Automated Testing') {
          echo "敬请期待!"
       }

    stage('Send Testing Report') {
          echo "敬请期待!"
       }

}

3、出现 fatal: Authentication failed for 'http://***.git/'

ERROR: Error cloning remote repo 'origin'
hudson.plugins.git.GitException: Command "/usr/bin/git fetch --tags --progress http*****.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout: 
stderr: remote: HTTP Basic: Access denied
fatal: Authentication failed for 'http://***.git/'

刚开始怀疑是执行Jenkins机器没有把公钥放在gitlab上,其实不是这个原因。

明显是权限账号问题,检查凭证,是不是账号密码输入错误了,或者pipeline中的脚本 credentialsId 写错了(我就犯了这么低级的错误,哈哈哈)

  

 4、出现下图not found

[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Code Check)
[Pipeline] echo
代码检查!
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Unit Test)
[Pipeline] echo
单元测试 !
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Compile)
[Pipeline] echo
编译开始
[Pipeline] sh
+ /home/tools/apache-maven-3.3.9/bin/mvn install
/var/jenkins_home/workspace/wechatcapi@tmp/durable-4f4430f6/script.sh: 1: /var/jenkins_home/workspace/wechatcapi@tmp/durable-4f4430f6/script.sh: /home/tools/apache-maven-3.3.9/bin/mvn: not found
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

原因:是因为缺少插件:Pipeline Maven Integration (此原因已排除,读者忽略,仅做记录 )

实际原因:pipeline中脚本要指定容器中mvn地址,而不是宿主机的

stage('Compile') {
echo '编译开始'
sh "/var/tools/apache-maven-3.3.9/bin/mvn clean"
}

这里用了容器的mvn但是容器中还缺少环境变量配置:

vi /etc/profile 又提示:

bash: vi: command not found

安装vi 又提示没有权限

jenkins@443d62de8949:/var/tools$ apt-get install vi
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

解决方法:进入容器时使用root账户进入

sudo docker exec -it -u root e36f /bin/bash

先更新源:

apt-get update

apt-get upgrade

再安装vi

apt-get install vi

如果安装vi不行,那就安装vim

apt-get install vim

5、settings.xml文件配置放置位置

  可以看到有两个.m2目录,正确的地方是/var/jenkins_home/.m2这个目录下放置settings.xml文件,同时记得修改settings文件中地址:<localRepository>/var/jenkins_home/.m2</localRepository>

root@e36f8029c9f2:~/.m2# find / -name .m2
/root/.m2
/var/jenkins_home/.m2
原文地址:https://www.cnblogs.com/gcgc/p/10190614.html