The environment

Host IP: 192.168.1.1 Jenkins HOST IP: 192.168.1.2 Harbor host IP: 192.168.1.3 Gitlab host IP: 192.168.0.10 System information: System: CentOS 7.5 Kernel: Docker compose 4.18.7-1.el7.elrepo.x86_64 Docker version: 18.09 Docker-compose version: 1.23.1Copy the code

The Docker installation method can be used to rapidly deploy all hosts using ansible-roles github.com/gitDream/an…

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 dockerCopy the code

Install Gitlab

Refer to this article:

Blog.51cto.com/bigboss/212…

  • Docker installation:

Github.com/JyBigBoss/d…

  • Install the Harbor
  • Blog.51cto.com/bigboss/231…
  • Docker with HTTPS Registry private repository highlights
  • Generate a CA certificate for harbor

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ./server.key -out ./server.crt -subj "/CN=registry.lotbrick.com"

Copy the docker import homemade Ca certificate before the client login harbor position mkdir -p/etc/docker/certs.d/registry.lotbrick.com # registry.lotbrick.com domain directory creation

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
Copy the code

  • Install Jenkins

  • yum install -y python-pippip install docker-composecd $HOME && mkdir jenkins && cd jenkinswget https://raw.githubusercontent.com/JyBigBoss/docker-compose/master/jenkins/Dockerfilewget https://raw.githubusercontent.com/JyBigBoss/docker-compose/master/jenkins/docker-compose.yamldocker-compose up -dCopy the code

    The plug-in Jenkins needs to install

  • Gitlab Hook, Build Authorization Token Root, Publish Over SSH, Gitlab AuthenticationGitlab, Git Parameter, Git Tag Message, Pipeline, Docker-build-step, Docker PipelineCopy the code

    Creating a Git repository

    • Create a test repository on the Web page and create a new index.html file in the repository

  • cd $HOMEgit clone[email protected]: yfg/test. Gitcdtest/cat << EOF > index.html<h1>Test 123</h1>EOFgit add .git commit -m 'add index.html'git pushGit tag v1 -m 'version:1'git push --tagsgit tag v2 -m 'version:2'git push --tagsCopy the code

      





    Create a test repository on Harbor

    Configuration Jenkins

    • Open the Jenkins Settings page and configure the Publish over SSH plugin

    • Create a pipeline project























  •   

      

    node {
      stage(' Git clone ') {
          git branch: 'master', credentialsId: 'a4a81561-8bc0-426e-89f9-b4d4aa1925d6', url: '[email protected]: yfg/test. The git'
          env.check_to_tag="$TAG"
          sh '[-n "${check_to_tag}"] && git checkout ${check_to_tag} | | {echo - e "switch to specify the version of the tag, the tag: ${check_to_tag} does not exist or is empty, please check the input 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-comage. 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('Warehouse address'.'Jenkins credentials ID')
        docker.withRegistry('https://registry.lotbrick.com'.'9ffa7ef5-38c6-49da-8936-ec596359be56'Def BuildImage = docker.build(){//build Dockerfile def BuildImage = docker.build("registry.lotbrick.com/test/http:${check_to_tag}"// Buildimage.push ()}} stage()'Depoly'Yaml file and execute the deploy script sshPublisher(Publishers: [sshPublisherDesc(configName:)){// Publish Over SSH, upload the docker-comemage. yaml file and execute the deploy script 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: '[and] +', remoteDirectory: '/root/deploy', remoteDirectorySDF: false, removePrefix: ' '.sourceFiles: 'docker-compose.yaml')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]}}Copy the code

    • Methods for generating pipeline scripts











    • Method of obtaining Jenkins credential ID









    • Release script: depoly.sh: put it in the /root/deploy directory of the host where the code will be deployed
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      #! /bin/bash
      echo
      'Updating version...... '
      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
      'Delete old 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
      "Update failed!!"

      exit
      111
      fi

      The test release

      • First Release

























      • Once again

      # Feel not enough play, pass a code to test again
      
      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
      Copy the code