阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Jenkins+Gitlab+Sonar代码检查平台搭建

189次阅读
没有评论

共计 20031 个字符,预计需要花费 51 分钟才能阅读完成。

安装 JDK

从官网安装,这里选择 yum 安装,我已经摆脱 tar 包综合征。怎么快怎么来,后期我将发布 Docker 环境。敬请期待 ing…

http://www.Oracle.com/technetwork/Java/javase/downloads/jdk8-downloads-2133151.html
Jenkins+Gitlab+Sonar 代码检查平台搭建

安装命令

yum -y install jdk-8u111-Linux-x64.rpm

Jenkins+Gitlab+Sonar 代码检查平台搭建

安装 Jenkins

安装
    wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/RedHat/jenkins.repo
    rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
    yum install jenkins
启动 jenkins
    service jenkins start
获取初始密码
    tail -f /var/log/jenkins/jenkins.log

Jenkins+Gitlab+Sonar 代码检查平台搭建

输入密码
Jenkins+Gitlab+Sonar 代码检查平台搭建
初始化要等一段时间,默认安装插件就行,后续的插件到 tools 里面选

Jenkins+Gitlab+Sonar 代码检查平台搭建
后续自己创建个账号密码。

安装 jenkins 插件

Jenkins 安装配置各种插件:
Git  ---- GitLab ---- Gitlab Authentication plugin  (Git 授权插件)
Gitlab Authentication plugin (环境变量注入, 目前用于获取 gitLog, 并传递给 Fir.im上传信息)
Email Extension  (邮件扩展插件, 打包完成后邮件通知各人员)
Fir Plugin  (Fir.im上传插件,apk/ipa 分发渠道)
Bearychat Plugin (上传到 Bearychat 插件, 同于通知)
Gradle Plugin   (Android 构建插件)
Xcode integration  (iOS 构建插件)
Keychains and Provisioning Profiles Management  (iOS 证书配置插件)
Sonar:代码质量管理平台, 也是通过安装各种插件来扩展代码检测功
SonarQube Plugin(代码审查插件)

系统管理–> 插件管理–> 选择你需要的

配置 Jenkins 环境

需要配置 sonar 环境,同时要在 sonar 里面生成 Token。请看 sonar 搭建篇。
这里是 jenkins 的系统配置

Jenkins+Gitlab+Sonar 代码检查平台搭建
sonar 的 token 配置
Jenkins+Gitlab+Sonar 代码检查平台搭建
Jenkins+Gitlab+Sonar 代码检查平台搭建
配置 Sonar_Runner
Jenkins+Gitlab+Sonar 代码检查平台搭建

Jenkins 和 Gitlab 免秘钥,同时可以拉取代码

我理解 admin 用户下的 ssh 免秘钥,可以拉取其他用户代码。我拉智标项目可以。(通过这个方法可以快速建立 jenkins 和 gitlab 免秘钥认证。同时实现拉起代码)
Jenkins+Gitlab+Sonar 代码检查平台搭建
Jenkins+Gitlab+Sonar 代码检查平台搭建
JENKINS 里面的配置,注意路径
Jenkins+Gitlab+Sonar 代码检查平台搭建
Jenkins+Gitlab+Sonar 代码检查平台搭建
Jenkins+Gitlab+Sonar 代码检查平台搭建

拉取代码、配置 sonar 配置文件

Jenkins+Gitlab+Sonar 代码检查平台搭建
构建触发 5 分钟执行一次
Jenkins+Gitlab+Sonar 代码检查平台搭建
还有一种只有开发提交到 gitlab 以后才会触发构建,没错呢就是 gitlab 钩子。jenkins 配置只需点一点。
Jenkins+Gitlab+Sonar 代码检查平台搭建
jenkins 失败构建重试
Jenkins+Gitlab+Sonar 代码检查平台搭建
删除旧的构建,不然增量就把目录搞爆了,我之前默认在 var 下,就呵呵了,周末收了 70 多封告警邮件提示构建失败。
Jenkins+Gitlab+Sonar 代码检查平台搭建
构建时选择 Sonar 插件
Jenkins+Gitlab+Sonar 代码检查平台搭建
好了这会可以构建完成了。当然领导说要发邮件给开发,这个需求我觉得蛮合理的。
Jenkins+Gitlab+Sonar 代码检查平台搭建
Jenkins+Gitlab+Sonar 代码检查平台搭建
Jenkins+Gitlab+Sonar 代码检查平台搭建
Jenkins+Gitlab+Sonar 代码检查平台搭建
这个是扩容邮件的插件,里面可以配置成功后发邮件给开发,当然我写了个 html 页面,发个网页过去高大上多了。
Jenkins+Gitlab+Sonar 代码检查平台搭建
哈哈哈哈哈哈
Jenkins+Gitlab+Sonar 代码检查平台搭建
Jenkins+Gitlab+Sonar 代码检查平台搭建
同事的代码结果不方便拿出来晒,怕被打~~~#_#
这个是 jenkins 和 jdk 的安装,其实应该先装 jdk 和 sonar。请看第二篇 soanr

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2017-01/139900p2.htm

sonar 平台搭建

安装 sonar 环境

解压缩
 unzip sonarqube-5.6.4.zip -d /usr/local/src/
 unzip sonar-scanner-2.6.1.zip -d /usr/local/src/
添加环境变量
export PATH="/letv/Redis-2.8.17/src:$SONAR_HOME:$SONAR_RUNNER_HOME/bin:$MAVEN_HOME/bin:$PATH"
export SONAR_HOME=/usr/local/src/sonarqube-5.6.4/bin/linux-x86-64
export SONAR_RUNNER_HOME=/usr/local/src/sonar-scanner-2.6.1/
export MAVEN_HOME=/usr/local/src/apache-maven-3.3.9

安装数据库并配置 >Sonar 还需要安装 MySQL 数据库 (5.6 以上)
这个集团 dba 安装好了,看看合适不

自己安装这个只有自己搞了这个是 5.6 的,集团数据库版本不支持。# rpm -ivh https://mirror.tuna.tsinghua.edu.cn/mysql/yum/mysql57-community-el6/mysql-community-release-el6-7.noarch.rpm
#yum install mysql-server -y
# /etc/init.d/mysqld start

#mysql
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar@pw';
GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar@pw';
FLUSH PRIVILEGES;
搞定
sonar.jdbc.username=sonar
sonar.jdbc.password=******
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.web.host=0.0.0.0
sonar.web.port=9000

之前测试环境的一些坑

1、mysql5.7 需要使用复杂密码(不建议用),同时修改密码方法如下
先修改 vim /etc/my.cnf
#skip-grant-tables
再试下
update mysql.user set authentication_string=password('keYnZh0oK5pUIoIx') where user='root' ;
ALTER USER 'root'@'localhost'IDENTIFIED BY 'DwlRDko4aTeO^WzH';
之后取消 #skip-grant-tables
重启 mysqld,再创建 sonar 库才可以。2、修改 jdbc 连接池
/usr/local/sonar/conf/sonar.properties
sonar.jdbc.username=sonar
sonar.jdbc.password=DwlRDko4aTeO^WzH
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
3、jdk 版本必须 1.8 以上不然抛异常
/usr/local/sonar/logs/sonar.log

来来来测试下集团 mysql

 mysql -h 10.149.14.242 -P3927 -u sonar_w -p *******

/usr/local/src/sonarqube-5.6.4/conf
修改配置文件如下
sonar.jdbc.username=sonar_w
sonar.jdbc.password=NjI2OGExO7TI4NDU1
sonar.jdbc.url=jdbc:mysql://10.149.14.242:3927/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.web.host=0.0.0.0
sonar.web.port=9000

启动 sonar

source /etc/profile
sonar.sh start

查看日志, 发现链接数据库报错

tail -f /usr/local/src/sonarqube-5.6.4/logs/sonar.log
2017.01.23 17:58:21 ERROR web[o.a.c.c.C.[.[.[/]] Exception sending context initialized event to listener instance of class org.sonar.server.platform.PlatformServletContextListener
org.sonar.api.utils.MessageException: Unsupported mysql version: 5.5. Minimal supported version is 5.6.

查看 mysql 版本,不符合 5.6 的要求

mysql> status
--------------
mysql  Ver 14.14 Distrib 5.1.73, for RedHat-linux-gnu (x86_64) using readline 5.1

Connection id:      354
Current database:   
Current user:       sonar_w@10.127.96.124
SSL:            Not in use
Current pager:      stdout
Using outfile:      ''
Using delimiter:    ;
Server version:     5.5.5-10.0.14-MariaDB-log Source distribution
Protocol version:   10
Connection:     10.149.14.242 via TCP/IP
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
TCP port:       3927
Uptime:         7 hours 25 min 23 sec

Threads: 2  Questions: 5224  Slow queries: 0  Opens: 0  Flush tables: 1  Open tables: 63  Queries per second avg: 0.195
--------------

IE 输入 http://10.127.96.124:9000/ 看是否能正常访问这个过程需要大约 10 几分钟,默认用户名和密码都是 admin,安装完了我必须自己新建一个用户赋予 admin 权限,曾经的曾经被队友坑 admin 的权限都给删了。英文界面不是很友好,下面就把他汉化。

cd /usr/local/src/sonarqube-5.6.4/extensions/plugins
/usr/local/src/sonarqube-5.6.4/bin/linux-x86-64/sonar.sh restart

搞得麻烦 我直接写了个启动脚本分享下

#!/bin/sh  
#  
# rc file for SonarQube  
#  
# chkconfig: 345 96 10  
# description: SonarQube system (www.sonarsource.org)  
#  
### BEGIN INIT INFO  
# Provides: sonar  
# Required-Start: $network  
# Required-Stop: $network  
# Default-Start: 3 4 5  
# Default-Stop: 0 1 2 6  
# Short-Description: SonarQube system (www.sonarsource.org)  
# Description: SonarQube system (www.sonarsource.org)  
### END INIT INFO  

#/usr/bin/sonar $*
/usr/local/src/sonarqube-5.6.4/bin/linux-x86-64/sonar.sh $*

service sonar restart 没问题

Jenkins+Gitlab+Sonar 代码检查平台搭建

开机启动

SonarQube 开机自启动(Ubuntu, 32位):sudo ln -s $SONAR_HOME/bin/linux-x86-32/sonar.sh /usr/bin/sonar
sudo chmod 755 /etc/init.d/sonar
sudo update-rc.d sonar defaults
SonarQube 开机自启动(RedHat, CentOS, 64位):#sudo ln -s $SONAR_HOME/bin/linux-x86-64/sonar.sh /usr/bin/sonar
sudo ln -s $SONAR_HOME/sonar.sh /usr/bin/sonar
sudo chmod 755 /etc/init.d/sonar
sudo chkconfig --add sonar

安装 sonar 插件

安装扫描器插件 > 注意这个扫描器要安装在 jenkins 服务器上

# cd /usr/local/src/
# wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.6.1.zip 
# unzip sonar-scanner-2.6.1.zip -d /usr/local/
#ln -s /usr/local/sonar-scanner-2.6.1/ /usr/local/sonar-scanner
配置让扫描器跟 sonar 关联起来
# cd 
# cd /usr/local/sonar-scanner/conf/
# grep "^[a-Z]" sonar-scanner.properties 
sonar.host.url=http://10.0.0.102:9000
sonar.sourceEncoding=UTF-8
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar@pw
sonar.jdbc.url=jdbc:mysql://10.0.0.102:3306/sonar?useUnicode=true&characterEncoding=utf8

下载测试代码来进行测试

# cd
# wget https://github.com/SonarSource/sonar-examples/archive/master.zip
# unzip master.zip
# cd /root/sonar-examples-master/projects/languages/php/php-sonar-runner
# /usr/local/sonar-scanner/bin/sonar-scanner

测试一下

[root@gitlab-102 php]# pwd
/root/sonar-examples-master/projects/languages/php
[root@gitlab-102 php]# cat php-sonar-runner/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 直接拉起我之前上传 gitlab 的代码。

配置 Sonar、Jenkins 进行持续审查 http://www.linuxidc.com/Linux/2016-08/133877.htm

sonarQube 代码质量管理工具环境筹建笔记 http://www.linuxidc.com/Linux/2016-08/133878.htm

SonarQube 代码质量管理平台安装与使用  http://www.linuxidc.com/Linux/2016-08/133879.htm

快速搭建 Sonar 代码质量管理平台 http://www.linuxidc.com/Linux/2016-08/134416.htm

Sonar6.0 基于 CentOS7.2 安装与 Eclipse 集成使用  http://www.linuxidc.com/Linux/2016-10/135969.htm

Sonar 的详细介绍:请点这里
Sonar 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-01/139900.htm

安装 JDK

从官网安装,这里选择 yum 安装,我已经摆脱 tar 包综合征。怎么快怎么来,后期我将发布 Docker 环境。敬请期待 ing…

http://www.Oracle.com/technetwork/Java/javase/downloads/jdk8-downloads-2133151.html
Jenkins+Gitlab+Sonar 代码检查平台搭建

安装命令

yum -y install jdk-8u111-Linux-x64.rpm

Jenkins+Gitlab+Sonar 代码检查平台搭建

安装 Jenkins

安装
    wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/RedHat/jenkins.repo
    rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
    yum install jenkins
启动 jenkins
    service jenkins start
获取初始密码
    tail -f /var/log/jenkins/jenkins.log

Jenkins+Gitlab+Sonar 代码检查平台搭建

输入密码
Jenkins+Gitlab+Sonar 代码检查平台搭建
初始化要等一段时间,默认安装插件就行,后续的插件到 tools 里面选

Jenkins+Gitlab+Sonar 代码检查平台搭建
后续自己创建个账号密码。

安装 jenkins 插件

Jenkins 安装配置各种插件:
Git  ---- GitLab ---- Gitlab Authentication plugin  (Git 授权插件)
Gitlab Authentication plugin (环境变量注入, 目前用于获取 gitLog, 并传递给 Fir.im上传信息)
Email Extension  (邮件扩展插件, 打包完成后邮件通知各人员)
Fir Plugin  (Fir.im上传插件,apk/ipa 分发渠道)
Bearychat Plugin (上传到 Bearychat 插件, 同于通知)
Gradle Plugin   (Android 构建插件)
Xcode integration  (iOS 构建插件)
Keychains and Provisioning Profiles Management  (iOS 证书配置插件)
Sonar:代码质量管理平台, 也是通过安装各种插件来扩展代码检测功
SonarQube Plugin(代码审查插件)

系统管理–> 插件管理–> 选择你需要的

配置 Jenkins 环境

需要配置 sonar 环境,同时要在 sonar 里面生成 Token。请看 sonar 搭建篇。
这里是 jenkins 的系统配置

Jenkins+Gitlab+Sonar 代码检查平台搭建
sonar 的 token 配置
Jenkins+Gitlab+Sonar 代码检查平台搭建
Jenkins+Gitlab+Sonar 代码检查平台搭建
配置 Sonar_Runner
Jenkins+Gitlab+Sonar 代码检查平台搭建

Jenkins 和 Gitlab 免秘钥,同时可以拉取代码

我理解 admin 用户下的 ssh 免秘钥,可以拉取其他用户代码。我拉智标项目可以。(通过这个方法可以快速建立 jenkins 和 gitlab 免秘钥认证。同时实现拉起代码)
Jenkins+Gitlab+Sonar 代码检查平台搭建
Jenkins+Gitlab+Sonar 代码检查平台搭建
JENKINS 里面的配置,注意路径
Jenkins+Gitlab+Sonar 代码检查平台搭建
Jenkins+Gitlab+Sonar 代码检查平台搭建
Jenkins+Gitlab+Sonar 代码检查平台搭建

拉取代码、配置 sonar 配置文件

Jenkins+Gitlab+Sonar 代码检查平台搭建
构建触发 5 分钟执行一次
Jenkins+Gitlab+Sonar 代码检查平台搭建
还有一种只有开发提交到 gitlab 以后才会触发构建,没错呢就是 gitlab 钩子。jenkins 配置只需点一点。
Jenkins+Gitlab+Sonar 代码检查平台搭建
jenkins 失败构建重试
Jenkins+Gitlab+Sonar 代码检查平台搭建
删除旧的构建,不然增量就把目录搞爆了,我之前默认在 var 下,就呵呵了,周末收了 70 多封告警邮件提示构建失败。
Jenkins+Gitlab+Sonar 代码检查平台搭建
构建时选择 Sonar 插件
Jenkins+Gitlab+Sonar 代码检查平台搭建
好了这会可以构建完成了。当然领导说要发邮件给开发,这个需求我觉得蛮合理的。
Jenkins+Gitlab+Sonar 代码检查平台搭建
Jenkins+Gitlab+Sonar 代码检查平台搭建
Jenkins+Gitlab+Sonar 代码检查平台搭建
Jenkins+Gitlab+Sonar 代码检查平台搭建
这个是扩容邮件的插件,里面可以配置成功后发邮件给开发,当然我写了个 html 页面,发个网页过去高大上多了。
Jenkins+Gitlab+Sonar 代码检查平台搭建
哈哈哈哈哈哈
Jenkins+Gitlab+Sonar 代码检查平台搭建
Jenkins+Gitlab+Sonar 代码检查平台搭建
同事的代码结果不方便拿出来晒,怕被打~~~#_#
这个是 jenkins 和 jdk 的安装,其实应该先装 jdk 和 sonar。请看第二篇 soanr

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2017-01/139900p2.htm

搭建 Gitlab

gitlab 免秘钥

ssh-keygen -t rsa
cat id_rsa.pub for gitlab
cat id_rsa for jenkins

配置 gitlab
Jenkins+Gitlab+Sonar 代码检查平台搭建
配置 jenkins
Jenkins+Gitlab+Sonar 代码检查平台搭建

GitHub 教程系列文章

通过 GitHub 创建个人技术博客图文详解  http://www.linuxidc.com/Linux/2015-02/114121.htm

GitHub 使用教程图文详解  http://www.linuxidc.com/Linux/2014-09/106230.htm 

使用 GitHub / GitLab 的 Webhooks 进行网站自动化部署  http://www.linuxidc.com/Linux/2016-06/131993.htm

多个 GitHub 帐号的 SSH key 切换 http://www.linuxidc.com/Linux/2016-05/131080.htm

如何在同一台电脑上使用两个 GitHub 账户 http://www.linuxidc.com/Linux/2016-05/131079.htm

利用 GitHub 搭建个人 Maven 仓库  http://www.linuxidc.com/Linux/2016-04/130197.htm

一分钟认识 GitHub http://www.linuxidc.com/Linux/2015-11/125089.htm

Debian 8 上安装 GitLab http://www.linuxidc.com/Linux/2016-12/137816.htm

分享实用的GitHub 使用教程 http://www.linuxidc.com/Linux/2014-04/100556.htm 

GitHub 使用操作指南  http://www.linuxidc.com/Linux/2016-10/135782.htm

GitHub 的详细介绍:请点这里
GitHub 的下载地址:请点这里

安装 Maven

tar -zvxf apache-maven-3.3.9-bin.tar.gz -C /usr/local/src/

这一段要改

      <id>sonar</id>

      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <!--
      <repositories>
        <repository>
          <id>jdk14</id>
          <name>Repository for JDK 1.4 builds</name>
          <url>http://www.myhost.com/maven/jdk14</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>-->
     <properties>
       <sonar.jdbc.url>jdbc:MySQL://localhost:3306/sonar?useUnicode=true;characterEncoding=utf</sonar.jdbc.url>
       <sonar.jdbc.username>sonar</sonar.jdbc.username>
       <sonar.jdbc.password>******</sonar.jdbc.password>
       <sonar.host.url>http://10.127.96.124:9000</sonar.host.url>

    </properties>
    </profile>

本地的仓储路径记得改一改,默认在根目录下

 <localRepository>/data/path/to/local/repo</localRepository>

Jenkins+Gitlab+Sonar 代码检查平台搭建

跑一个 Java:maven:project 妥妥的。

mvn clean package
mvn sonar:sonar

Jenkins+Gitlab+Sonar 代码检查平台搭建

maven 配置文件 setting
我这边用的自己的私用库

<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user,
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in
 |                 ${maven.home}/conf/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | The sections in this sample file are intended to give you a running start at
 | getting the most out of your Maven installation. Where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 |-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">


  <localRepository>/data/path/to/local/repo</localRepository>


  <!-- interactiveMode
   | This will determine whether maven prompts you when it needs input. If set to false,
   | maven will use a sensible default value, perhaps based on some other setting, for
   | the parameter in question.
   |
   | Default: true
  <interactiveMode>true</interactiveMode>
  -->

  <!-- offline
   | Determines whether maven should attempt to connect to the network when executing a build.
   | This will have an effect on artifact downloads, artifact deployment, and others.
   |
   | Default: false
  <offline>false</offline>
  -->

  <!-- pluginGroups
   | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
   | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
   | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
   |-->
  <pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>

  <!-- proxies
   | This is a list of proxies which can be used on this machine to connect to the network.
   | Unless otherwise specified (by system property or command-line switch), the first proxy
   | specification in this list marked as active will be used.
   |-->
  <proxies>
    <!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
    -->
  </proxies>

  <!-- servers
   | This is a list of authentication profiles, keyed by the server-id used within the system.
   | Authentication profiles can be used whenever maven must make a connection to a remote server.
   |-->
  <servers>
    <!-- server
     | Specifies the authentication information to use when connecting to a particular server, identified by
     | a unique name within the system (referred to by the 'id' attribute below).
     |
     | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
     |       used together.
     |
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
    -->

    <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
  </servers>

  <!-- mirrors
   | This is a list of mirrors to be used in downloading artifacts from remote repositories.
   |
   | It works like this: a POM may declare a repository to use in resolving certain artifacts.
   | However, this repository may have problems with heavy traffic at times, so people have mirrored
   | it to several places.
   |
   | That repository definition will have a unique id, so we can create a mirror reference for that
   | repository, to be used as an alternate download site. The mirror site will be the preferred
   | server for that repository.
   |-->
   <mirrors>
    <mirror>
      <id>ding</id>
      <mirrorOf>yun</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://10.142.164.239/nexus/content/</url>
    </mirror>
   </mirrors>
  <!-- profiles
   | This is a list of profiles which can be activated in a variety of ways, and which can modify
   | the build process. Profiles provided in the settings.xml are intended to provide local machine-
   | specific paths and repository locations which allow the build to work in the local environment.
   |
   | For example, if you have an integration testing plugin - like cactus - that needs to know where
   | your Tomcat instance is installed, you can provide a variable here such that the variable is
   | dereferenced during the build process to configure the cactus plugin.
   |
   | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
   | section of this document (settings.xml) - will be discussed later. Another way essentially
   | relies on the detection of a system property, either matching a particular value for the property,
   | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
   | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
   | Finally, the list of active profiles can be specified directly from the command line.
   |
   | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
   |       repositories, plugin repositories, and free-form properties to be used as configuration
   |       variables for plugins in the POM.
   |
   |-->
  <profiles>
    <!-- profile
     | Specifies a set of introductions to the build process, to be activated using one or more of the
     | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
     | or the command line, profiles have to have an ID that is unique.
     |
     | An encouraged best practice for profile identification is to use a consistent naming convention
     | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
     | This will make it more intuitive to understand what the set of introduced profiles is attempting
     | to accomplish, particularly when you only have a list of profile id's for debug.
     |
     | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.-->
    <profile>
      <id>sonar</id>

      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <!--
      <repositories>
        <repository>
          <id>jdk14</id>
          <name>Repository for JDK 1.4 builds</name>
          <url>http://www.myhost.com/maven/jdk14</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>-->
     <properties>
       <sonar.jdbc.url>jdbc:mysql://localhost:3306/sonar?useUnicode=true;characterEncoding=utf</sonar.jdbc.url>
       <sonar.jdbc.username>sonar</sonar.jdbc.username>
       <sonar.jdbc.password>******</sonar.jdbc.password>
       <sonar.host.url>http://10.127.96.124:9000</sonar.host.url>

    </properties>
    </profile>

    <!--
     | Here is another profile, activated by the system property 'target-env' with a value of 'dev',
     | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
     | might hypothetically look like:
     |
     | ...
     | <plugin>
     |   <groupId>org.myco.myplugins</groupId>
     |   <artifactId>myplugin</artifactId>
     |
     |   <configuration>
     |     <tomcatLocation>${tomcatPath}</tomcatLocation>
     |   </configuration>
     | </plugin>
     | ...
     |
     | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
     |       anything, you could just leave off the <value/> inside the activation-property.
     |
    <profile>
      <id>env-dev</id>

      <activation>
        <property>
          <name>target-env</name>
          <value>dev</value>
        </property>
      </activation>

      <properties>
        <tomcatPath>/path/to/tomcat/instance</tomcatPath>
      </properties>
    </profile>
    -->
     <profile>  
      <id>profile-nexus</id> 
      <repositories>  
        <repository>  
          <id>nexus</id>  
          <url>http://10.142.164.239/nexus/content/groups/public/</url>  
          <snapshots><enabled>true</enabled></snapshots>  
          <releases><enabled>true</enabled></releases>  
        </repository>  
      </repositories>  
      <pluginRepositories>  
        <pluginRepository>  
          <id>nexus</id>  
          <url>http://10.142.164.239/nexus/content/groups/public/</url>  
          <snapshots><enabled>true</enabled></snapshots>  
          <releases><enabled>true</enabled></releases>  
        </pluginRepository>  
      </pluginRepositories>  
    </profile>  
  </profiles>  

 <activeProfiles>  
  <activeProfile>profile-nexus</activeProfile>  
</activeProfiles>   
</settings>

Nexus 安装与配置  http://www.linuxidc.com/Linux/2016-09/135084.htm

CentOS 安装 Nexus(Maven 私有库)详细配置及上传本地 jar 到私服  http://www.linuxidc.com/Linux/2016-11/136954.htm

Ubuntu 下 搭建 Nexus Maven 私服中央仓库  http://www.linuxidc.com/Linux/2016-08/133936.htm

Linux 下使用 Nexus 搭建 Maven 私服详解 http://www.linuxidc.com/Linux/2016-08/134630.htm

Linux 下使用 Nexus 搭建 Maven 私服 http://www.linuxidc.com/Linux/2016-08/134617.htm

Linux 下安装配置 Nexus http://www.linuxidc.com/Linux/2016-09/135083.htm

maven 项目的 pom.xml 文件要修改

<sonar.language>Java</sonar.language>
<sonar.jacoco.itReportPath>${project.basedir}/reports/jacoco.exec</sonar.jacoco.itReportPath>

配置 jenkins

配置 enable 的邮件插件

配置 job

最坑的是要改 hosts(纯属集团邮件服务器问题~)

来来来 代码走起我们测发邮件

yum -y install sendmail
service sendmail restart

夺伟的 email 发送脚本 直接 copy 了,懒得改了

#!/bin/bash
source /etc/profile
cd `dirname $0`
if [$# -ne 5 ];
then
    echo "There are must be 5 parameters: userName groupName entrances_ip passwd" 
    exit 1
fi

userName=$1
groupName=$2
entrances_ip=$3
passwd=$4
ccPerson=$5

ipsStr=''
OLD_IFS="$IFS"
    IFS=","
    ENTRANCES_NUM_ARR=($entrances_ip)
    IFS="$OLD_IFS"

    for entrance_ip in ${ENTRANCES_NUM_ARR[@]}
      do
        ipsStr=$ipsStr"【"$entrance_ip"】"
    done


#From: 叶夺伟 <yeduowei@le.com>           
toUser=$userName"@le.com"
cccUser='yeduowei@le.com'
ccUser='dingyunlong@le.com'
/usr/sbin/sendmail -t <<EOF
From: 大数据部 <data-service@le.com>           
To: $toUser                                       
Cc: $ccUser,$ccPerson,$cccUser
Subject: 集群账户开通                                          
你好:已在下列入口机中 $ipsStr 建立账户【$userName 所属组(队列) $groupName】密码 : $passwd


EOF

结果是可以收到邮件,看来 jenkins 配置的有问题
Jenkins+Gitlab+Sonar 代码检查平台搭建

继续排查,把默认邮件也配上, 没权限,就是这么坑
Jenkins+Gitlab+Sonar 代码检查平台搭建

最后发现问题在哪了,我之前还解决了,真是三天不学习就忘了


注意:jenkins 框架中,在填写邮箱地址的时候,填写的发送邮件的邮箱的地址信息必须与系统管理员的邮箱地址信息一致,不然无法成功发送邮件。

Jenkins+Gitlab+Sonar 代码检查平台搭建

上图 跟着节奏走

Jenkins+Gitlab+Sonar 代码检查平台搭建

 这里写图片描述

Java_maven 项目 sonar 代码检查

这里注意不要画蛇添足,跟着配置走就行,另外构建失败要删除 workspace 里面的 job,我之前在里面手动编译的,自己把自己坑了。
上图

结果发邮件出来了
Jenkins+Gitlab+Sonar 代码检查平台搭建

sonar 平台结果
Jenkins+Gitlab+Sonar 代码检查平台搭建

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计20031字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中