持续集成与自动化部署-jenkins&sonar代码质量管理平台部署和基础使用(代码片段)

author author     2022-11-15     546

关键词:

1 jenkins

安装参考链接

1.1 安装jenkins

[[email protected] ~]# yum install -y java-1.8.0
[[email protected] ~]# cd /etc/yum.repos.d/
[[email protected] yum.repos.d]# wget http://pkg.jenkins.io/redhat/jenkins.repo
[[email protected] yum.repos.d]# rpm --import http://pkg.jenkins.io/redhat/jenkins.io.key
[[email protected] yum.repos.d]# yum install -y jenkins
[[email protected] yum.repos.d]# systemctl start jenkins

1.2 安装jenkins插件

[[email protected] ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
d71307d2e9764f428d853bc49ac6b5ea

技术分享图片

选择默认安装,会安装通用的插件,剩下的在需要用的的时候再安装。
技术分享图片

等待安装完成
技术分享图片
设置用户名密码
user:admin
key: 123..abc
技术分享图片
技术分享图片

1.3 新建一个任务

[[email protected] ~]# usermod -s /bin/bash jenkins
 -bash-4.2$ ssh-copy-id -i .ssh/id_rsa.pub [email protected]
The authenticity of host ‘10.0.0.204 (10.0.0.204)‘ can‘t be established.
ECDSA key fingerprint is cd:9c:13:52:1a:96:c6:2b:4c:0c:5a:83:f6:94:01:48.
Are you sure you want to continue connecting (yes/no)? yes
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]‘s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh ‘[email protected]‘"
and check to make sure that only the key(s) you wanted were added.

-bash-4.2$ 
-bash-4.2$ ssh [email protected]
Last login: Wed Apr 12 09:33:40 2017
[[email protected] ~]$ 
[[email protected] ~]$ logout
Connection to 10.0.0.204 closed.

技术分享图片
技术分享图片

测试
点击立即构建
技术分享图片
自动部署完成。
技术分享图片

1.4 jenkins安装gitlab插件

注:jenkins填写的是私钥(gitlab服务器上填写的是公钥 - 步骤同gitlab添加ssh 公钥,jenkins用ssh协议到gitlab上面去拉取代码。)

技术分享图片

技术分享图片

2. 代码质量管理 - sonar

Sonar 是一个用于代码质量管理的开放平台。通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具。与持续集成工具(例如 Hudson/Jenkins 等)不同,Sonar 并不是简单地把不同的代码检查工具结果(例如 FindBugs,PMD 等)直接显示在 Web 页面上,而是通过不同的插件对这些结果进行再加工处理,通过量化的方式度量代码质量的变化,从而可以方便地对不同规模和种类的工程进行代码质量管理。
在对其他工具的支持方面,Sonar 不仅提供了对 IDE 的支持,可以在 Eclipse 和 IntelliJ IDEA 这些工具里联机查看结果;同时 Sonar 还对大量的持续集成工具提供了接口支持,可以很方便地在持续集成中使用 Sonar。
此外,Sonar 的插件还可以对 Java 以外的其他编程语言提供支持,对国际化以及报告文档化也有良好的支持

2.1 部署sonar

注:1 sonar 需要安装数据库版本为mysql 5.6及以上。 2 需要java环境,java版本为1.8以上

mysql 5.6 Yum源
https://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
yum -y install  mysql-community-server

[sonar部署参考链接] (https://www.unixhot.com/article/56)

2.1.1 下载 安装 sonar源码

[[email protected] ~]# cd /server/tools/
[[email protected] ~]# wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-5.6.zip
[[email protected] tools]#  unzip sonarqube-5.6.zip
[[email protected] tools]# mv sonarqube-5.6 /usr/local/
[[email protected] tools]# ln -s /usr/local/sonarqube-5.6/ /usr/local/sonarqube

2.1.2 准备Sonar数据库

CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
GRANT ALL ON sonar.* TO ‘sonar‘@‘localhost‘ IDENTIFIED BY ‘[email protected]‘;
GRANT ALL ON sonar.* TO ‘sonar‘@‘%‘ IDENTIFIED BY ‘[email protected]‘;
FLUSH PRIVILEGES;
[[email protected] ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.01 sec)

mysql> GRANT ALL ON sonar.* TO ‘sonar‘@‘localhost‘ IDENTIFIED BY ‘[email protected]‘;
GRANT ALL ON sonar.* TO ‘sonar‘@‘%‘ IDENTIFIED BY ‘[email protected]‘;
Query OK, 0 rows affected (0.06 sec)

mysql> GRANT ALL ON sonar.* TO ‘sonar‘@‘%‘ IDENTIFIED BY ‘[email protected]‘;
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

mysql> 

2.1.3 配置Sonar

[[email protected] tools]# cd /usr/local/sonarqube/conf/
[[email protected] conf]# vim sonar.properties
sonar.jdbc.username=sonar
[email protected]
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

2.1.4 启动Sonar

如果没有正常启动,查看日志一般就可以解决。

可以在Sonar的配置文件来配置Sonar Web监听的IP地址和端口,默认是9000端口。

[[email protected] conf]# vim sonar.properties
sonar.web.host=0.0.0.0
sonar.web.port=9000

[[email protected] conf]# /usr/local/sonarqube/bin/linux-x86-64/sonar.sh start
[[email protected] logs]# netstat -tnlpua|grep 9000
tcp        0      0 0.0.0.0:9000            0.0.0.0:*               LISTEN      44042/java  

2.2 安装语言插件

分析什么语言就下载对应的语言插件。

web界面 用户名密码,默认为:admin/admin

步骤:
方法一 web界面安装

Administration --> System -->  Update Center --> Available  搜索chiness Pack 点击install。

安装完成之后需要重启sonar
[[email protected] plugins]# /usr/local/sonarqube/bin/linux-x86-64/sonar.sh restart

技术分享图片

方法二 下载jar包进行替换
下载的连接可以根据在web界面安装插件的失败提醒来下载,如果不能下载,用迅雷下载。

https://github.com/SonarQubeCommunity/sonar-l10n-zh/releases

https://github.com/SonarQubeCommunity/sonar-l10n-zh/releases/download/sonar-l10n-zh-plugin-1.11/sonar-l10n-zh-plugin-1.11.jar
下载到/usr/local/sonarqube/extensions/plugins目录

需要重启才能生效
[[email protected] plugins]# /usr/local/sonarqube/bin/linux-x86-64/sonar.sh restart

技术分享图片

2.3 安装sonar 代码扫描工具插件

参考链接

2.3.1 下载安装源码文件

[[email protected] plugins]# cd /server/tools/
[[email protected] tools]# wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.6.1.zip  
[[email protected] tools]# cp -rf sonar-scanner-2.6.1 /usr/local/sonar-scanner

2.3.2 修改配置文件

[[email protected] tools]# cd /usr/local/sonar-scanner/conf
[[email protected] conf]# cat sonar-scanner.properties 

#----- Default SonarQube server
sonar.host.url=http://localhost:9000

#----- Default source code encoding
sonar.sourceEncoding=UTF-8

#----- Global database settings (not used for SonarQube 5.2+)
sonar.jdbc.username=sonar
[email protected]

#----- MySQL
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8

2.3.3 下载测试代码进行扫描

扫描的是php语言,因此需要php语言的插件SonarPHP

[[email protected] tools]# unzip sonar-examples-4.5.zip
[[email protected] tools]#  cd /server/tools/sonar-examples-4.5/projects/languages/php/php-sonar-runner

[[email protected] php-sonar-runner]# /usr/local/sonar-scanner/bin/sonar-scanner
.....
INFO: Sensor PHP sensor
INFO: 1 source files to be analyzed
INFO: 1/1 source files have been analyzed
INFO: No PHPUnit test report provided (see ‘sonar.php.tests.reportPath‘ property)
INFO: No PHPUnit unit test coverage report provided (see ‘sonar.php.coverage.reportPath‘ property)
INFO: No PHPUnit integration test coverage report provided (see ‘sonar.php.coverage.itReportPath‘ property)
INFO: No PHPUnit overall coverage report provided (see ‘sonar.php.coverage.overallReportPath‘ property)
INFO: Sensor PHP sensor (done) | time=1030ms
INFO: Sensor Analyzer for "php.ini" files
INFO: Sensor Analyzer for "php.ini" files (done) | time=6ms
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=24ms
INFO: Sensor Code Colorizer Sensor
INFO: Sensor Code Colorizer Sensor (done) | time=1ms
INFO: Sensor CPD Block Indexer
INFO: DefaultCpdBlockIndexer is used for php
INFO: Sensor CPD Block Indexer (done) | time=0ms
INFO: Calculating CPD for 1 files
INFO: CPD calculation finished
INFO: Analysis report generated in 265ms, dir size=22 KB
INFO: Analysis reports compressed in 14ms, zip size=9 KB
INFO: Analysis report uploaded in 472ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard/index/org.sonarqube:php-simple-sq-scanner
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://localhost:9000/api/ce/task?id=AVthpV-GZk_1lOkjIdp8
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 16.131s
INFO: Final Memory: 43M/117M

注意:这里面有一个文件需要定义的,告诉sonar 语言,版本,及相关。

[[email protected] php-sonar-runner]# cat sonar-project.properties 
# Required metadata
sonar.projectKey=org.sonarqube:php-simple-sq-scanner
sonar.projectName=PHP :: Simple Project :: SonarQube Scanner
sonar.projectVersion=1.0

# Comma-separated paths to directories with sources (required)
sonar.sources=src

# Language
sonar.language=php

# Encoding of the source files
sonar.sourceEncoding=UTF-8

技术分享图片

技术分享图片

持续集成与自动化部署-jenkins与sonargitlab集成(代码片段)

1将jenkins和sonar集成1.1安装sonar插件jenkins上安装sonarqubeplugin,如果没有就安装sonarqubescannerforjenkins这个插件。1.2关联sonar插件1.2.1系统设置设置sonar地址系统管理>系统设置>AddSonarQube>sonarqubeservers修改如下:1.2.2全局设置设置sonar... 查看详情

jenkins与gitlib实现自动化部署与持续构建(代码片段)

Jenkins概念Jenkins是一个功能强大的应用程序,允许持续集成和持续交付项目,无论用的是什么平台。这是一个免费的源代码,可以处理任何类型的构建或持续集成。集成Jenkins可以用于一些测试和部署技术。Jenkins是一种软件允许... 查看详情

jenkins容器自动化部署与持续集成(kubernetes)(代码片段)

         本文是SpringCloud实战(九)-Docker自动化部署与持续集成,若要关注前文,请点击传送门:        SpringCloud实战(八)-断路器监控与集群监控(HystrixDashboard)        前文我们介绍了Hy... 查看详情

持续集成与自动化部署-devops&持续集成交付部署介绍

...件开发的过程中,频繁的将代码集成到主干上,然后进行自动化测试。2.2持续部署持续交付是指在持续集成的基础上,将集 查看详情

pytest接口自动化测试框架|jenkins集成初探

视频来源:B站《冒死上传!pytest接口自动化测试框架(基础理论到项目实战及二次开发)教学视频【软件测试】》一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持&#x... 查看详情

jenkins持续集成案例之-自动化部署

Jenkins是一个开源软件项目,目的就是提供了一个开放的软件平台,让我们实现软件集成变成可能,我们经常听到企业中讲CI、CD两个名词,这些都可以跟jenkins联系在一起,实现持续集成,持续部署及持续交付;在企业中,Jenkins... 查看详情

持续集成部署工具jenkins简介

...集成、持续部署、持续交付。也可以利用jenkins实现docker自动化流水2.安装gitlabgitlab部署参考https://blog.51cto.com/flyfish225 查看详情

ci与cd之docker上安装jenkins(代码片段)

...步的使用,这也是持续集成的目的,CI是属于开发人员的自动化流程。 CD:持续交付(ContinuousDelivery)和持续部署(ContinuousDeployment),这里查阅了一些资料,并简单总结了一下:    1.持续交付意味着所有的变更都可以随时交... 查看详情

使用 Jenkins 和 Opswork 集成进行持续部署

】使用Jenkins和Opswork集成进行持续部署【英文标题】:ContinuousDeploymentusingJenkinsandOpsworkintegration【发布时间】:2016-08-2802:55:07【问题描述】:我正在尝试使用jenkins和OpsWorks设置持续部署。我已经配置了Jenkins,但我不知道如何将Jenk... 查看详情

持续集成工具篇:jenkins与流水线管理转

该文章,围绕持续集成:Jenkins+Docker+K8S相关组件,实现自动化管理源码编译、打包、镜像构建、部署等操作;一、Jenkins安装1、环境部署下载Jenkins包java-jarjenkins.war--httpPort=8090注意这里直接下载war文件,以优秀不够,你是否无可... 查看详情

jenkins视频教程

....avi06、Jenkins持续集成网站构建实战.avi07、Jenkins持续集成自动化部署一.avi08、Jenkins持续集成自动化部署二.avi09、Jenkins持续集 查看详情

gitlab+jenkins自动化部署持续集成(代码片段)

环境:1、centos7.62、jdk1.8.0_1413、4G30G4core4、关闭防火墙和selinux一、安装jenkins  1、下载jenkinshttps://pkg.jenkins.io/redhat/ jenkins-2.208-1.1.noarch.rpm  2、安装jdk和jenkinsrpm包  rpm-ivhjdk-8u141-linux-x64.rpm  rpm-ivhjenkins-2.208-1.1.noarch.rpm... 查看详情

linux-gitlab+jenkins持续集成+自动化部署(代码片段)

GitLab+Jenkins持续集成+自动化部署什么是持续集成?(1)Continuousintegration(CI)持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通常每个成员至少集成一次,也就意味着每天可能会发生多次集成。每次集成都... 查看详情

持续集成工具jenkins的使用

jenkins类似于Hadson,是一款持续集成工具。使用jenkins完成自动化部署的表现为:当开发人员向版本库提交新的代码后,应用服务器上自动部署,用户或测试人员使用的马上就是最新的应用程序。搭建该持续集成环境可以把整个构... 查看详情

持续集成+自动化部署[代码流水线管理及jenkins和gitlab集成]

持续集成+自动化部署[代码流水线管理及Jenkins和gitlab集成]标签(空格分隔):Jenkins一、代码流水线管理  Pipeline名词顾名思义就是流水线的意思,因为公司可能会有很多项目。如果使用jenkins构建完成后,开发构建项目需要一... 查看详情

12.jenkins持续集成企业实战

...enkins相关概念1.6Jenkins平台设置1.7Jenkins构建JOB工程1.8Jenkins自动化部署1.9Jenkins插件安装1.10Jenkins邮件配置1.11Jenkins多实例配置1.12Jenkins+A 查看详情

1.gitlab和jenkins结合构建持续集成(ci)环境

...开发过程中,可以频繁的将代码部署集成到主干,并进行自动化测试。持续交付:持续交付指的是在持续集成的环境基础之上,将代码部署到预生产环境。持续部署:在持续交付的基础上,把部署到生产环境的过程自动化。 ... 查看详情

持续集成与持续部署宝典part3:创建集成环境

...量的机器上。接着,我们将环境设置到了Jenkins CI上,自动化处理了源代码的持续构建、打包和测试。在本章中,我们将进一步对流水线进行研究(如下所示),了解如何将项目持续部署到一个长时间运行的 查看详情