部署数据库 postgresql
编写 postgres-compose.yaml 文件
version: "3"
services:
db:
image: postgres:10.21
hostname: '192.168.2.26'
container_name: 'postgres-sonarqube'
environment:
TZ: 'Asia/Shanghai'
POSTGRES_USER: sonar
POSTGRES_PASSWORD: sonar
volumes:
- /data/data/postgres-sonarqube-com/psqls:/var/lib/postgresql
- /data/data/postgres-sonarqube-com/data:/var/lib/postgresql/data
ports:
- "6632:5432"
privileged: true
networks:
front:
driver: bridge
back:
driver: bridge
driver_opts:
foo: "1"
bar: "2"
执行部署
docker-compose -f postgres-compose.yaml up -d
部署 SonarQube
编写 sonarqube-composes.yaml
version: "3"
services:
sonarqube:
image: sonarqube:8.9.9-community
hostname: '192.168.2.26'
container_name: 'sonarqube-community'
environment:
TZ: 'Asia/Shanghai'
SONAR_JDBC_URL: jdbc:postgresql://192.168.2.26:6632/sonar
SONAR_JDBC_USERNAME: sonar
SONAR_JDBC_PASSWORD: sonar
volumes:
- /data/data/sonarqube-community/sonarqube_data:/opt/sonarqube/data
- /data/data/sonarqube-community/sonarqube_extensions:/opt/sonarqube/extensions
- /data/data/sonarqube-community/sonarqube_logs:/opt/sonarqube/logs
ports:
- "9009:9000"
privileged: true
networks:
front:
driver: bridge
back:
driver: bridge
driver_opts:
foo: "1"
bar: "2"
执行部署
docker-compose -f sonarqube-composes.yaml up -d
查看状态
docker ps -a | grep "sonarqube-community\|postgres-sonarqube"
[gestep@ldap1 sonarqube-community]$ docker ps -a | grep "sonarqube-community\|postgres-sonarqube"
74fe2e1762b1 sonarqube:8.9.9-community "bin/run.sh bin/sona…" 19 hours ago Up 19 hours 0.0.0.0:9009->9000/tcp sonarqube-community
ea19e47a28bb postgres:10.21 "docker-entrypoint.s…" 21 hours ago Up 21 hours 0.0.0.0:6632->5432/tcp postgres-sonarqube
安装插件
以下插件在页面无法安装
# 插件下载地址
https://github.com/mc1arke/sonarqube-community-branch-plugin/releases
# 看好对应版本,我安装的是8.9.9素以选择插件版本是 1.8.1
sonarqube-community-branch-plugin-1.8.1.jar
9.1+ 1.11.0
9.0 1.9.0
8.9 1.8.1
8.7-8.8 1.7.0
8.5-8.6 1.6.0
8.2-8.4 1.5.0
8.1 1.4.0
7.8-8.0 1.3.2
7.4-7.7 1.0.2
===========================================================================
https://github.com/gabrie-allaigre/sonar-gitlab-plugin/releases
# 下载以下版本,最新版即可
sonar-gitlab-plugin-4.1.0-SNAPSHOT.jar
# 下载好以上插件之后,放到镜像映射出来的目录下:
即: /data/data/sonarqube-community/sonarqube_extensions/plugins 目录下
# 重启 sonarqube 使插件生效
# 停止
docker-compose -f sonarqube-composes.yaml down
# 启动
docker-compose -f sonarqube-composes.yaml up -d
部署Gitlab-14.0.12
编写 gitlab-composes.yaml 文件
# 在当前目录先创建 .env 文件
[gestep@ldap1 gitlab_composes]$ vim .env
CONF=/data/data/composes-gitlab/conf/:/etc/gitlab/
LOG=/data/data/composes-gitlab/logs/:/var/log/gitlab/
DATA=/data/data/composes-gitlab/data/:/var/opt/gitlab
LOCALE_TIME=/usr/share/zoneinfo/Asia/Shanghai:/etc/localtime
version: '3'
services:
web:
image: 'gitlab/gitlab-ce:14.0.12-ce.0'
restart: always
hostname: '192.168.2.21'
container_name: 'gitlab'
environment:
TZ: 'Asia/Shanghai'
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://192.168.2.22/gitlab/'
gitlab_rails['gitlab_shell_ssh_port'] = 22
nginx['listen_port'] = 80
ports:
- '8082:80'
- '6443:443'
- '62227:22'
- '994:994'
volumes:
- ${CONF}
- ${LOG}
- ${DATA}
- ${LOCALE_TIME}
privileged: true
部署运行
docker-compose -f gitlab-composes.yaml up -d
配置
cd /data/data/composes-gitlab/conf/
vim gitlab.rb
# 修改一下项
............
# external_url 'GENERATED_EXTERNAL_URL'
external_url 'http://192.168.2.22/gitlab/'
............
# 保存重启
docker-compose -f gitlab-composes.yaml down
docker-compose -f gitlab-composes.yaml up -d
配置对接gitlab
创建 SonarQube 账号 用于代码检测
进入项目设置,权限管理,将刚创建的账号添加进项目,给与开发者权限
登录SonarQube账号并创建GitLab OAuth app
登录成功之后,点击右上角头像,选择 Edit profile
填写信息,提交即可
注意这个红框里边的信息,提前复制出来保存,因为只显示一次
配置访问(密钥) Token
!!!!!!!!!!!!! 存好这个令牌,它只显示一次 !!!!!!!!!!!!!!!!!!!!!!!
SonarQube 配置
管理员账号登录
Gitlab User Token后边设置
访问Sonar及配置
# 访问
http://192.168.2.26:9009/
初始账号/密码是:admin / admin
登录之后修改自己的密码即可
查看项目页面,不出意外项目已经显示了
配置CICD .gitlab-ci.yml
项目根目录创建.gitlab-ci.yml
sonarqube-check:
image: maven:3.6.3-jdk-8-alpine
variables:
SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar" # Defines the location of the analysis task cache
GIT_DEPTH: "0" # Tells git to fetch all the branches of the project, required by the analysis task
cache:
key: "${CI_JOB_NAME}"
paths:
- .sonar/cache
script:
- mvn sonar:sonar -Dsonar.projectKey=wangjinping_yw-test_AYGKuCJCTzJcoUHVW75A -Dsonar.host.url=http://192.168.2.206:9009 -Dsonar.login=test
allow_failure: true
only:
- master # or the name of your main branch