软件开发最初是由两个团队共同组成:(没有采用DevOps之前)
没有采用DevOps的缺点:
采用DevOps的优点?
整体的软件开发流程:
为了保证整体流程可以高效的完成,各个阶段都有比较常见的工具,如下图:
注意:由于新版的SonarQube不再支持MySQL,而是支持PostgreSQL。那么安装SonarQube时需要先安装PostgreSQL。
docker pull postgres
docker pull sonarqube:8.9.9-community
mkdir -p /usr/local/docker/sonarqube_docker
cd /usr/local/docker/sonarqube_docker
vi docker-compose.yml
内容如下:
version: "3.1"
services:
db:
image: postgres
container_name: db
ports:
- 5432:5432
networks:
- sonarnet
environment:
POSTGRES_USER: sonar
POSTGRES_PASSWORD: sonar
sonarqube:
image: sonarqube:8.9.9-community
container_name: sonarqube
depends_on:
- db
ports:
- "9000:9000"
networks:
- sonarnet
environment:
SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonar
SONAR_JDBC_USERNAME: sonar
SONAR_JDBC_PASSWORD: sonar
networks:
sonarnet:
driver: bridge
vi /etc/sysctl.conf
在该配置文件中,添加下面的内容:(直接复制下面的内容到配置文件中,保存退出即可!)
vm.max_map_count=262144
[root@centos7-jenkins sonarqube_docker]# sysctl -p
vm.max_map_count = 262144
docker-compose up -d
访问地址:sonarqube的ip+9000端口。
SonarQube的账号和密码都是admin !
sonar-scanner官网下载地址
[root@centos7-jenkins ~]# ls
apache-maven-3.8.6-bin.tar.gz jenkins anaconda-ks.cfg jdk-8u333-linux-x64.tar.gz sonar-scanner-cli-4.6.2.2472-linux.zip
yum -y install unzip
unzip sonar-scanner-cli-4.6.2.2472-linux.zip
mv sonar-scanner-4.6.2.2472-linux sonar-scanner
[root@centos7-jenkins ~]# cd /usr/local/docker/jenkins_docker/data
[root@centos7-jenkins data]# mv /root/sonar-scanner ./
[root@centos7-jenkins data]# vi sonar-scanner/conf/sonar-scanner.properties
我们的配置文件内容如下:(记得下面的ip 192.168.184.80要换成你SonarQube的IP。)
#Configure here general information about the environment, such as SonarQube server connection details for example
#No information about specific project should appear here
#----- Default SonarQube server
sonar.host.url=http://192.168.184.80:9000
#----- Default source code encoding
sonar.sourceEncoding=UTF-8
[root@centos7-jenkins ~]# cd /usr/local/docker/jenkins_docker/data/sonar-scanner/bin/
可以看到下面的sonar-scanner就是我们需要的代码质量检测工具:
[root@centos7-jenkins bin]# ls
sonar-scanner sonar-scanner-debug
[root@centos7-jenkins ~]# cd /usr/local/docker/jenkins_docker/data/workspace/mytest
[root@centos7-jenkins mytest]# pwd
/usr/local/docker/jenkins_docker/data/workspace/mytest
[root@centos7-jenkins mytest]# /usr/local/docker/jenkins_docker/data/sonar-scanner/bin/sonar-scanner -Dsonar.source=./ -Dsonar.projectname=linux-mytest -Dsonar.login=fa58b2f8903333918bfa22cf0bc07f3ac0e92435 -Dsonar.projectKey=linux-mytest -Dsonar.java.binaries=./target/
配置提示:
sonar.projectname=${JOB_NAME}
sonar.projectKey=${JOB_NAME}
sonar.source=./
sonar.java.binaries=target
1:由于我们刚刚案例执行过了一次,所以要在Jenkins宿主机上先删除一个目录:
[root@centos7-jenkins ~]# rm -rf /usr/local/docker/jenkins_docker/data/workspace/mytest/.scannerwork/
Harbor v2.4.2下载链接
[root@centos7-jenkins ~]# ls | grep harbor
harbor-offline-installer-v2.4.2.tgz
tar -zxvf harbor-offline-installer-v2.4.2.tgz -C /usr/local
cd /usr/local/harbor/
cp harbor.yml.tmpl harbor.yml
vim harbor.yml
[root@centos7-jenkins harbor]# pwd
/usr/local/harbor
开始执行:(然后会加载一大堆的东西,需要等一下)
[root@centos7-jenkins harbor]# ./install.sh
vi /etc/docker/daemon.json
内容如下:
{
"registry-mirrors": [
"https://u01jo9qv.mirror.aliyuncs.com",
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com"
],
"insecure-registries": ["192.168.184.80:80"]
}
systemctl restart docker
[root@centos7-jenkins harbor]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytest v1.0.0 fc62ecfe296d 10 hours ago 661MB
docker tag mytest:v1.0.0 192.168.184.80:80/repo/mytest:v1.0.0
[root@centos7-jenkins harbor]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytest v1.0.0 fc62ecfe296d 11 hours ago 661MB
192.168.184.80:80/repo/mytest v1.0.0 fc62ecfe296d 11 hours ago 661MB
docker login -u admin -p Harbor12345 192.168.184.80:80
[root@centos7-jenkins harbor]# docker push 192.168.184.80:80/repo/mytest:v1.0.0
The push refers to repository [192.168.184.80:80/repo/mytest]
5f70bf18a086: Pushed
34589845b783: Pushed
35c20f26d188: Pushed
c3fe59dd9556: Pushed
6ed1a81ba5b6: Pushed
a3483ce177ce: Pushed
ce6c8756685b: Pushed
30339f20ced0: Pushed
0eb22bfb707d: Pushed
a2ae92ffcd29: Pushed
v1.0.0: digest: sha256:b9504aee3cc90755bb237d9f91795b75f816558330657ffae4fd3ff92fdaee7d size: 2418
[root@centos7-jenkins harbor]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.184.80:80/repo/mytest v1.0.0 fc62ecfe296d 25 hours ago 661MB
mytest v1.0.0 fc62ecfe296d 25 hours ago 661MB
[root@centos7-jenkins harbor]# docker rmi -f fc62ecfe296d
Untagged: 192.168.184.80:80/repo/mytest:v1.0.0
Untagged: 192.168.184.80:80/repo/mytest@sha256:b9504aee3cc90755bb237d9f91795b75f816558330657ffae4fd3ff92fdaee7d
Untagged: mytest:v1.0.0
Deleted: sha256:fc62ecfe296d996c03ac5ef9a0c073f5772aacf382e7f8215d18052d18707982
[root@centos7-jenkins harbor]# docker pull 192.168.184.80:80/repo/mytest:v1.0.0
v1.0.0: Pulling from repo/mytest
7448db3b31eb: Already exists
c36604fa7939: Already exists
29e8ef0e3340: Already exists
a0c934d2565d: Already exists
a360a17c9cab: Already exists
cfcc996af805: Already exists
2cf014724202: Already exists
4bc402a00dfe: Already exists
bce9a3522658: Already exists
4f4fb700ef54: Already exists
Digest: sha256:b9504aee3cc90755bb237d9f91795b75f816558330657ffae4fd3ff92fdaee7d
Status: Downloaded newer image for 192.168.184.80:80/repo/mytest:v1.0.0
192.168.184.80:80/repo/mytest:v1.0.0
默认情况下,Jenkins容器内部是使用不了Docker的,我们这次就是为了解决这种情况。
解决方法有两个:(我们使用的是第二种)
第一种:在Jenkins容器内部安装一个Docker。(缺点是会占用很多的资源,浪费容器空间,不推荐使用)。
第二种:Jenkins容器内部使用宿主机的Docker,也就是说Jenkins容器和宿主机共同使用同一个Docker。(推荐!)
[root@centos7-jenkins harbor]# cd /var/run/
[root@centos7-jenkins run]# chown root:root docker.sock
[root@centos7-jenkins run]# chmod o+rw docker.sock
[root@centos7-jenkins ~]# cd /root/jenkins/
[root@centos7-jenkins jenkins]# docker-compose down
[root@centos7-jenkins jenkins]# rm -rf docker-compose.yml
[root@centos7-jenkins jenkins]# vi docker-compose.yml
新文件内容如下:
version: "3.1"
services:
jenkins:
image: jenkins/jenkins:2.359
container_name: jenkins
ports:
- 9200:8080
- 50000:50000
volumes:
- /usr/local/docker/jenkins_docker/data/:/var/jenkins_home/
- /var/run/docker.sock:/var/run/docker.sock
- /usr/bin/docker:/usr/bin/docker
- /etc/docker/daemon.json:/etc/docker/daemon.json
docker-compose up -d
[root@centos7-jenkins jenkins]# docker exec -it jenkins bash
[root@centos7-jenkins jenkins容器]# docker version
2:配置Jenkins:
mv target/*.jar docker/
docker build -t mytest:$tag docker/
docker login -u admin -p Harbor12345 192.168.184.80:80
docker tag mytest:$tag 192.168.184.80:80/repo/mytest:$tag
docker push 192.168.184.80:80/repo/mytest:$tag
vi /root/deploy.sh
内容如下:
harbor_addr=$1
harbor_repo=$2
project=$3
version=$4
host_port=$5
container_port=$6
imageName=$harbor_addr/$harbor_repo/$project:$version
containerId=`docker ps -a | grep ${project} | awk '{print $1}'`
if [ "$containerId" != "" ] ; then
docker stop $containerId
docker rm $containerId
fi
tag=`docker images | grep ${project} | awk '{print $2}'`
if [[ "$tag" =~ "$version" ]] ; then
docker rmi -f $imageName
fi
docker login -u admin -p Harbor12345 $harbor_addr
docker pull $imageName
docker run -d -p $host_port:$container_port --name $project $imageName
echo "SUCCESS"
chmod a+x /root/deploy.sh
cd /root
[root@centos7-jenkins ~]# ./deploy.sh 192.168.184.80:80 repo mytest v3.0.0 8082 8082
3fd32cbb9557
3fd32cbb9557
Untagged: 192.168.184.80:80/repo/mytest:v3.0.0
Untagged: 192.168.184.80:80/repo/mytest@sha256:bd9556efbf06af2fba04b482e318111266a725cc2b10edf215e1190874df06b3
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
v3.0.0: Pulling from repo/mytest
Digest: sha256:bd9556efbf06af2fba04b482e318111266a725cc2b10edf215e1190874df06b3
Status: Downloaded newer image for 192.168.184.80:80/repo/mytest:v3.0.0
192.168.184.80:80/repo/mytest:v3.0.0
2f73780159ee473a62e0d9e43874c8abcc7e173ac23ee0adfe162c442187e0f4
SUCCESS
[root@centos7-jenkins ~]# docker ps | grep mytest
3fd32cbb9557 192.168.184.80:80/repo/mytest:v3.0.0 "/bin/sh -c 'java -j…" 38 seconds ago Up 37 seconds 0.0.0.0:8082->8082/tcp, :::8082->8082/tcp mytest
[root@centos7-jenkins ~]# mv /root/deploy.sh /usr/bin/
8:配置Jenkins构建后操作:
提示:
deploy.sh 192.168.184.80:80 repo ${JOB_NAME} $tag $host_port $container_port