准备工作
准备gitlib 准备sonar需要的数据库PostgreSQL
SonarQube 部署
#启用临时容器 复制出默认配置
docker run --rm -d sonarqube:10.7.0-community
# 复制配置文件
docker cp ba50e4b9d592:/opt/sonarqube/conf/ /home/docker/sonar/conf/
修改/home/docker/sonar/conf/下的配置文件 sonar.properties 调整数据库连接信息
sonar.jdbc.username=sonar
sonar.jdbc.password=123456
sonar.jdbc.url=jdbc:postgresql://pgsql:5432/sonarqubedb?currentSchema=public
#设置官方推荐的值
sudo sysctl -w vm.max_map_count=524288
sudo sysctl -w fs.file-max=131072
sudo ulimit -n 131072
sudo ulimit -u 8192
docker run -d --rm --name sonarqube -p 19000:9000 \
-v /home/docker/sonar/conf:/opt/sonarqube/conf \
-v /home/docker/sonar/data:/opt/sonarqube/data \
-v /home/docker/sonar/logs:/opt/sonarqube/logs \
-v /home/docker/sonar/extensions:/opt/sonarqube/extensions \
--privileged=true --network inet sonarqube:10.7.0-community
# 查看启动日志
docker logs -f sonarqube
登录 ip:19000 默认 账号 admin 密码 admin (测试修改Qwer1234.1234)
集成gitlab+sonar的自动化质量分析
- 为sonar创建gitlab token
- sonar 配置gitlab
进入sonar 配置 进入配置页面 选择gitlab 配置gitlab的url和token 然后再配置具体导入的项目
- 部署/配置gitlibrunner
为自动化准备代码提交的编译 打包 以及sonar的自动分析 需要 maven 和 sonar-scanner
- gitlib-runner
部署docker gitlab-runner 执行器为docker
- gitlib-runner
docker run -d --name gitlab-runner --privileged=true -v /var/run/docker.sock:/var/run/docker.sock -v /home/docker/gitlab-runner/config:/etc/gitlab-runner --network inet gitlab/gitlab-runner:latest
docker exec -it gitlab-runner gitlab-runner register \
--non-interactive \
--url "http://gitlab/"\
--registration-token "glrt-pZPKN7V8FN1auqag6mTR" \
--executor "docker"\
--docker-image maven-runner:1.0.0 \
--description "docker-runner" \
--tag-list "docker,aws" \
--run-untagged="true" \
--locked="false" \
--access-level="not_protected"
可在/etc/gitlab-runner/config.toml配置执行器
concurrent = 1
check_interval = 0
[
session_server
]
session_timeout = 1800
[[
runners
]]
name = "Docker-mavenGitLab-Runner"
url = "http://gitlab"
token = ""
executor = "docker"
[
runners.docker
]
tls_verify = false
image = "maven-runner:1.0.0"
privileged = false
disable_cache = false
volumes = ["/cache"]
shm_size = 0
[
runners.cache
]
Type = "s3"
Path = "s3 - cache - path"
Shared = true
- maven-runner
FROM openjdk:11-jdk-alpine
ENV MAVEN_VERSION =3.9.9
# 设置工作目录
WORKDIR /usr/local
# 下载maven 到 /usr/local 并解压
RUN wget https://dlcdn.apache.org/maven/maven-3/3.9.9/binaries/apache-maven-3.9.9-bin.tar.gz \
&& tar -zxvf /usr/local/apache-maven-3.9.9-bin.tar.gz
# 将本地的Maven安装文件复制到容器中(如果需要自定义Maven版本)
# ADD apache-maven-3.9.9-bin.tar.gz /usr/local
# 设置Maven环境变量(如果是复制安装文件的方式)
ENV MAVEN_HOME /usr/local/apache-maven-3.9.9
# 将Maven添加到PATH环境变量中(如果是复制安装文件的方式)
ENV PATH $PATH:$MAVEN_HOME/bin
CMD ["bash"]
docker image build --no-cache -t maven-runner:1.0.0 .
docker run --rm -d --name maven-runner maven-runner:1.0.0
修改gitlab-ci.yml
stages: # List of stages for jobs, and their order of execution
- mavenbuild
stage: mavenbuild
# Docker in Docker。官方建议显式指定相同版本号,以免版本不一致导致的兼容性问题。
image: docker:20.10.16
services:
- docker:20.10.16-dind
script:
- docker run --name projectname-maven-runner -d --rm maven-runner bash echo "Compiling the code..."
after_script:
# 4. 删除本地镜像
- docker stop projectname-maven-runner
tags:
- projectname-maven-runner
- -sonar-scanner