Jenkins构建Maven项目

2023-04-24  本文已影响0人  小波同学

一、Jenkins项目构建类型

1.1 Jenkins项目构建类型介绍

Jenkins中自动构建项目的类型有很多,常用的有如下的三种:

每种类型的构建其实都可以完成一样的构建过程和结果,只是在操作方式、灵活度等方面有所区别,在实际开发中可以根据自己的需求和习惯灵活选择(强烈推荐流水线类型,因为灵活度非常高)。

1.2 自由风格项目构建

1.2.1 安装Publish over SSH插件

1.2.2 配置Publish over SSH

目的

1.2.3 创建项目

1.2.4 配置源码管理,从Gitlab上拉取代码

目的:配置项目拉取地址和凭证

1.2.5 编译打包

echo "构建开始"
mvn clean install -Dmaven.test.skip=true
echo "构建成功"

目的:将拉取到的项目,通过maven工具打包安装

1.2.6 部署

将SpringBoot生成的jar包发送到远程服务器。
在远程服务器192.168.18.102新建stop.sh和start.sh文件:

cd /usr/local
vim stop.sh
#!/bin/bash
echo "stop SpringBoot BAppApiServerApplication"
pid=`ps -ef | grep java | grep -v grep | awk '{print $2}'`
echo “旧应用进程id:$pid”
if [ -n "$pid" ]
then
kill -9 $pid
rm -rf springboot2-1.0.jar
fi
vim start.sh
#!/bin/bash
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.9.11-2.el7_9.x86_64
echo ${JAVA_HOME}
echo "授权当前用户"
chmod 777 /usr/local/springboot2-1.0.jar
echo "执行...."
cd /usr/local/
nohup ${JAVA_HOME}/bin/java -jar springboot2-1.0.jar > /dev/null &
echo "启动成功"

目的:在远程服务器上配置好停止和启动项目的脚本。

cd /usr/local
chmod 777 *.sh
./stop.sh
./start.sh

目的:配置发布远程服务器的相关信息,发布到的目标路径,以及发布后要执行的脚本。

ERROR: Error fetching remote repo 'origin'
hudson.plugins.git.GitException: Failed to fetch from ssh://git@gitlab.lucy.com:2222/lucy/test.git
    at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:888)
    at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1155)
    at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1186)
    at org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:120)
    at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:90)
    at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:77)
    at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution$1$1.call(SynchronousNonBlockingStepExecution.java:50)
    at hudson.security.ACL.impersonate(ACL.java:290)
    at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution$1.run(SynchronousNonBlockingStepExecution.java:47)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: hudson.plugins.git.GitException: Command "git fetch --tags --progress ssh://git@gitlab.lucy.com:2222/lucy/test.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout: 
stderr: Host key verification failed.
fatal: Could not read from remote repository.
 
Please make sure you have the correct access rights
and the repository exists.
 
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2016)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:1735)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$300(CliGitAPIImpl.java:72)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:420)
    at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:886)
    ... 13 more

解决方案
删除Jenkins workspace下的工程后重新构建。

1.3 Maven项目构建

1.4 Pipeline流水线项目构建(强烈推荐)

1.4.1 Pipeline的概念

1.4.2 Pipeline的好处

1.4.3 如何创建Jenkins的Pipeline?

1.4.4 安装Pipeline插件

1.4.5 Pipeline语法快速入门之Declarative声明式

生成的内容如下:

pipeline {
    agent any
    
    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

stages:代表整个流水线的所有执行阶段。通常而言,stages只有1个,里面包含多个stage。
stage:代表流水线中的某个阶段,可能会出现多个。一般分为拉取代码、编译构建、部署等阶段。
steps:代表一个阶段内需要执行的逻辑。steps里面是shell脚本,git拉取代码,ssh远程发布等任意内容。

编写一个简单声明式的Pipeline:

pipeline {
    agent any

    stages {
        stage('拉取代码') {
            steps {
                echo '拉取代码'
            }
        }
        stage('编译构建') {
            steps {
                echo '编译构建'
            }
        }
        stage('项目部署') {
            steps {
                echo '项目部署'
            }
        }
    }
}

1.4.6 Pipeline语法快速入门之脚本式

生成的内容如下:

node {
    def mvnHome
    stage('Preparation') { // for display purposes
        // Get some code from a GitHub repository
        git 'https://github.com/jglick/simple-maven-project-with-tests.git'
        // Get the Maven tool.
        // ** NOTE: This 'M3' Maven tool must be configured
        // **       in the global configuration.
        mvnHome = tool 'M3'
    }
    stage('Build') {
        // Run the maven build
        withEnv(["MVN_HOME=$mvnHome"]) {
            if (isUnix()) {
                sh '"$MVN_HOME/bin/mvn" -Dmaven.test.failure.ignore clean package'
            } else {
                bat(/"%MVN_HOME%\bin\mvn" -Dmaven.test.failure.ignore clean package/)
            }
        }
    }
    stage('Results') {
        junit '**/target/surefire-reports/TEST-*.xml'
        archiveArtifacts 'target/*.jar'
    }
}

node:节点,一个Node就是一个Jenkins节点,Master或者Agent,是执行Step的具体运行环境。
Stage:阶段,一个Pipeline可以划分为若干个Stage,每个Stage代表一组操作,比如Build、Test、Deploy、State是一个逻辑分组的概念。
Step:步骤,Step是最基本的操作单元,可以是打印一句话,也可以是构建一个Docker镜像,由各类Jenkins插件提供。比如命令:sh ‘make’,相当于我们平时Shell终端中执行的make命令一样。

node {
    def mvnHome
    stage('拉取代码') { 
       echo '拉取代码'
    }
    stage('编译构建') {
         echo '编译构建'
    }
    stage('项目部署') {
        echo '项目部署'
    }
}

1.4.7 拉取代码

拉取代码的流水线的声明式代码:

pipeline {
    agent any

    stages {
        stage('拉取代码') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '7d5c4945-2533-41e2-bd47-5dd97eb37f38', url: 'git@192.168.18.100:develop_group/springboot2.git']]])
            }
        }
        
    }
}

1.4.8 编译打包

编译打包的流水线的声明式代码:

pipeline {
    agent any

    stages {
        stage('拉取代码') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '7d5c4945-2533-41e2-bd47-5dd97eb37f38', url: 'git@192.168.18.100:develop_group/springboot2.git']]])
            }
        }
        stage('编译打包') {
            steps {
                sh '''echo "开始构建"
                mvn clean install -Dmaven.test.skip=true
                echo "构建结束"'''
            }
        }
                
    }
}

1.4.9 远程部署

远程部署的流水线的声明式代码:

pipeline {
    agent any

    stages {
        stage('拉取代码') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '7d5c4945-2533-41e2-bd47-5dd97eb37f38', url: 'git@192.168.18.100:develop_group/springboot2.git']]])
            }
        }
        stage('编译打包') {
            steps {
                sh '''echo "开始构建"
                mvn clean install -Dmaven.test.skip=true
                echo "构建结束"'''
            }
        }
        stage('远程部署') {
            steps {
                sshPublisher(publishers: [sshPublisherDesc(configName: '192.168.18.102', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''cd /usr/local
chmod 777 *.sh
./stop.sh
./start.sh''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: 'target', sourceFiles: 'target/*.jar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
            }
        }
                
    }
}

1.4.10 Pipeline Script from SCM(强烈推荐)

二、构建细节

2.1 常用的构建触发器

2.1.1 概述

2.1.2 触发远程构建

2.1.3 其他工程构建后触发

2.1.4 定时构建

一些定时表达式的例子(H代表形参 ):
每30分钟构建一次: H/30 10:02 10:32
每2个小时构建一次: H H/2 *
每天的8点,12点,22点,一天构建3次: (多个时间点中间用逗号隔开) 0 8,12,22 *
每天中午12点定时构建一次:H 12 *
每天下午18点定时构建一次: H 18 *
在每个小时的前半个小时内的每10分钟: H(0-29)/10
每两小时一次,每个工作日上午9点到下午5点(也许是上午10:38,下午12:38,下午2:38,下午 4:38) :H H(9-16)/2 1-5

2.1.5 轮询SCM(不建议)

注意:这种构建触发器,Jenkins会定时扫描本地整个项目的代码,增大系统的开销,不建议使用。

2.2 Git Hook自动触发构建

2.2.1 概述

2.2.2 安装Gitlab Hook和Gitlab插件

2.2.3 Jenkins设置自动构建

2.2.4 Gitlab开启webhook功能

2.2.5 在项目中添加webhook

2.2.6 Jenkins取消Enable authentication for “/project” end-point

2.3 Jenkins的参数化构建

2.3.1 Jenkins的参数化构建概述

2.3.2 使用Gitlab的分支名称来部署不同的分支项目

pipeline {
    agent any

    stages {
        stage('拉取代码') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '7d5c4945-2533-41e2-bd47-5dd97eb37f38', url: 'git@192.168.18.100:develop_group/springboot2.git']]])
            }
        }
        stage('编译打包') {
            steps {
                sh '''echo "开始构建"
                mvn clean install -Dmaven.test.skip=true
                echo "构建结束"'''
            }
        }
        stage('远程部署') {
            steps {
                sshPublisher(publishers: [sshPublisherDesc(configName: '192.168.18.102', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''cd /usr/local
chmod 777 *.sh
./stop.sh
./start.sh''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: 'target', sourceFiles: 'target/*.jar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
            }
        }

    }
}

“${branch}”是重点。

2.4 配置邮箱服务器发送构建结果

2.4.1 需要安装Email Extension Template插件

2.4.2 Jenkins设置邮箱参数

2.4.3 准备邮箱内容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次构建日志</title>
</head>

<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4"
      offset="0">
<table width="95%" cellpadding="0" cellspacing="0"
       style="font-size: 11pt; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, 'PingFang SC', Cambria, Cochin, Georgia, Times, 'Times New Roman', serif">
    <tr>
        <td>(本邮件是程序自动下发的,请勿回复!)</td>
    </tr>
    <tr>
        <td><h2>
            <font color="#0000FF">构建结果 - ${BUILD_STATUS}</font>
        </h2></td>
    </tr>
    <tr>
        <td><br />
            <b><font color="#0B610B">构建信息</font></b>
            <hr size="2" width="100%" align="center" /></td>
    </tr>
    <tr>
        <td>
            <ul>
                <li>项目名称&nbsp;:&nbsp;${PROJECT_NAME}</li>
                <li>构建编号&nbsp;:&nbsp;第${BUILD_NUMBER}次构建</li>
                <li>触发原因:&nbsp;${CAUSE}</li>
                <li>构建日志:&nbsp;<a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
                <li>构建&nbsp;&nbsp;Url&nbsp;:&nbsp;<a href="${BUILD_URL}">${BUILD_URL}</a></li>
                <li>工作目录&nbsp;:&nbsp;<a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li>
                <li>项目&nbsp;&nbsp;Url&nbsp;:&nbsp;<a href="${PROJECT_URL}">${PROJECT_URL}</a></li>
            </ul>
        </td>
    </tr>
    <tr>
        <td><b><font color="#0B610B">Changes Since Last
            Successful Build:</font></b>
            <hr size="2" width="100%" align="center" /></td>
    </tr>
    <tr>
        <td>
            <ul>
                <li>历史变更记录 : <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a></li>
            </ul> ${CHANGES_SINCE_LAST_SUCCESS,reverse=true, format="Changes for Build #%n:<br />%c<br />",showPaths=true,changesFormat="
<pre>[%a]<br />%m</pre>",pathFormat="&nbsp;&nbsp;&nbsp;&nbsp;%p"}
        </td>
    </tr>
    <tr>
        <td><b>Failed Test Results</b>
            <hr size="2" width="100%" align="center" /></td>
    </tr>
    <tr>
        <td><pre
                style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">$FAILED_TESTS</pre>
            <br /></td>
    </tr>
    <tr>
        <td><b><font color="#0B610B">构建日志 (最后 100行):</font></b>
            <hr size="2" width="100%" align="center" /></td>
    </tr>
    <tr>
        <td><textarea cols="80" rows="30" readonly="readonly"
                      style="font-family: Courier New">${BUILD_LOG, maxLines=100}</textarea>
        </td>
    </tr>
</table>
</body>
</html>

2.4.4 编写Jenkinsfile添加构建后发送邮箱

pipeline {
    agent any

    stages {
        stage('拉取代码') {
            steps {
               checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'bacbbbb1-2df9-470d-adf8-5cb6dc496807', url: 'git@192.168.209.100:develop_group/springboot2.git']]])
            }
        }
        stage('编译构建') {
            steps {
                sh '''echo "开始构建"
                mvn clean install -Dmaven.test.skip=true
                echo "构建结束"'''
            }
        }
        stage('项目部署') {
            steps {
                sshPublisher(publishers: [sshPublisherDesc(configName: '192.168.209.102', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''#!/bin/bash
                echo "stop SpringBoot BAppApiServerApplication"
                pid=`ps -ef | grep java | grep -v grep | awk \'{print $2}\'`
                echo “旧应用进程id:$pid”
                if [ -n "$pid" ]
                then
                kill -9 $pid
                rm -rf springboot2-1.0.jar
                fi
                export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.9.11-2.el7_9.x86_64
                echo ${JAVA_HOME}
                echo "授权当前用户"
                chmod 777 /usr/local/springboot2-1.0.jar
                echo "执行...."
                cd /usr/local/
                nohup ${JAVA_HOME}/bin/java -jar springboot2-1.0.jar > /dev/null &
                echo "启动成功"

                ''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: 'target', sourceFiles: 'target/*.jar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
            }
        }
    }
    post {
      always {
         emailext (
             subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!',
             body: '${FILE,path="email.html"}',
             to: 'xxx@qq.com'
         )
      }
    }
}

三、Jenkins+SonarQube代码审查

3.1 概述

软件 服务器 版本
JDK 192.168.18.101 11
PostgreSQL 192.168.18.101 5.7
SonarQube 192.168.18.101-8.6.0 8.6.0

3.2 安装SonarQube

3.2.1 安装PostgreSQL(也可以安装Mysql数据库)

# 用户名是 postgres  ,密码是123456
docker run --name postgres -v dv_pgdata:/var/lib/postgresql/data --restart=always -e POSTGRES_PASSWORD=123456 -p 5432:5432 -d postgres:12.1

在PostgreSQL中新建sonar数据库:

CREATE DATABASE sonar;

3.2.2 安装SonarQube

在PostgreSQL中新建sonar数据库:

CREATE DATABASE sonar;

下载SonarQube:

wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-8.6.0.39681.zip

解压sonar:

unzip -d /usr/local/ sonarqube-8.6.0.39681.zip
cd /usr/local
mv sonarqube-8.6.0.39681 sonarqube-8.6.0

创建用户,用于设置权限:

# 创建sonar用户,sonar不能用root启动,否则报错
useradd sonar
passwd sonar
chown -R sonar /usr/local/sonarqube-8.6.0

修改sonar的配置文件:

vim /usr/local/sonarqube-8.6.0/conf/sonar.properties
# 内容如下
sonar.jdbc.username=postgres 
sonar.jdbc.password=123456
sonar.jdbc.url=jdbc:postgresql://localhost:5432/sonar

sonar默认自带了ES,所以需要修改配置,防止启动报错:

vim /etc/security/limits.conf
# 追加内容
* soft nofile 65536
* hard nofile 65536
* soft nproc 4096
* hard nproc 4096
vim /etc/security/limits.d/90-nproc.conf
# 追加内容
* soft nproc 4096
vim /etc/sysctl.conf
# 追加内容
vm.max_map_count=655360
sysctl -p
reboot

启动sonar(sonar的默认端口是9000):

cd /usr/local/sonarqube-8.6.0/
# 启动
su sonar ./bin/linux-x86-64/sonar.sh start
# 查看状态
su sonar ./bin/linux-x86-64/sonar.sh status
# 停止
su sonar ./bin/linux-x86-64/sonar.sh stop
# 查看日志
tail -f logs/sonarxxx.logs

访问sonar:http://192.168.18.101:9000。


比如我此时的令牌是:1463a2ebf240a0c77bd9c965f76006fe03d166a6。

3.3 实现代码审查

3.3.1 概述

3.3.2 安装SonarQube Scanner插件

3.3.3 安装SonarQube Scanner

3.3.4 Jenkins配置Sonar Server

3.3.5 非流水线项目添加SonarQube代码审查

配置如下:

# must be unique in a given SonarQube instance
sonar.projectKey=springboot2
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 8.6.0
sonar.projectName=springboot2
sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set.
sonar.sources=.
sonar.exclusions=**/test/**,**/target/**
sonar.java.source=11
sonar.java.target=11
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
sonar.java.binaries=**target/classes

3.3.6 流水线项目添加SonarQube代码审查

在项目的根目录中添加sonar-project.properties文件,内容如下:

# must be unique in a given SonarQube instance
sonar.projectKey=springboot2
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 8.6.0
sonar.projectName=springboot2
sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set.
sonar.sources=.
sonar.exclusions=**/test/**,**/target/**
sonar.java.source=11
sonar.java.target=11
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
sonar.java.binaries=**target/classes

Jenkinsfile:

pipeline {
    agent any

    stages {
        stage('拉取代码') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '7d5c4945-2533-41e2-bd47-5dd97eb37f38', url: 'git@192.168.18.100:develop_group/springboot2.git']]])
            }
        }
        stage('编译打包') {
            steps {
                sh '''echo "开始构建"
                mvn clean install -Dmaven.test.skip=true
                echo "构建结束"'''
            }
        }
        stage('代码检查') {
            steps {
                script {
                   // 引入SonarQubeScanner工具
                   scannerHome = tool 'sonarqube-scanner'
                }
                // 引入了 SonarQube服务器的环境
                withSonarQubeEnv('sonarqube-8.6.0') {
                   sh "${scannerHome}/bin/sonar-scanner"
                }
            }
        }
        stage('远程部署') {
            steps {
                sshPublisher(publishers: [sshPublisherDesc(configName: '192.168.18.102', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''cd /usr/local
chmod 777 *.sh
./stop.sh
./start.sh''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: 'target', sourceFiles: 'target/*.jar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
            }
        }

    }
}

参考:
https://blog.csdn.net/weixin_41979002/article/details/121706985

https://cjdhy.blog.csdn.net/article/details/126578748

上一篇 下一篇

猜你喜欢

热点阅读