容器版单个jenkins实现CI/CD----带solo博客开源项目
实验架构: 192.168.0.96 gitlab 192.168.0.97 jenkins、docker-1.7 192.168.0.98 harbor、docker-1.7集群
jenkins安装、配置请参照:https://www.cnblogs.com/effortsing/p/10480972.html
gitlab安装请参照:https://www.cnblogs.com/effortsing/p/10012158.html
harbor安装请参照:https://www.cnblogs.com/effortsing/p/10061096.html
docker安装请参照:https://www.cnblogs.com/effortsing/p/10013567.html
1、下载solo源码包、修改代码访问地址、推送到gitlab代码仓库中
找一台主机上安装git
yum install -y git
配置ssh密钥
ssh-keygen -t rsa -f /root/.ssh/id_rsa -P ""
添加ssh-key公钥到gitlab, 哪台需要连接gitlab服务器,哪台就需要把他的公钥添加到gitlab服务器上
cat ~/.ssh/id_rsa.pub
[root@test3 ~]# cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCxiS/XYe2x+iwhU6PuiV8XTmNgQ9w3FMgC4JuPkyHwIhHxh+1M/Evj7AqGJIURcrl1CHqJKng8d/M8WT+NoqwlA524hKpjv4RgEW2dl1kLfQLVVJmoB9NOvr5+cdmQ1V8xuhhxtcLw7JhigXu7HNCEs6bJ+MVwD83oc9jV7HVB3mgmZrk2+Ntxz8cr/W9MoLmkqEQJ3JYmsXmJsofcMPOQJNpmIScAu7kWJ4tIJAN5SuhNjQTw+v5HgLJT/LTdf/0DUCP55ulsDWP03ilIsEMT1FX1mz2tkQsopim2Z/Tqtk96OTNYB5svNb+nJXkRUskbQ+pYjU3hr0kxkAr/NEzX root@test3
登录gitlab,在右上角设置中找到SSH密钥
将刚才生成的公钥内容复制到密钥中,标题名字随意
创建项目
创建一个hello项目
点击小扳手(管理区域),新建项目
Project name为项目的名称
Import project from从其他代码仓库导入代码
Project description项目说明
Visibility Level项目等级
private只有你自己跟你指定的人能看
internal只有拥有gitlab账号的用户可以查看与拉取
public该项目能被所有人访问到并clone
从gitlab远程主机克隆一个版本库
mkdir /root/test/
cd /root/test/
git clone git@192.168.0.94:root/hello.git
这里有个警告,因为刚才创建的版本库是空的,所以这里提醒,克隆了一个空库
创建一个文件
cd /root/test/hello/
touch read.txt
echo "Hello world" >> read.txt
1.2、下载solo源码包
solo源码下载链接:https://pan.baidu.com/s/15sRvAmG9dfB3fqprxhzFEw 提取码:mygh
上传solo代码
rz solo_A5.zip
unzip solo_A5.zip
rm -rf solo_A5.zip
mv /root/test/hello/solo/* /root/test/hello/
查看solo项目所有文件
[root@bogon hello]# ls
CHANGE_LOGS.html CONTRIBUTING.md Dockerfile LICENSE package-lock.json PULL_REQUEST_TEMPLATE.md scripts src
CODE_OF_CONDUCT.md docker-compose.yml gulpfile.js package.json pom.xml README.md solo 说明.htm
1.3、修改solo项目里面的ip地址
这里需要修改项目地址为部署到的那一台地址,否则最后访问项目是报错的,
这里把192.168.0.98既作为harbor仓库又作为docker服务部署地址,所以填写192.168.0.98
vi /root/test/hello/src/main/resources/latke.properties
serverHost=192.168.0.98
将项目添加到仓库
git add .
配置用户名和邮箱,tell me who you are
git config --global user.email "15613691030@163.com"
git config --global user.name "zhuzihao"
提交文件到仓库
git commit -m "2nd Commit" #2nd Commit是本次提交的说明
创建tag版本
git tag 1.0.2
查看git版本号
git tag
最后推送到gitlab上
git push origin master
在gitlab上看到,已经推送成功了
推送代码参照: https://www.cnblogs.com/effortsing/p/10482792.html
2.1、配置jenkins主机免密登录harbor仓库、docker集群
参照:https://www.cnblogs.com/effortsing/p/10060748.html
2.2、配置jenkins主机上的docker可以登录harbor仓库并且上传镜像到harbor仓库
参照:https://www.cnblogs.com/effortsing/p/10061096.html
2.3、配置jenkins主机上的git秘钥到gitlab上实现拉取代码功能, 参照:https://www.cnblogs.com/effortsing/p/10482792.html
3、安装ssh agent插件,添加凭证,用于远程登录docker集群进行部署
jenkins主机上的公钥放在docker集群,私钥拿出来,写入到jenkins凭据,截图如下:
![](https://img2018.cnblogs.com/blog/1413787/201903/1413787-20190304181256888-327019912.png)
![](https://img2018.cnblogs.com/blog/1413787/201903/1413787-20190304181303853-317045692.png)
![](https://img2018.cnblogs.com/blog/1413787/201903/1413787-20190304181310642-1401742434.png)
![](https://img2018.cnblogs.com/blog/1413787/201903/1413787-20190304181317123-1447050613.png)
![](https://img2018.cnblogs.com/blog/1413787/201903/1413787-20190304181322660-587134994.png)
4、生成pipeline可以用的git链接地址。写到pipline中,这样在pipline脚本才可以从gitlab上拉取代码如下图所示:
添加登录gitlab服务器的账号、密码
添加gitllab仓库的http协议地址
![](https://img2018.cnblogs.com/blog/1413787/201903/1413787-20190304180151919-898615607.png)
![](https://img2018.cnblogs.com/blog/1413787/201903/1413787-20190304180238636-1361764988.png)
![](https://img2018.cnblogs.com/blog/1413787/201903/1413787-20190304180248078-339072626.png)
必须是生成下面格式:
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'c32a3a24-2c9d-4c8d-b456-9d6d0badf3a2', url: 'http://192.168.0.96:8081/zihao/test.git']]])
上面生成的代码填写到下面的pipline脚本中
说明:下面pipline代码编译镜像那一步的代码必须靠左,目的是不要有空格,否则报错,deploy那一步也必须是那种格式,否则报错
5、编写pipline脚本如下:
注意里面的JAVA_HOME要正确 pipeline { agent any stages { stage('git checkout'){ steps { echo 'git clone' checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'c32a3a24-2c9d-4c8d-b456-9d6d0badf3a2', url: 'http://192.168.0.96:8081/zihao/test.git']]]) } } stage('maven build'){ steps { echo 'maven build' sh ''' export JAVA_HOME=/docker-java-home /usr/share/maven/bin/mvn clean package -Dmaven.test.skip=true ''' } } stage('docker build and push images'){ steps { echo 'docker build and push images' sh ''' REPOSITORY=192.168.0.98:5000/library/solo/solo:${Tag} cat >Dockerfile<<EOF FROM 192.168.0.98:5000/library/tomcat-85:latest RUN rm -rf /usr/local/tomcat/webapps/ROOT/ COPY target/*.war /usr/local/tomcat/webapps/ROOT.war WORKDIR /usr/local/tomcat EXPOSE 8080 CMD ["./bin/catalina.sh", "run"] EOF docker build -t $REPOSITORY . docker login -u admin -p Harbor12345 192.168.0.98:5000 docker push $REPOSITORY ''' } } stage('deploy') { steps { sshagent (credentials: ['docker']){ sh ''' REPOSITORY=192.168.0.98:5000/library/solo/solo:${Tag} ssh -o StrictHostKeyChecking=no -l root 192.168.0.98 docker container run -d --name blog-solo-2 -v /opt:/opt -p 89:8080 $REPOSITORY ''' } } } } }
6、启用Tag标签,如下图所示:
点击参数化构建过程=》选择字符参数
7、输入版本号,开始构建,如图所示:
8、在192.168.0.98上查看部署好的容器项目
docler ps -l
9、访问项目
http://192.168.0.98:89
说明:远程执行命令前提需要安装ssh agent插件, 现在ssh agent的认证,已不支持明文用户密码,而只能用加密方式实现。 所以先在jenknis和docker集群之间实现ssh免密码rsa证书登陆。 jenkins主机上的公钥放在docker集群,私钥拿出来,写入到jenkins凭据 参照:https://blog.csdn.net/weixin_34067980/article/details/86444168
参照视频:链接:https://pan.baidu.com/s/10qtI5BlNFM2A6bMkR7FEEA 提取码:0pwt
脚本参照: https://blog.csdn.net/jackyzhousales/article/details/81713004 https://blog.csdn.net/a1010256340/article/details/83989059 生成pipeline可以用的git链接地址参照: https://www.cnblogs.com/FRESHMANS/p/8184874.html