docker-compose 快速部署持續集成測試環境 Gitlab+Harbor+Jenkins pipeline 實現 tag run docker Images

環境

測試部署主機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安裝方式  可用 ansible-roles 快速部署全部主機   https://github.com/gitDream/ansible-roles_jenkins_harbor_gitlab.git

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

安裝Gitlab

  • 參考這篇文章:

    http://blog.51cto.com/bigboss/2129284css

  • Docker方式安裝:

    https://github.com/JyBigBoss/docker-compose/blob/master/gitlab/docker-compose.yamlhtml

  • 安裝Harbor

  • 參考:

  • http://blog.51cto.com/bigboss/2316525         
  • docker 配https registry私有倉庫的重點說明

  •  

    生成ca 證書  給harbor 使用
  • openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ./server.key -out ./server.crt -subj "/CN=registry.lotbrick.com"
    

    copydocker 客戶端登入harbor 前導入自制Ca 證書位置         mkdir -p /etc/docker/certs.d/registry.lotbrick.com        #registry.lotbrick.com  建立域名目錄          node

    mkdir -p /etc/docker/certs.d/registry.lotbrick.com
    scp ./server.crt  /etc/docker/certs.d/registry.lotbrick.com/ca.crt
  • systemctl daemon-reload
    systemctl restart docker
    docker login
    registry.lotbrick.com
     
      

     

                                                                                                                                                     
  • 安裝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/
      cat << EOF > index.html
      <h1>Test 123</h1>
      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
      

        

      Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
      Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
      Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器


      在Harbor上建立一個test倉庫

      Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器


      配置Jenkins

        • 打開Jenkins的設置頁面,配置Publish over SSH插件

          Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器

        • 建立一個流水線(pipeline)項目

          Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
          Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
          Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
          Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
          Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
          Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
          Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
          Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
          Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
          Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
          Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
          Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器

      

      

    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 '''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 '''cat << EOF > docker-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: [sshPublisherDesc(configName: 'jenkins_pipeline_test_deploy', 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)])
    
    }
    }
      • 生成流水線腳本的方法
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器python

      • Jenkins憑據ID獲取方法
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器linux

      • 發佈腳本: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
        

          

        測試發佈

        • 第一次發佈

        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器

        • 再來一次

        #感受沒玩夠,再傳個代碼測試一回 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 

        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器
        Gitlab+Harbor+Jenkins pipeline實現利用tag部署docker容器

相關文章
相關標籤/搜索