Gitlab+Harbor+Jenkins pipeline实现利用tag部署docker容器
环境测试部署主机IP:192.168.1.1
Jenkins主机IP:192.168.1.2
Harbor主机IP:192.168.1.3
Gitlab主机IP:192.168.0.10
系统信息:
系统:CentOS 7.5
内核:4.18.7-1.el7.elrepo.x86_64
Docker版本:18.09
docker-compose版本:1.23.1
有道笔记原文,为了防止复制粘贴出来的代码格式有误~~~~
所有主机的Docker安装方式
wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install -y docker-ce
mkdir /etc/docker/
cat/etc/docker/daemon.json
{ "registry-mirrors": ["https://registry.docker-cn.com"],
"live-restore": true,
"default-shm-size": "128M",
"max-concurrent-downloads": 10,
"oom-score-adjust": -1000,
"debug": false
}
EOF
systemctl enable docker
systemctl restart docker
安装Gitlab
[*]
参考这篇文章:
http://blog.运维网.com/bigboss/2129284
[*]
Docker方式安装:
https://github.com/JyBigBoss/docker-compose/blob/master/gitlab/docker-compose.yaml
安装Harbor
[*]
参考:
http://blog.运维网.com/bigboss/2316525
安装Jenkins
yum install -y python-pip
pip install docker-compose
cd $HOME && mkdir jenkins && cd jenkins
wget https://raw.githubusercontent.com/JyBigBoss/docker-compose/master/jenkins/Dockerfile
wget https://raw.githubusercontent.com/JyBigBoss/docker-compose/master/jenkins/docker-compose.yaml
docker-compose up -d
[*]
Jenkins需要安装的插件
Gitlab Hook、Build Authorization Token Root、Publish Over SSH、Gitlab Authentication
Gitlab、Git Parameter、Git Tag Message、Pipeline、docker-build-step、Docker Pipeline
创建git仓库
[*]
在web页面创建一个test仓库,并在在仓库中新建一个index.html文件
cd $HOME
git clone git@192.168.0.10:yfg/test.git
cd test/
catindex.html
Test 123
EOF
git add .
git commit -m 'add index.html'
git push
#创建两个tag
git tag v1 -m 'version:1'
git push --tags
git tag v2 -m 'version:2'
git push --tags
http://i2.运维网.com/images/blog/201811/15/2cb4dd28a9c5be3fb37a97f9ad7f13b9.png
http://i2.运维网.com/images/blog/201811/15/a4aa51cd838e80275e7d6b9efefeabe7.png
http://i2.运维网.com/images/blog/201811/15/357dae11547088e0a23dd984d826634a.png
在Harbor上创建一个test仓库
http://i2.运维网.com/images/blog/201811/15/3e2a99f6613a00541efe8568f89b4da3.png
配置Jenkins
[*]
打开Jenkins的设置页面,配置Publish over SSH插件
http://i2.运维网.com/images/blog/201811/15/de4e039c60c2a0f1fd64623d172681d0.png
[*]
创建一个流水线(pipeline)项目
http://i2.运维网.com/images/blog/201811/15/22c30147e65f921ffc87e04ac5b918dd.png
http://i2.运维网.com/images/blog/201811/15/991e2171ee70cdeba68a5253eb061b86.png
http://i2.运维网.com/images/blog/201811/15/17a52e78f2a17805653b5cb50559e473.png
http://i2.运维网.com/images/blog/201811/15/dedd6d308993e2ea20b2198851b2e405.png
http://i2.运维网.com/images/blog/201811/15/6c504d747e9909ea40fa3327d7e452bb.png
http://i2.运维网.com/images/blog/201811/15/8450466e4f40772ad118e328846be923.png
http://i2.运维网.com/images/blog/201811/15/224fda410556ab3336dd4c58e5445a67.png
http://i2.运维网.com/images/blog/201811/15/25e30e735a1187d2867e37d6501053d2.png
http://i2.运维网.com/images/blog/201811/15/91f638f952b580e0124fd44217ade195.png
http://i2.运维网.com/images/blog/201811/15/9889332b961fdfa07ea9a6a254c75124.png
http://i2.运维网.com/images/blog/201811/15/1171a9dd8593cbf670ceec1c50d6c7db.png
http://i2.运维网.com/images/blog/201811/15/9d940835a6a5b6bde11d70e2612289b2.png
[*]
编写pipeline脚本,下面是这次测试发布用到的脚本
[*]Docker Pipeline插件用法:https://jenkins.io/zh/doc/book/pipeline/docker/
[*]完整的pipeline脚本
node {
stage(' Git clone ') {
git branch: 'master', credentialsId: 'a4a81561-8bc0-426e-89f9-b4d4aa1925d6', url: 'git@192.168.0.10:yfg/test.git'
env.check_to_tag="$TAG"
sh '[ -n "${check_to_tag}" ] &&git checkout ${check_to_tag} ||{ echo -e "切换至指定的tag的版本,tag:${check_to_tag} 不存在或为空,请检查输入的tag!" && exit 111; }'
}
stage("Create Dockerfile"){
sh '''catDockerfile
FROM python:3.7.1-alpine
RUN mkdir /test
WORKDIR /test
COPY ./ /test
EXPOSE 8000
CMD ["python","-m","http.server"]
EOF'''
sh 'cat Dockerfile'
}
stage("Create docker-compose.yaml "){
sh '''catdocker-compose.yaml
version: "2.4"
services:
http:
image: registry.lotbrick.com/test/http:${check_to_tag}
container_name: python-http_server
ports:
- "80:8000"
restart: always
EOF'''
sh 'cat docker-compose.yaml'
}
stage('Build Image And Push to registry') {
//withRegistry('仓库地址','jenkins凭据ID')
docker.withRegistry('https://registry.lotbrick.com', '9ffa7ef5-38c6-49da-8936-ec596359be56'){
//build当前目录(workspace)下的Dockerfile
def BuildImage = docker.build("registry.lotbrick.com/test/http:${check_to_tag}")
//Push刚才Build出来的镜像
BuildImage.push()
}
}
stage('Depoly'){
//调用Publish Over SSH插件,上传docker-compose.yaml文件并且执行deploy脚本
sshPublisher(publishers: +', remoteDirectory: '/root/deploy', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'docker-compose.yaml')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
[*] 生成流水线脚本的方法
http://i2.运维网.com/images/blog/201811/15/4de1f46d1c2610733aef187faaeb6b7f.png
http://i2.运维网.com/images/blog/201811/15/eeb05d757ef259d2626111893a6b56c0.png
http://i2.运维网.com/images/blog/201811/15/8c783ff2440f59b40a19ba1cca5a06c7.png
http://i2.运维网.com/images/blog/201811/15/934fd805f6661253d31022c5eb71d926.png
http://i2.运维网.com/images/blog/201811/15/c6af2d827c57edda5e55ac01432fc751.png
http://i2.运维网.com/images/blog/201811/15/3bf62b0aebe1740960f894babbe9151c.png
[*] Jenkins凭据ID获取方法
http://i2.运维网.com/images/blog/201811/15/5666012ee5f8a652d13cac2c420a27ae.png
http://i2.运维网.com/images/blog/201811/15/1fd6dd608bc091d7237846ff98548b72.png
http://i2.运维网.com/images/blog/201811/15/5d940ec7a465f96e7ef3402db139a281.png
http://i2.运维网.com/images/blog/201811/15/539cc5fb42e487a937a5d8fc744c8015.png
http://i2.运维网.com/images/blog/201811/15/a16154519d60de8aa1f26ae8c1a40c4d.png
[*]发布脚本:depoly.sh:放到要部署代码的主机的/root/deploy目录下
#!/bin/bash
echo '正在更新版本......'
cd /root/deploy
IMAGE_NAME='registry.lotbrick.com/test/http'
DOCKER_TAG=`awk -F ':' '/.*image/{print $NF}' docker-compose.yaml`
echo -e "\n"
docker-compose pull && docker-compose up -d
if ["$?" == 0 ];then
echo '删除旧的image'
OLD_IMAGE=`docker images | grep $IMAGE_NAME | awk '{print $2,$3}' | grep -v "${DOCKER_TAG}" |awk '{print $1}'`
for i in $OLD_IMAGE;do
docker rmi http:$i
done
else
echo "更新版本失败!!!"
exit 111
fi
测试发布
[*]
第一次发布
http://i2.运维网.com/images/blog/201811/15/1d82cfa0ee2bfb5dbd683473549637e2.png
http://i2.运维网.com/images/blog/201811/15/ccf5bd8474c4692a96d10dad184fadb7.png
http://i2.运维网.com/images/blog/201811/15/14ae9bd2bfb0db5f46a8c819b715ec2b.png
http://i2.运维网.com/images/blog/201811/15/754cd0ad73e5b8561bfdecd323b4f160.png
http://i2.运维网.com/images/blog/201811/15/56eff1ecb2db427fc73e7a6502f54fd2.png
http://i2.运维网.com/images/blog/201811/15/728f41f9c5ff2d1bb08d21962da53185.png
http://i2.运维网.com/images/blog/201811/15/4e395cbbb28aa66cd4a303d8b4fdfca2.png
http://i2.运维网.com/images/blog/201811/15/b14a99c9a99e23ba5a4653525d4072a9.png
http://i2.运维网.com/images/blog/201811/15/3335300a72d82a7552097bda96ab9bd0.png
http://i2.运维网.com/images/blog/201811/15/dc6f9cafb7a18eb93c5914d2eac00a8c.png
http://i2.运维网.com/images/blog/201811/15/51ac5be4ce6c409f5798dbd966980af1.png
http://i2.运维网.com/images/blog/201811/15/c72fa7d41e4d1e31e29dedea0b65b1a5.png
http://i2.运维网.com/images/blog/201811/15/9bd52b518bd276ea60f1a1029758984f.png
[*]
再来一次
#感觉没玩够,再传个代码测试一回
cd $HOME
git clone https://github.com/HFIProgramming/mikutap.git
\cp -r mikutap/* test/
cd test
git add .
git commit -m 'add mikutap page'
git tag v3 -m 'add mikutap page'
git push --tags
http://i2.运维网.com/images/blog/201811/15/428e8ae2d588dc91332077e21898a336.png
http://i2.运维网.com/images/blog/201811/15/ee026360c47eaf0719bb2ba8760e91ef.png
http://i2.运维网.com/images/blog/201811/15/143ec512fbfdafbb67e1e0df13c2ed76.png
http://i2.运维网.com/images/blog/201811/15/b2ceed0b1bb5d520c912497ed6a5ad20.png
http://i2.运维网.com/images/blog/201811/15/eb4d61deaee2d27c17f7307ae94d3387.png
https://s1.运维网.com/images/blog/201901/30/6ed9ce7a907af4719c53290d9bccfbf3.png
页:
[1]