Gitlab+Harbor+Jenkins pipeline实现利用tag部署docker容器

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 << EOF > /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

2.安装Gitlab
参考这篇文章:
https://blog.51cto.com/bigboss/2129284

Docker方式安装:
https://github.com/JyBigBoss/docker-compose/blob/master/gitlab/docker-compose.yaml

3.安装Harbor
参考:
https://blog.51cto.com/bigboss/2316525

4.安装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

pipline脚本如下(jenkins部署):

node {
  stage(' Git clone ') {
      git branch: 'test', credentialsId: '7b78ace6-2631-4c9d-8779-8a6b3db3f63b', url: 'git@139.159.146.8:root/demo.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 label: '', script: '''cat << EOF > Dockerfile
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 label: '', script: '''cat << EOF > docker-compose.yaml
version: "2.4"
services:
  http:
    image: 139.9.73.126/demo/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('http://139.9.73.126', 'da6c05c9-2401-4beb-8d71-9440d1006cfd'){
        //build当前目录(workspace)下的Dockerfile
        def BuildImage = docker.build("139.9.73.126/demo/http:${check_to_tag}")
        //Push刚才Build出来的镜像
        BuildImage.push()
      }
}
stage('Depoly'){
    //调用Publish Over SSH插件,上传docker-compose.yaml文件并且执行deploy脚本
      sshPublisher(publishers: [sshPublisherDesc(configName: '测试机1(48)', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '/bin/bash /root/deploy/deploy.sh', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '/root/deploy', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'docker-compose.yaml')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
      sshPublisher(publishers: [sshPublisherDesc(configName: '测试机2(53)', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '/bin/bash /root/deploy/deploy.sh', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '/root/deploy', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'docker-compose.yaml')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])

}
}    

deploy.sh(实际跑业务的服务器)

#!/bin/bash
  echo '正在更新版本......'

  cd /root/deploy

  IMAGE_NAME='139.9.73.126/demo/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 $IMAGE_NAME:$i
      done
  else
     echo "更新版本失败!!!"
     exit 111
  fi

jenkins配置如下:
在这里插入图片描述
在这里插入图片描述
流水线脚本编写可以通过以下方式:
1.套用模板
node {
stage(’ 克隆代码’) {
xxxxxxxxxx
}
stage(“构建镜像”){
xxxxxxxxxx
}
stage("生成docker-compose "){
xxxxxxxxxxx
}

stage(‘构建镜像打标签并推送harbor仓库’) {
xxxxxxxxxx
}
stage(‘服务器部署’){
xxxxxxxxxx
}
2.使用流水线语法帮助
具体可去jenkins官网查看:

https://www.jenkins.io/zh/doc/book/pipeline/

在这里插入图片描述
在这里插入图片描述
测试
在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值