美文网首页
哥哥带你学Jenkins自动化打包二(持续更新中……)

哥哥带你学Jenkins自动化打包二(持续更新中……)

作者: 哥哥是欧巴Vitory | 来源:发表于2019-04-20 11:48 被阅读0次

登录 sonar

Web登录:IP:9000

sonar跟jenkins类似,也是以插件为主。

sonar安装插件有2种方式:第一种将插件下载完存放在sonar的插件目录,第二种使用web界面来使用安装存放插件路径[/usr/local/sonarqube/extensions/plugins/]。

1、安装中文插件

登陆:用户名:admin 密码:admin。

重启才会生效。

生效后界面如下。

和 jenkins 一样,需要什么插件直接去应用市场下载即可。

2、安装 sonar 扫描器

Sonar通过SonarQube Scanner(扫描器)来对代码进行分析。

wgethttps://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.8.zipunzip sonar-scanner-2.8.zipmv sonar-scanner-2.8/usr/local/sonar-scanner

3、扫描器和 sonar 关联起来

编辑文件/usr/local/sonar-scanner/conf/sonar-scanner.properties,修改为如下内容:

sonar.host.url=http://localhost:9000        sonar.sourceEncoding=UTF-8

代码扫描

1、下载代码

sonar 插件提供了一个测试的代码库。

github:https://github.com/SonarSource/sonar-scanning-examples

下载地址:https://codeload.github.com/SonarSource/sonar-scanning-examples/zip/master

wgethttps://codeload.github.com/SonarSource/sonar-scanning-examples/zip/masterunzip mastercd sonar-scanning-examples-master/sonarqube-scanner

2、sonar-project.properties

我们会看到项目下面有一个文件 sonar-project.properties,如果想要代码被扫描,就需要在代码路径下放一个配置文件。

[root@monitor sonarqube-scanner]# cat sonar-project.properties sonar.projectKey=org.sonarqube:sonarqube-scanner#Keysonar.projectName=ExampleofSonarQube Scanner Usage#这里的名称会显示在web上面sonar.projectVersion=1.0#版本,也会显示在web上面sonar.sources=src#软件包存放路径sonar.sourceEncoding=UTF-8#字体编码

3、扫描

需要在项目文件里面进行执行,程序会在当面目录下扫描sonar-project.properties文件,根据配置文件进行扫描工作。扫描之后我们在web界面上就可以看到代码的扫描结果。

[root@monitor sonarqube-scanner]# /usr/local/sonar-scanner/bin/sonar-scannerINFO: Scanner configuration file: /usr/local/sonar-scanner/conf/sonar-scanner.propertiesINFO: Project root configuration file: /root/tmp/sonar-scanning-examples-master/sonarqube-scanner/sonar-project.propertiesINFO: SonarQube Scanner 2.8INFO: Java 1.8.0_45 Oracle Corporation (64-bit)INFO: Linux 2.6.32-573.el6.x86_64 amd64INFO: User cache: /root/.sonar/cacheINFO: Publish modeINFO: Load global settingsINFO: Load global settings (done) | time=67msINFO: Server id: A737A953-AWVGKXYQugQwgVmc5Jb0INFO: User cache: /root/.sonar/cacheINFO: Load plugins indexINFO: Load plugins index (done) | time=48msINFO: Plugin [l10nzh] defines 'l10nen' as base plugin. This metadata can be removed from manifest of l10n plugins since version 5.2.INFO: SonarQube server 6.7.5INFO: Default locale: "en_US", source code encoding: "UTF-8"INFO: Process project propertiesINFO: Load project repositoriesINFO: Load project repositories (done) | time=113msINFO: Load quality profilesINFO: Load quality profiles (done) | time=28msINFO: Load active rulesINFO: Load active rules (done) | time=416msINFO: Load metrics repositoryINFO: Load metrics repository (done) | time=23msWARN: SCM provider autodetection failed. No SCM provider claims to support this project. Please use sonar.scm.provider to define SCM of your project.INFO: Project key: org.sonarqube:sonarqube-scannerINFO: -------------  Scan Example of SonarQube Scanner UsageINFO: Load server rulesINFO: Load server rules (done) | time=49msINFO: Base dir: /root/tmp/sonar-scanning-examples-master/sonarqube-scannerINFO: Working dir: /root/tmp/sonar-scanning-examples-master/sonarqube-scanner/.sonarINFO: Source paths: src, copybooksINFO: Source encoding: UTF-8, default locale: en_USINFO: Index filesINFO: 36 files indexedINFO: Quality profile for flex: Sonar wayINFO: Quality profile for js: Sonar wayINFO: Quality profile for php: Sonar wayINFO: Quality profile for py: Sonar wayINFO: Quality profile for xml: Sonar wayINFO: Sensor PythonXUnitSensor [python]INFO: Sensor PythonXUnitSensor [python] (done) | time=10msINFO: Sensor Python Squid Sensor [python]INFO: Python unit test coverageINFO: Python integration test coverageINFO: Python overall test coverageINFO: Sensor Python Squid Sensor [python] (done) | time=271msINFO: Sensor SonarJavaXmlFileSensor [java]INFO: 1 source files to be analyzedINFO: Sensor SonarJavaXmlFileSensor [java] (done) | time=71msINFO: 1/1 source files have been analyzedINFO: Sensor Flex [flex]INFO: 2 source files to be analyzedINFO: 2/2 source files have been analyzedINFO: Sensor Flex [flex] (done) | time=98msINFO: Sensor Flex Cobertura [flex]INFO: Analyzing Cobertura report: coverage-report/coverage-cobertua-flex.xmlWARN: Using absolute path pattern is deprecated. Please use relative path instead of 'file:**/Circle.as'WARN: Using absolute path pattern is deprecated. Please use relative path instead of 'file:**/UncoveredCircle.as'INFO: Sensor Flex Cobertura [flex] (done) | time=62msINFO: Sensor XML Sensor [xml]INFO: Sensor XML Sensor [xml] (done) | time=125msINFO: Sensor PHP sensor [php]INFO: 1 source files to be analyzedINFO: 1/1 source files have been analyzedINFO: No PHPUnit test report provided (see 'sonar.php.tests.reportPath' property)INFO: No PHPUnit coverage reports provided (see 'sonar.php.coverage.reportPaths' property)INFO: Sensor PHP sensor [php] (done) | time=387msINFO: Sensor Analyzer for "php.ini" files [php]INFO: Sensor Analyzer for "php.ini" files [php] (done) | time=2msINFO: Sensor JavaScript Squid Sensor [javascript]INFO: 1 source files to be analyzedINFO: 1/1 source files have been analyzedINFO: Unit Test Coverage Sensor is startedINFO: Integration Test Coverage Sensor is startedINFO: Overall Coverage Sensor is startedINFO: Sensor JavaScript Squid Sensor [javascript] (done) | time=194msINFO: Sensor Zero Coverage SensorINFO: Sensor Zero Coverage Sensor (done) | time=23msINFO: Sensor CPD Block IndexerINFO: Sensor CPD Block Indexer (done) | time=18msINFO: No SCM system was detected. You can use the 'sonar.scm.provider' property to explicitly specify it.INFO: 7 files had no CPD blocksINFO: Calculating CPD for 6 filesINFO: CPD calculation finishedINFO: Analysis report generated in 88ms, dir size=90 KBINFO: Analysis reports compressed in 25ms, zip size=42 KBINFO: Analysis report uploaded in 60msINFO: ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard/index/org.sonarqube:sonarqube-scannerINFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis reportINFO: More about the report processing at http://localhost:9000/api/ce/task?id=AWVG9oWP0Yruaz76HJQYINFO: Task total time: 3.756 sINFO: ------------------------------------------------------------------------INFO: EXECUTION SUCCESSINFO: ------------------------------------------------------------------------INFO: Total time: 4.698sINFO: Final Memory: 52M/421MINFO: ------------------------------------------------------------------------[root@monitor sonarqube-scanner]# /usr/local/sonar-scanner/bin/sonar-scannerINFO: Scanner configuration file: /usr/local/sonar-scanner/conf/sonar-scanner.propertiesINFO: Project root configuration file: /root/tmp/sonar-scanning-examples-master/sonarqube-scanner/sonar-project.propertiesINFO: SonarQube Scanner 2.8INFO: Java 1.8.0_45 Oracle Corporation (64-bit)INFO: Linux 2.6.32-573.el6.x86_64 amd64INFO: User cache: /root/.sonar/cacheINFO: Publish modeINFO: Load global settingsINFO: Load global settings (done) | time=63msINFO: Server id: A737A953-AWVGKXYQugQwgVmc5Jb0INFO: User cache: /root/.sonar/cacheINFO: Load plugins indexINFO: Load plugins index (done) | time=50msINFO: Plugin [l10nzh] defines 'l10nen' as base plugin. This metadata can be removed from manifest of l10n plugins since version 5.2.INFO: SonarQube server 6.7.5INFO: Default locale: "en_US", source code encoding: "UTF-8"INFO: Process project propertiesINFO: Load project repositoriesINFO: Load project repositories (done) | time=12msINFO: Load quality profilesINFO: Load quality profiles (done) | time=29msINFO: Load active rulesINFO: Load active rules (done) | time=446msINFO: Load metrics repositoryINFO: Load metrics repository (done) | time=26msWARN: SCM provider autodetection failed. No SCM provider claims to support this project. Please use sonar.scm.provider to define SCM of your project.INFO: Project key: org.sonarqube:sonarqube-scannerINFO: -------------  Scan Example of SonarQube Scanner UsageINFO: Load server rulesINFO: Load server rules (done) | time=42msINFO: Base dir: /root/tmp/sonar-scanning-examples-master/sonarqube-scannerINFO: Working dir: /root/tmp/sonar-scanning-examples-master/sonarqube-scanner/.sonarINFO: Source paths: src, copybooksINFO: Source encoding: UTF-8, default locale: en_USINFO: Index filesINFO: 36 files indexedINFO: Quality profile for flex: Sonar wayINFO: Quality profile for js: Sonar wayINFO: Quality profile for php: Sonar wayINFO: Quality profile for py: Sonar wayINFO: Quality profile for xml: Sonar wayINFO: Sensor PythonXUnitSensor [python]INFO: Sensor PythonXUnitSensor [python] (done) | time=10msINFO: Sensor Python Squid Sensor [python]INFO: Python unit test coverageINFO: Python integration test coverageINFO: Python overall test coverageINFO: Sensor Python Squid Sensor [python] (done) | time=365msINFO: Sensor SonarJavaXmlFileSensor [java]INFO: 1 source files to be analyzedINFO: Sensor SonarJavaXmlFileSensor [java] (done) | time=65msINFO: 1/1 source files have been analyzedINFO: Sensor Flex [flex]INFO: 2 source files to be analyzedINFO: 2/2 source files have been analyzedINFO: Sensor Flex [flex] (done) | time=98msINFO: Sensor Flex Cobertura [flex]INFO: Analyzing Cobertura report: coverage-report/coverage-cobertua-flex.xmlWARN: Using absolute path pattern is deprecated. Please use relative path instead of 'file:**/Circle.as'WARN: Using absolute path pattern is deprecated. Please use relative path instead of 'file:**/UncoveredCircle.as'INFO: Sensor Flex Cobertura [flex] (done) | time=62msINFO: Sensor XML Sensor [xml]INFO: Sensor XML Sensor [xml] (done) | time=120msINFO: Sensor PHP sensor [php]INFO: 1 source files to be analyzedINFO: 1/1 source files have been analyzedINFO: No PHPUnit test report provided (see 'sonar.php.tests.reportPath' property)INFO: No PHPUnit coverage reports provided (see 'sonar.php.coverage.reportPaths' property)INFO: Sensor PHP sensor [php] (done) | time=386msINFO: Sensor Analyzer for "php.ini" files [php]INFO: Sensor Analyzer for "php.ini" files [php] (done) | time=2msINFO: Sensor JavaScript Squid Sensor [javascript]INFO: 1 source files to be analyzedINFO: 1/1 source files have been analyzedINFO: Unit Test Coverage Sensor is startedINFO: Integration Test Coverage Sensor is startedINFO: Overall Coverage Sensor is startedINFO: Sensor JavaScript Squid Sensor [javascript] (done) | time=202msINFO: Sensor Zero Coverage SensorINFO: Sensor Zero Coverage Sensor (done) | time=39msINFO: Sensor CPD Block IndexerINFO: Sensor CPD Block Indexer (done) | time=19msINFO: No SCM system was detected. You can use the 'sonar.scm.provider' property to explicitly specify it.INFO: 7 files had no CPD blocksINFO: Calculating CPD for 6 filesINFO: CPD calculation finishedINFO: Analysis report generated in 95ms, dir size=90 KBINFO: Analysis reports compressed in 30ms, zip size=42 KBINFO: Analysis report uploaded in 148msINFO: ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard/index/org.sonarqube:sonarqube-scannerINFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis reportINFO: More about the report processing at http://localhost:9000/api/ce/task?id=AWVG93I90Yruaz76HJQmINFO: Task total time: 3.803 sINFO: ------------------------------------------------------------------------INFO: EXECUTION SUCCESSINFO: ------------------------------------------------------------------------INFO: Total time: 4.812sINFO: Final Memory: 53M/416MINFO: ------------------------------------------------------------------------

4、web查看扫描结果

Jenkins 配置Sonar

1、安装 Sonar 插件

系统管理--->管理插件。找到插件SonarQube Scanner进行安装。

2、配置 SonarQube servers

系统管理--->系统设置。

3、配置 SonarQube Scanner

系统管理--->全局工具配置。

任务构建配置

编辑我们之前成功的项目,在Pre Steps选择Execute SonarQube Scanner。

sonar.projectKey=wenjuan

sonar.projectName=wenjuan

sonar.projectVersion=1.0

sonar.sources=src

sonar.language=java\

构建

1、新版本构建问题

因为我安装的是比较新的版本,SonarJava 的版本是5.6,在4.12以后的SoanrJava分析插件,在分析的时候提示Please provide compiled classes of your project with sonar.java.binaries property的问题。

解决版本是把 SoanrJava 的版本降低,选择的版本是4.10,下载地址如下:

https://sonarsource.bintray.com/Distribution/sonar-java-plugin/sonar-java-plugin-4.10.0.10260.jar

把这个 jar 包放到/usr/local/sonarqube/extensions/plugins/里面,把之前的高版本的删掉,然后重启 sonarqube 即可。

2、正常构建

问题解决之后,我们就可以直接进行构建了。

点击Console Output可以查看构建输出的内容。

构建完成后,我们发现这里的SonarQube可以点击,我们点击SonarQube就会链接到10.0.0.14:9000 ,就是代码查看器的地址。

钉钉的配置

1、 成为钉钉群主

进入某个群->点击机器人图标。

注意:如果你不是群主,且群主开启了仅群主可管理,那么你将无法创建机器人。

2、选择自定义机器人并设置信息

选择自定义机器人,填写机器人信息。

3、复制 webhook 地址

注意:此处的webhook将用于Jenkins当中的参数配置。

Jenkins 的配置钉钉

1、安装插件

进入系统管理,进入管理插件,搜索插件(dingding)并安装。

等待安装完成之后,返回首页。

2、配置 webhook

进入我们之前wenjuan的项目,前往"构建后的操作"。

点击“增加构建后操作步骤”->选择“钉钉通知配置器”。

填写 webhook。

注意:Jenkins URL配置中需要在最后添加"/",要不然会导致拼接的Url出错,这里填写有问题会导致无法从钉钉中跳转到Jenkins任务.

测试

选择 立即构建

构建

钉钉接收消息-推送结果,点击结果即可跳转到Jenkins页面。

问题

之前的 java 程序是通过 ssh 发布到服务器上面的,通过的是 shell 脚本,但是有些时候,我们并没有服务器的权限,只有对应 tomcat 的管理权限,直接通过 tomcat 的管理控制台发布 war 包程序,这也叫做热发布,发布过程中不需要重启 tomcat。

tomcat 配置

1、开启管理用户

tomcat 默认没有这样的用户,需要在TOMCAT_HOME/conf/tomcat-users.xml添加manager-script组和相应的用户,增加如下几行:

或者为了简便,直接弄一个权限很大的账号,比如下面:

注:配置好后需要重启Tomcat才能生效。

登陆 tomcat 后台

只有角色是manager-gui的用户才有管理后台的权限,而 Jenkins 的部署只需要角色为manager-script即可。

访问我们的内网服务器地址http://10.0.0.11:8081/manager,输入账号密码登陆查看。

我们可以看到正在运行的项目,我们可以通过undeploy来删除,然后通过我们下面的方法进行部署。

Jenkins 配置 Deploy 

1、下载插件

下载插件Deploy to container。

2、添加 Deploy 构建

打开我们之前的 wenjuan 项目,在构建后操作选择Deploy war/ear to a container。

3、配置 deploy 构建

WAR/EAR files:是 war 包的相对路径(相对于工作区路径,即在工作区中 war 包的相对路径),如我的 maven 执行完成之后会在工作区的 target 目录下生成项目 .war,所以这里我的路径就写 target\项目.war。

Content path:tomcat 的发布路径,即使用 localhost/(content path设置的名字)来访问项目。

Containers:选择 Tomcat 7.x,Credentials 添加我们上面 tomcat 添加的用户,Tomcat URL 即为我们正常访问 tomcat 的页面的地址。

Deploy on failure:是发生错误的时候是否发布到tomcat。

测试结果

1、查看构建日志

通过日志可以看到部署成功。

2、查看服务器

3、查看 tomcat 后台

没有任何问题,热部署成功。 

如果你看到这里,那么恭喜你,成功了,祝贺祝贺 ,如有任何问题,请添加我微信沟通 13916854721

相关文章

网友评论

      本文标题:哥哥带你学Jenkins自动化打包二(持续更新中……)

      本文链接:https://www.haomeiwen.com/subject/fwmkgqtx.html