一、git-gitee——10.0.0.108

yum install -y git
#创建项目并初始化数据
[root@rocky8 ~]$ git config --global user.name "机智的z同学"
[root@rocky8 ~]$ git config --global user.email "2298408548@qq.com"

[root@rocky8 ~]$ mkdir helloword
[root@rocky8 ~]$ cd helloword
[root@rocky8 ~/helloword]$ git init
[root@rocky8 ~/helloword]$ ls -a
.  ..  .git
[root@rocky8 ~/helloword]$ tree .git
echo hello README.md  工作区work
git add README.md        暂存区index
git commit -m "first commit"    本地仓库

git remote add origin https://gitee.com/a-witty-z-classmate/helloword1.git    添加远程仓库
git push -u origin master        把本地仓库的master分支推送到远程仓库master分支

为此在向远程库推送的时候,要先进行pull同步远程仓库,让本地新建的库和远程库进行同步。

二、gitlab——10.0.0.55

yum install -y gcc iproute2 ntpdate tcpdump telnet traceroute lrzsz tree openssl iotop zip unzip ipmitool nfs-kernel-server nfs-common libssl-dev libpcre3 libpcre3-dev zlib1g-dev openssh-server

wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu/pool/bionic/main/g/gitlab-ce/gitlab-ce_14.1.0-ce.0_amd64.deb
dpkg -i gitlab-ce_14.1.0-ce.0_amd64.deb

vim /etc/gitlab/gitlab.rb
external_url 'http://gitlab.zyy.com'
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "2298408548@qq.com"
gitlab_rails['smtp_password'] = "wbswllfqjbqgdjbj"
gitlab_rails['smtp_domain'] = "qq.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_from'] = "2298408548@qq.com"
gitlab_rails['initial_root_password'] = "12345678"

vim /etc/hosts
10.0.0.55       gitlab.zyy.com

gitlab-ctl reconfigure    #每次修改完配置文件都需要执行配置并启动服务
gitlab-ctl status

浏览器访问http://10.0.0.55

1.关闭账号注册功能
2.修改邮箱地址(验证时修改本地hosts文件10.0.0.55 gitlab.zyy.com后才可以跳转)
3.创建用户yuanyuan9810@outlook.com(12345678)
4.创建组
5.创建项目
6.将用户添加到组或项目

在linux客户端测试clone和提交项目

vim /etc/hosts    (10.0.0.55 gitlab.zyy.com)

#在linux客户端测试clone
git clone http://gitlab.zyy.com/study/testproject1.git (yuanyuan9810@outlook.com/12345678)

#修改代码和提交项目
cd testproject1/    
git add .;git commit -m "modify README"
git push

develop的角色修改代码时需创建分支
git checkout -b dev
echo "<h1>v3.0</h1>" >index.html
git add .;git commit -m "v2.0"
git push --set-upstream origin dev

基于key验证

基于key验证(--加入客户端公钥)
git remote remove origin
git remote add origin git@gitlab.zyy.com:study/testproject1.git        #重新建立连接
git checkout -b dev
git push -u origin dev

合并分支

developer只能提交合并申请,不能确认
owner才可以同意合并

数据备份和恢复

gitlab-ctl backup-etc -p /root/        #备份配置文件
tar -xvf gitlab_config_1639103860_2021_12_10.tar -C /        #还原配置文件

gitlab-backup create        #备份数据
ll /var/opt/gitlab/backups/        #默认保存位置

在http://10.0.0.55删除项目后:
gitlab-ctl stop puma
gitlab-ctl stop sidekiq
gitlab-backup restore BACKUP=1639115251_2021_12_10_14.1.0
gitlab-ctl reconfigure
gitlab-ctl 
浏览器访问http://10.0.0.55

三、jenkens——10.0.0.28

jenkins基于JAVA实现,安装jenkins前需要先安装 JDK

dnf -y install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64
wget --no-check-certificate https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat/jenkins-2.290-1.1.noarch.rpm
yum -y install ca-certificates
rpm -i jenkins-2.290-1.1.noarch.rpm;systemctl start jenkins
cat /var/log/jenkins/jenkins.log

浏览器访问http://10.0.0.28:8080/
选择不安装插件,后续定制安装
修改密码(admin/12345678)

命令行管理

java -jar jenkins-cli.jar -s http://10.0.0.28:8080/ -webSocket help
java -jar jenkins-cli.jar -s http://admin:12345678@10.0.0.28:8080/ -webSocket list-jobs
java -jar jenkins-cli.jar -s http://admin:12345678@10.0.0.28:8080/ -webSocket list-plugins

插件安装源配置

ls /var/lib/jenkins/plugins/  #插件目录(可以拷贝稳定版的插件目录来安装)

系统自带的安装路径太慢嘞,改为国内镜像源
①查看镜像源:http://mirrors.jenkins-ci.org/status.html
默认:https://updates.jenkins.io/update-center.json
改为:https://mirrors.aliyun.com/jenkins/updates/update-center.json
         https://mirror.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
     https://jenkins-zh.gitee.io/update-center-mirror/tsinghua/update-center.json
wget https://mirror.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json    #但依旧内容是国内源

②sed -i.bak 's#https://updates.jenkins.io/download#https://mirror.tuna.tsinghua.edu.cn/jenkins#g' /var/lib/jenkins/updates/default.json    #国内源
浏览器访问http://10.0.0.28:8080/restart    #重启

创建和构建job

ls /var/lib/jenkins/workspace/    #jobs的存放目录
ll /var/lib/jenkins/workspace/helloword-job    #jobs运行shell时的路径 (建议将Jenkins的user改为root,拥有大权限)

jenkins中环境变量:env > env.txt
EXECUTOR_NUMBER=0
LANG=en_US.UTF-8
BUILD_ID=4
OLDPWD=/var/lib/jenkins/workspace/helloword-job
INVOCATION_ID=7bbae34ddad14965ae04dd407e4a3214
JOB_BASE_NAME=helloword-job
WORKSPACE_TMP=/var/lib/jenkins/workspace/helloword-job@tmp
CI=true
HUDSON_COOKIE=61db09b0-84dc-49f2-9720-ac114667afaf
USER=jenkins
BUILD_NUMBER=4
WORKSPACE=/var/lib/jenkins/workspace/helloword-job
PWD=/var/lib/jenkins/workspace
HUDSON_URL=http://10.0.0.28:8080/
HOME=/var/lib/jenkins
NODE_NAME=master
JOURNAL_STREAM=9:44633
HUDSON_SERVER_COOKIE=72d8bd382034b338
JENKINS_HOME=/var/lib/jenkins
JOB_NAME=helloword-job
HUDSON_HOME=/var/lib/jenkins
BUILD_URL=http://10.0.0.28:8080/job/helloword-job/4/
SHELL=/bin/bash
JOB_URL=http://10.0.0.28:8080/job/helloword-job/
BUILD_DISPLAY_NAME=#4
SHLVL=3
BUILD_TAG=jenkins-helloword-job-4
NODE_LABELS=master
LOGNAME=jenkins
PATH=/sbin:/usr/sbin:/bin:/usr/bin
JENKINS_SERVER_COOKIE=72d8bd382034b338
_=/bin/env

用户项目权限管理分配(插件role-base)

1.安装角色权限相关的插件role-base 
2.创建一个全局角色read-role,分配角色读权限;给用户zhu、zhao选择read-role角色赋予读权限
3.创建项目角色testproject1-role、testproject2-role并分配角色各权限;给用户zhu、zhao分配项目角色实现分工

邮箱配置(插件mailer)

1.配置jenkins管理员邮箱2298408548@qq.com(Jenkins—系统管理—系统设置)
2.安装邮箱插件mailer后配置发件信息(2298408548@qq.com wbswllfqjbqgdjbj smtp.qq.com)

实现可视化(插件blue ocean)

实例:幸运大转盘

shell构建

image.png

10.0.0.55   gitlab                10.0.0.7   web1
10.0.0.28        genkins                10.0.0.17  web2
10.0.0.108  git(developer)

1.gitlab从url上导入项目:https://gitee.com/doudoujun2017/jq-luck-draw

2.开发人员从gitlab上拉代码,改写后上传
cd jenkinstest/
① git init;git config --global user.name "zhuayuanyuan";git config --global user.email "2298408548@qq.com";
② ssh-keygen实现基于key验证(zhuyy@jinkens)
③ git clone git@gitlab.zyy.com:test/luck-draw.git
  scp -r * 10.0.0.7:/usr/share/nginx/html/    #(cd luck-draw)网页浏览10.0.0.17 
④ vim js/index.js 
  git commit -am "change item v1.0";git push    #(cd jenkistest/luck-draw)

3.在jenkins上新建任务
① ssh-keygen实现基于key验证(root@jinkens)
② git clone git@gitlab.zyy.com:test/luck-draw.git (在jinkens服务端测试可以实现,修改jenkins的运行user为root)
③ 新建任务-->shell构建-->立即构建(控制台查看输出):/var/lib/jenkins/workspace/luck-draw-job(执行目录)
rm -rf ${WORKSPACE}/*
git clone git@gitlab.zyy.com:test/luck-draw.git            #从gitlab上拉取代码
tar -C luck-draw -cf ${WORKSPACE}/luck-draw.tar .   #将新代码打包
scp -r luck-draw.tar 10.0.0.7:/usr/share/nginx/            #将打包的新代码传至web服务端
ssh 10.0.0.7 "rm -rf /usr/share/nginx/html/* && tar xf /usr/share/nginx/luck-draw.tar -C /usr/share/nginx/html/"                                                     #在web服务端解包并并放至html目录下

4.开发人员继续修改代码并上传;在jenkins上立即构建,查看效果
vim js/index.js
cd ..
git commit -am "change item v2.0";git push

5.回滚到v1.0版本
git revert HEAD (以进v3.0为退v1.0)
git push

调用脚本

mkdir -p jenkins/scrips
cd  jenkins/scrips
vim luck-draw.sh                    #将shell构建的命令写在脚本里面
#!/bin/bash
WORKSPACE=/var/lib/jenkins/workspace/luck-draw-job
rm -rf ${WORKSPACE}/*
git clone git@gitlab.zyy.com:group1/luck-draw.git
tar -C luck-draw -cf ${WORKSPACE}/luck-draw.tar . 
scp -r luck-draw.tar 10.0.0.7:/usr/share/nginx/
ssh 10.0.0.7 "rm -rf /usr/share/nginx/html/* && tar xf /usr/share/nginx/luck-draw.tar -C /usr/share/nginx/html/"
git commit -am "change item v4.0";git push

新建任务-->shell构建-->立即构建
bash +x /root/jenkins/scrips/luck-draw.sh

vim js/index.js;     #开发人员继续修改代码并上传
git commit -am "change item v2.0";git push

与gitlab建立连接(插件gitlab)

部署nginx项目

1.安装gitlab插件
2.基于key验证,添加凭据git@gitlab.zyy.com:group1/luck-draw.git
👉实现了从gitlab上拉取代码到WORKSPASE:/var/lib/jenkins/workspace/luck-draw-job(不再创建子目录)

[root@rocky8 ~/jenkistest/luck-draw]$ vim js/index.js 
[root@rocky8 ~/jenkistest/luck-draw]$ git commit -am "change item v3.0";git push

vim jenkins/scrips/luck-draw2.sh
#!/bin/bash
WORKSPACE=/var/lib/jenkins/workspace/luck-draw-job
#rm -rf ${WORKSPACE}/*(jenkins会自动帮我们清空呦,真棒!)
tar -cf luck-draw.tar .                           
scp -r luck-draw.tar 10.0.0.7:/usr/share/nginx/                 
ssh 10.0.0.7 "rm -rf /usr/share/nginx/html/* && tar xf /usr/share/nginx/luck-draw.tar -C /usr/share/nginx/html/"
3.执行shell
bash +x /root/jenkins/scrips/luck-draw2.sh

部署tomcat项目

1.下载安装tomcat
git clone https://gitee.com/lbtooth/hello-world-war.git
tar -C hello-world-war/src/main/webapp/ -cf hello.tar .
scp hello.tar 10.0.0.7:/usr/local/tomcat/webapps
cd /usr/local/tomcat/webapps/
tar -xf hello.tar hello
访问:http://10.0.0.7:8080/hello/

2.建立本地仓库代码push到gitlab
git remote remove origin
git remote add origin git@gitlab.zyy.com:group1/helloword-war.git
git add .
git commit -m "change v1"
git push -u origin master

3.jenkins建立计划任务
mkdir tarfile 
mkdir xtarfile
#!/bin/bash
DATE=`date +%F-%s`
HOST_LIST="
10.0.0.7
"
tar -C $WORKSPACE/src/main/webapp/ -cf helloword.tar .
for host in ${HOST_LIST};do
scp -r helloword.tar $host:/root/tarfile/helloworld-war-${DATE}.tar
ssh $host  " mkdir /root/xtarfile/helloworld-war-${DATE} && \
             tar xf /root/tarfile/helloworld-war-${DATE}.tar -C /root/xtarfile/helloworld-war-${DATE} && \
             rm -rf /usr/local/tomcat/webapps/helloworld && \
             ln -s /root/xtarfile/helloworld-war-${DATE} /usr/local/tomcat/webapps/helloworld
             systemctl restart tomcat"
done

shell构建-->立即构建:bash -x /root/jenkins/scrips/helloworld.sh
访问:http://10.0.0.7:8080/helloworld/

实现项目的回滚

#实现回滚
current_version=`readlink /usr/local/tomcat/webapps/helloworld`                                     #当前版本
pre_version=$(ls /root/xtarfile | grep -B1 `basename $current_version` |head -n1)     #取出前一版本
rm -f /usr/local/tomcat/webapps/helloworld                                                                                     #删除软连接
ln -s /root/xtarfile/$pre_version /usr/local/tomcat/webapps/helloworld                            #重键软连接

参数化构建

vim deploy-rollback.sh

1.布尔值参数(deploy)

2.选项参数(ops:选项+描述更直观)
ops:deploy rollback
branch:main dev

3.字符参数(arg:灵活指定参数)
branch:默认值(main)-->指定分支($branch)

image.png
image.png

git参数构建(插件git parameter)

git tag v1.0
git push origin --tags

1.安装插件git parameter
2.git参数构建(tag)

编译部署java应用(maven和tomcat插件)

make —-> .configure
maven —-> pom.xml

1.配置tomcat(创建管理用户名和密码admin/12345678)
vim webapp1/host-manager/META-INF/context.xml 
vim conf/tomcat-users.xml
(必须有manager-script和manager-gui角色)

2.安装maven和tomcat插件
插件:maven integration
插件:deploy to container

3.安装编译工具maven,并配置镜像加速
apt install -y maven
mvn --version

vim /etc/maven/settings.xml
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

4.全局工具配置jdk和maven
Maven home: /usr/share/maven
JAVA_HOME:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.312.b07-1.el8_4.x86_64/    #java的工作目录

5.创建tomcat全局凭证(基于用户名和密码)

6.创建一个maven项目
① 源码管理git repositories
② 构建环境,输入maven编译配置
    clean package -Dmaven.test.skip=true    # (Goals and options)
    mv target/hello-world-war-1.0.0.war target/hellomaven.war    #(post-steps)
③ 构建后操作,配置tomcat

7.测试:http://10.0.0.7:8080/hellomaven/

构建webhook触发器

①触发远程构建

图形界面:http://10.0.0.28:8080/job/helloword-war-maven-job/build?token=666666(因为cookie有记录)
curl命令:http://admin:12345678@10.0.0.28:8080/job/helloword-war-maven-job/build?token=666666

②GitLab webhook URL

http://10.0.0.28:8080/job/webhooktest-job/
684171ad3ae923ed17acc710f5d6d920

③定时构建

④轮询SCM

1.源码管理(git库)
2.构建触发器(①②③④)
Build when a change is pushed to GitLab-----Secret token

3.配置 gitlab webhook(在admin设置允许Webhook和服务对本地网络的请求)

4.验证:更改代码上传是否自动构建
报错:Hook executed successfully but returned HTTP 403

①进入jenkins:Manage Jenkins- >Configure Global Security -> 授权策略 -> Logged-in users can do anything (登录用户可以做任何事情) 点选 -> 匿名用户具有可读权限 点选

②去掉跨站点请求伪造,点选放开。Manage Jenkins- >Configure Global Security->CSRF Protection(跨站请求伪造保护)
 高版本jenkins不能界面禁用跨站请求伪造保护
    vim /etc/sysconfig/jenkins
    JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true -Dhudson.security.csrf.GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION=true"

③去掉Gitlab enable authentication 点选放开。系统管理 -> 系统设置 -> Enable authentication for '/project' end-point

构建后项目关联

  1. 在前面任务中利用构建后操作关联后续任务
  2. 在后面任务中利用构建触发器关联前面任务

    构建后通知

    邮件通知

    配置系统管理员邮箱地址-->配置邮件通知
    安装mailer插件
    增加构建后操作步骤---E-mail notification
    

    钉钉通知

    ```bash 1.注册钉钉,创建群聊 2.智能群助手添加机器人:通过webhook接入自定义服务 ①复制加签内容 SEC556712d8df817607a85775522a2efc532ee909bf04ebc057c4d29892dc9b8436 ②复制Webhook信息 https://oapi.dingtalk.com/robot/send? access_token=7e29cf202a14849572149ed98f8506d7b2096d1d1750892d7ad41eacf019b65c

3.Jenkins安装插件dingtalk,系统配置里配置钉钉

4.配置任务实现钉钉通知 General—>钉钉机器人

5.执行任务验证钉钉通知

<a name="KGFBv"></a>
## 四、jenkens高级功能
<a name="tHGsB"></a>
### 🔣jenkens主从分布式
![image.png](https://cdn.nlark.com/yuque/0/2021/png/12467445/1639584468889-d9c0d9c8-010c-4023-a67a-dd52aa06d88d.png#clientId=ud9eb793b-365b-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=338&id=ubd6c36af&margin=%5Bobject%20Object%5D&name=image.png&originHeight=841&originWidth=1275&originalType=binary&ratio=1&rotation=0&showTitle=false&size=132834&status=done&style=none&taskId=u8cea7822-78ad-4d5c-82b5-414872b9ad3&title=&width=512.5)
```bash
1.安装java环境,安装Jenkins
2.互相实现基于key验证

3.配置master管理节点
① 安装插件ssh-build agents
② 节点管理添加从节点
    slave1:10.0.0.18  slave1-tag
  slave2:10.0.0.8        slave2-tag
③ 构建任务测试,限制项目的运行节点

4.配置gitlab和slave(节点和jenkins节点的环境要一致,包括:git,mvn,ssh等)
yum -y install git tar
①在slave上配置gitlab的域名解析;
②在gitlab上配置与slave基于key的验证
③将需要执行脚本拷贝至slave
④在slave上配置与web服务器基于key的验证

🔣pipeline流水线

代码克隆 →编译打包(make/maven) →复制到远程web服务器上 →停止服务器旧版本,指向新版本

######################脚本式(最外层是node)########################
node {                                            
    stage('Get code') {
        echo '获取代码'
    }
    stage('Build') {
        echo '构建项目代码'
    }
    stage('Test') {
        echo '测试项目功能'
    }
    stage('Deploy') {
        echo '部署项目'
    }
}

#####################声明式(最外层是 pipeline)########################

pipeline {                                                #流水线的最外层结构,代表整条pipeline
    agent any                                                #每个node都是一个jenkins节点是执行step的具体服务器
    stages {                                                #stages,用于包含所有stage的定义
        stage('获取代码') {                     #stage,任务有哪些功能阶段组成,每个stage都是一个操作
             steps {                                            #steps,每个功能阶段需要执行哪些命令,是jenkins pipeline最基本的操作单元
                echo '获取代码'
                }
            }
        stage('构建代码') {
            steps {
                echo '构建项目代码'
                }
            }
        stage('代码测试') {
            steps {
                echo '测试项目功能'
                }
            }
        stage('项目部署') {
                steps {
                echo '部署项目'
                }
            }
        }
}

1. 脚本部署(Pipeline Script)

1.安装pipeline插件
2.创建流水线任务
3.点击流水线语法,可以自动生成拉取代码的pipeline脚本
4.指定node节点运行
node("slave1-tag") {                                        
    stage('克隆代码') {
        sh 'rm -rf /var/lib/jenkins/workspace/pipeline-job/*'
        git credentialsId: '28571e06-15ae-4156-8834-3ef3c091d336', url: 'git@gitlab.zyy.com:group1/helloword-war.git'
    }
    stage('代码打包') {
        sh 'cd /var/lib/jenkins/workspace/pipeline-job/src/main/webapp/ && tar czf /root/hello-code.tar.gz *'
    }
    stage('代码复制') {
        sh 'scp -r /root/hello-code.tar.gz 10.0.0.7:/root/tarfile/'
    }
    stage('停止tomcat服务') {
        sh 'ssh 10.0.0.7 "systemctl stop tomcat"'
    }
    stage('代码部署') {
        sh 'ssh 10.0.0.7 "rm -rf /usr/local/tomcat/webapp1/hello-code/* && cd /root/tarfile/ && tar xf hello-code.tar.gz -C /usr/local/tomcat/webapp1/hello-code && cp -r /usr/local/tomcat/webapp1/hellomaven/META-INF/ /usr/local/tomcat/webapp1/hello-code"'
    }
    stage('启动tomcat服务') {
        sh 'ssh 10.0.0.7 "systemctl start tomcat"'
    }
}

2. Jenkinsfile文件部署(SCM)

1.将部署脚本写入Jenkinsfile文件,提交至gitlab仓库
2.创建流水线任务,选择Pipeline script from SCM,并指明相对脚本路径和git仓库
手动
git clone git@gitlab.zyy.com:group1/spring-boot-helloWorld.git
vim src/main/java/com/neo/controller/HelloWorldController.java
mvn clean package -Dmaven.test.skip=true
scp target/spring-boot-helloworld-0.9.0-SNAPSHOT.jar 10.0.0.7:/usr/local/tomcat/webapp1/
mv spring-boot-helloworld-0.9.0-SNAPSHOT.jar spring-boot-helloworld.jar
java -jar spring-boot-helloworld.jar --server.port=8888

pipeline部署

input 指令

if条件判断

Parallel 并行执行

Post通知(Email Extension 插件 )

安装插件Email Extension
系统管理--系统配置--配置邮件信息
在pipeline中定制邮件通知

🔣视图

列表视图
我的视图
创建pipeline视图(安装插件build pipeline)

五、代码质量测试SonarQube

1.安装jdk

2.创建用户,修改内核参数

apt -y install openjdk-11-jdk
useradd -s /bin/bash -m sonarqube

vim /etc/sysctl.conf 
vm.max_map_count=262144  #限制一个进程可以拥有的VMA(虚拟内存区域)的数量
fs.file-max=65536                 #系统中可以同时打开的文件数目

vim /etc/security/limits.conf
sonarqube - nofile 65536
sonarqube - nproc 4096
root - nofile 65536
root - nproc 4096
#如果以systemd 运行SonarQueb,需要在service文件配置
[servcie]
.....
LimitNOFILE=65536
LimitNPROC=4096

3.安装数据库pgsql

apt -y install postgresql

vim /etc/postgresql/10/main/postgresql.conf        # 修改监听端口
listen_addresses = '0.0.0.0'            

vim /etc/postgresql/10/main/pg_hba.conf                #接受远程连接
# IPv4 local connections:
host    all             all             10.0.0.0/24            md5

systemctl restart postgresql;systemctl status postgresql

su - postgres                                #使用postgres用户登录(PostgresSQL安装后会自动创建postgres用户,无密码)
psql -U postgres                        #登录postgresql数据库
CREATE DATABASE sonarqube;    #创建用户和数据库并授权
CREATE USER sonar WITH ENCRYPTED PASSWORD '123456';
GRANT ALL PRIVILEGES ON DATABASE sonarqube TO sonar;
ALTER DATABASE sonarqube OWNER TO sonar;
\l
\q

4.下载sonarqube

wget -P /usr/local/src https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-8.9.5.50698.zip

unzip sonarqube-8.9.5.50698.zip
ln -s /usr/local/src/sonarqube-8.9.5.50698 /usr/local/sonarqube

chown -R sonarqube.sonarqube /usr/local/sonarqube/

vim /usr/local/sonarqube/conf/sonar.properties
sonar.jdbc.username=sonar
sonar.jdbc.password=123456
sonar.jdbc.url=jdbc:postgresql://10.0.0.5/sonarqube
sonar.web.host=0.0.0.0 #此为默认值
sonar.web.port=9527 #默认值9000

su - sonarqube    #sonar需要调用elasticsearch,而且默认需要使用普通用户启动,如果以root启动会报错
/usr/local/sonarqube/bin/linux-x86-64/sonar.sh start
/usr/local/sonarqube/bin/linux-x86-64/sonar.sh status
/usr/local/sonarqube/bin/linux-x86-64/sonar.sh stop

创建service文件(参考https://docs.sonarqube.org/7.9/setup/operate-server/)
vim /etc/systemd/system/sonarqube.service 
[Unit]
Description=SonarQube service
After=syslog.target network.target

[Service]
Type=simple
User=sonarqube
Group=sonarqube
PermissionsStartOnly=true
ExecStart=/usr/bin/nohup /usr/bin/java -Xms32m -Xmx32m -Djava.net.preferIPv4Stack=true -jar /usr/local/sonarqube/lib/sonar-application-8.9.5.50698.jar
StandardOutput=syslog
LimitNOFILE=65536
LimitNPROC=4096
TimeoutStartSec=5
Restart=always

[Install]
WantedBy=multi-user.target

systemctl enable --now sonarqube.service;systemctl status sonarqube.service

5.登录web界面:http://10.0.0.5:9000

初始登录密码:admin/admin ---> admin/12345678
1.安装中文插件 /usr/local/sonarqube/extensions/plugins/
 ① administration- Marketplace,安装插件chinese(ダメだ!)
 ② git clone https://gitee.com/huangmo2017/sonar-l10n-zh.git
   cd sonar-l10n-zh/
   mvn clean package -Dmaven.test.skip=true
   scp target/sonar-l10n-zh-plugin-8.4-SNAPSHOT.jar 10.0.0.5:/usr/local/sonarqube/extensions/plugins

2.允许匿名访问Force user authentication

6.jenkins服务器安装扫描器

cd /usr/local/src
wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scannercli-4.3.0.2102-linux.zip
unzip sonar-scanner-cli-4.3.0.2102-linux.zip
ln -s /usr/local/src/sonar-scanner-4.3.0.2102-linux/ /usr/local/sonar-scanner

vim /usr/local/sonar-scanner/conf/sonarscanner.properties #指向sonarqube服务器的地址
sonar.host.url=http://10.0.0.5:9000
sonar.sourceEncoding=UTF-8

1.准备测试代码,(默认生成的配置文件)
cat sonar-project.properties
sonar.projectKey=org.sonarqube:php-simple-sq-scanner                     #自定义项目唯一标识key
sonar.projectName=PHP :: Simple Project :: SonarQube Scanner     #项目名称,会显示在sonarqube的web
sonar.projectVersion=1.0 #项目版本
sonar.sources=src #源代码目录
sonar.language=php #代码语言类型
sonar.sourceEncoding=UTF-8 #编码格式

cat sonar-project.properties                            
sonar.projectKey=sprint-boot-helloworld        #项目的唯一标识
sonar.projectName=sprint-boot-helloworld    #项目的名称,用于显示在 sonarqube web 界面
sonar.projectVersion=1.0                                    #项目版本
sonar.sources=.                                                        #项目源码所在目录
sonar.java.binaries=.                                            #项目源码编译生成的二进制文件路径
sonar.language=java                                                #编程语言
sonar.sourceEncoding=UTF-8                                #编码格式

2.在源代码目录执行扫描/usr/local/sonar-scanner/bin/sonar-scanner

3.sonarquebe web界面验证扫描结果

7.部署项目

1.jenkins安装sonarqube scanner插件
2.配置sonarqube server以及验证信息
3.配置指定sonarqube scanner的路径
4.创建任务时添加扫描步骤,将代码发送给sonarqube server(先扫描再部署)
5.在sonarqube server创建jenkins的网络回调接口,汇报代码检测报告
6.准备项目的Jenkinsfile文件(使质量通过则构建)

问题报错

👉git push解决办法

👉远程来源已经存在解决办法
[root@git-gitee ~/test]$ git remote add origin git@gitlab.zyy.com:test/luck-draw.git
fatal: remote origin already exists. 

git remote -v
git remote show origin #查看远程仓库详细信息
git remote rm origin
git remote remove origin #删除关联
git remote add origin git@gitlab.example.com:testgroup/testproject.git #建立远程仓
git push -u origin master #将本地master分支推送到远程仓库master



👉git push解决办法
① 报错:! [remote rejected] master -> master (pre-receive hook declined) 
解决:
将所要push的内容所在的分支的protected权限关闭:进入所在项目的settings --> 点击进入Protected branches,点击unprotected将master分支的权限改变,即关闭master的protected权限

👉报错:! [rejected] master -> master (fetch first) error: failed to push some refs to ' 。。。'
解决:
出现这个问题是因为github中的README.md文件不在本地代码目录中,可以通过如下命令进行代码合并
git pull --rebase origin master

👉报错:Hook executed successfully but returned HTTP 403

👉报错:Hook executed successfully but returned HTTP 403

①进入jenkins:Manage Jenkins- >Configure Global Security -> 授权策略 -> Logged-in users can do anything (登录用户可以做任何事情) 点选 -> 匿名用户具有可读权限 点选

②去掉跨站点请求伪造,点选放开。Manage Jenkins- >Configure Global Security->CSRF Protection(跨站请求伪造保护)
 高版本jenkins不能界面禁用跨站请求伪造保护
    vim /etc/sysconfig/jenkins
    JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true -Dhudson.security.csrf.GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION=true"

③去掉Gitlab enable authentication 点选放开。系统管理 -> 系统设置 -> Enable authentication for '/project' end-point

👉安装插件时报错:java.security.cert.CertificateNotYetValidExcepti

👉报错:There were errors checking the update sites: SSLHandshakeException: PKIX path validation failed: java.security.cert.CertPathValidatorEx

解决办法:修改Advanced里面Update Site的URL(https-->http)
http://mirror.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

👉安装插件时报错:java.security.cert.CertificateNotYetValidException: NotBefore: Wed Dec 15 10
问题原因:证书不在有效期,linux系统时间不对。将linux系统时间修改成当前时间
yum -y install chrony;systemctl enable --now chronyd

👉java -jar 解包时

①👉报错:The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]

原因:Tomcat建议使用apache的apr,来更好的运行Tomcat;
yum install tomcat-native
yum instal apr-devel

👉报错:Shutting down ExecutorService 'applicationTaskExecutor'

原因:项目目启动后,在resource目录下没有加载到配置信息。配置文件位于 src/main/java 目录下,而项目默认的加载配置文件的目录是 src/main/resource,因而找不到配置文件
解决方法:在pom.xml中,将src/main/java添加为配置文件目录,如下
<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.xml</include>
        </includes>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
    </resource>
</resources>

image.png

👉This application has no explicit mapping for /error, so you are seeing this as a fallback.