sonar+jenkins安装步骤

sonar的安装

1.https://www.sonarqube.org/downloads/ 下载社区版。

2.进入下载解压好的目录中的bin文件夹

3.根据操作系统选择进入不同的文件夹

4.通过命令行模式启动sonar,命令为

sonar.sh start

5.启动完成后,在浏览器中输入 http://localhost:9000/ 可直接访问。(login的初始账号密码为admin/admin)

可能会有的坑:直接运行 sonar.sh start可能会导致该command不存在,所以可以采用 ./sonar.sh start。 停止使用./sonar.sh stop

  ./sonar.sh restart  重启

6.安装mysql:

mysql的版本需要查看sonar.properties中的注释,每个版本的sonar所要求的不一样。去mysql官网下载安装文件,运行mysql -u root -p时会有command not found的提示,这是因为没有做映射。。只需要把mysql安装目录,比如MYSQLPATH/bin/mysql,映射到/usr/local/bin目录下:

cd /usr/local/bin

ln -fs /usr/local/mysql-8.0.11-macos10.13-x86_64/bin/mysql mysql

进入mysql

mysql -u -root -p

8.0以上版本的mysql修改密码

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '密码';

mac上还能使用该命令进行安装。

1):brew install mysql (推荐使用该命令)

2):安装完成后使用: sudo mysql.server start 启动mysql服务。  sudo mysql.server stop \restart

3):mysql需要进行基本配置。

$ mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords

and improve security. It checks the strength of password

and allows the users to set only those passwords which are

secure enough. Would you like to setup VALIDATE PASSWORD plugin?

// 提示是否设置密码

Press y|Y for Yes, any other key for No: y

// 提示选择密码强度等级

There are three levels of password validation policy:

LOW    Length >= 8

MEDIUM Length >= 8, numeric, mixed case, and special characters

STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1

Please set the password for root here.

// 按照所选的密码强度要求设定密码

New password:

Re-enter new password:

// 提示密码强度50,不符合要求重新设置密码

Estimated strength of the password: 50

Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y

... Failed! Error: Your password does not satisfy the current policy requirements

New password:

Re-enter new password:

// 提示密码强度100,符合要求继续进行

Estimated strength of the password: 100

Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y

By default, a MySQL installation has an anonymous user,

allowing anyone to log into MySQL without having to have

a user account created for them. This is intended only for

testing, and to make the installation go a bit smoother.

You should remove them before moving into a production

environment.

// 提示删除默认无密码用户

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y

Success.

Normally, root should only be allowed to connect from

'localhost'. This ensures that someone cannot guess at

the root password from the network.

// 提示禁止远程root登录

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : no

... skipping.

By default, MySQL comes with a database named 'test' that

anyone can access. This is also intended only for testing,

and should be removed before moving into a production

environment.

// 提示删除默认自带的test数据库

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y

- Dropping test database...

Success.

- Removing privileges on test database...

Success.

Reloading the privilege tables will ensure that all changes

made so far will take effect immediately.

// 提示是否重新加载privilege tables

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y

Success.

All done!

测试数据库是否能链接成功。。。mysql都有一个root用户,密码就为刚才配置数据库时所设置的密码。

4):通过该连接新建一个数据库,数据库名称可任意指定。

配置sonar连接数据库:

1.打开SonarQube安装目录下的sonar.properties文件,目录为:

/path to your SoaneQube location directory/conf/sonar.properties

2.在sonar.properties文件中mysql下oracle前添加如下信息

sonar.jdbc.url=jdbc:mysql:/localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

sonar.jdbc.username=root

sonar.jdbc.password=password

然后需要重启sonar,,,  /.sonar.sh restart

ps:sonar.jdbc.url是你的mysql数据库连接url,sonar.jdbc.username是mysql数据库用户名,sonar.jdbc.password是数据库用户密码,加粗代表的是数据库名称。使用navicat建立新库。

连接mysql 报错 Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

网上找不到朋友说是因为非正常关机导致,mysql.server start 运行报错 ERROR! The server quit without updating PID file();

解决办法 :进入 cd /usr/local/var/mysql 删除 :rm -rf ib_logfile0 运行:sudo mysql.server restart

配置sonar-scanner:

1.下载sonar-scanner。

1):下载完成后打开Sonar-Scanner目录下sonar-runner.properties文件,目录如:

/Users/cw/Documents/devtools/sonar/sonar-scanner-2.8/conf/sonar-scanner.properties

2):在mysql节点下添加如下信息

sonar.jdbc.url=jdbc:mysql:/localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

    sonar.jdbc.username=root

    sonar.jdbc.password=password

ps:

注意:如果待测试项目与SonarQube服务不在同一台机器,则需要添加SonarQube服务的IP:

    #----- Default SonarQube server

    sonar.host.url=http://XXX.XXX.XXX.XXX:9000

2.配置环境变量:

vim ~/.bash_profile

    在文件末尾添加如下字段:

    export SONAR_RUNNER_HOME=path to your Sonar-Scanner directory

    export PATH=$PATH:$SONAR_RUNNER_HOME/bin

    保存修改

    使配置立即生效:source ~/.bash_profile

3.测试Sonar-Scanner

打开终端输入如下命令

  sonar-runner -version

运行sonar:

1.打开待测试的项目根目录,新建sonar-project.properties文件并输入如下信息

# must be unique in a given SonarQube instance

    sonar.projectKey=cypressE2E

    # this is the name displayed in the SonarQube UI

    sonar.projectName=i2work-operation1

    sonar.projectVersion=1.0

    # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.

    # Since SonarQube 4.2, this property is optional if sonar.modules is set.

    # If not set, SonarQube starts looking for source code from the directory containing

    # the sonar-project.properties file.

    sonar.sources=.

    # Encoding of the source code. Default is default system encoding

    #sonar.sourceEncoding=UTF-8

    #path to your project build output path

sonar.projectKey 任意命名

sonar.projectName=i2work-operation1 sonar中所显示的项目名称

sonar.sources=.  目标目录,该文件与项目根目录在同个地方的话,就使用 .

2.打开终端,进入待测试项目根目录,执行如下命令:

sonar-runner

经过等待,然后就可以用在sonar页面上看到项目代码的扫描结果了。

配置jenkins

1.在插件管理中心添加 SonarQube Scanner

2.在系统管理-系统设置中,设置sonar名称与服务地址:

我这里是本地环境。

3.在系统管理-全局工具设置中配置sonar

1.在代码执行之前添加新的构建任务。

完成!


可能会有的坑:

启动sonar后,无法访问url,查看sonar下的logs文件夹中的log文件,可以查看到以下报错。

解决办法:

删除sonar下的temp。(注意做好该temp目录的备份,以防万一)

你可能感兴趣的:(sonar+jenkins安装步骤)