自动部署到 Linux Docker 服务器

功能介绍

持续集成可自动部署项目到 Linux Docker 服务器,步骤如下:

  1. 在云计算(比如腾讯云)的网页控制台创建 SSH 密钥对,把私钥录入 CODING 凭据管理,把公钥加载到服务器。也可以使用命令手动创建,把私钥 id_rsa 录入 CODING 凭据管理,把公钥 id_rsa.pub 的内容复制到服务器的 ~/.ssh/authorized_keys,参考文档:《凭据管理》

腾讯云 创建 SSH 密钥对
腾讯云 下载 SSH 私钥
腾讯云 服务器 绑定 SSH 公钥
CODING 凭据管理 录入 SSH 私钥

  1. 获取 Docker 仓库的用户名和密码(CODING 制品库一键创建访问令牌即可获得),录入持续集成的环境变量中;

CODING 制品库 生成访问令牌
CODING 持续集成 环境变量 DOCKER_USER

  1. 在持续集成中使用下述 Jenkinsfile
    1. 构建 Docker 镜像并上传到仓库(CODING 制品库),参考文档:《构建 Docker 类型制品》
    2. 执行 SSH 登录服务器,拉取 Docker 镜像;

Jenkinsfile

pipeline {
  agent any
  stages {
    stage('检出') {
      steps {
        checkout([$class: 'GitSCM', branches: [[name: env.GIT_BUILD_REF]],
        userRemoteConfigs: [[url: env.GIT_REPO_URL, credentialsId: env.CREDENTIALS_ID]]])
      }
    }
    stage('构建') {
      steps {
        echo '构建中...'
        script {
          // 请修改 dockerServer、dockerPath、imageName
          dockerServer = 'codes-farm-docker.pkg.coding.net'
          dockerPath = '/laravel-demo/laravel-docker'
          imageName = "${dockerServer}${dockerPath}/laravel-demo:1.0.0"
          def customImage = docker.build(imageName)

          // 推送 Docker 镜像到仓库
          docker.withRegistry("https://${dockerServer}", CODING_ARTIFACTS_CREDENTIALS_ID) {
            customImage.push()
          }
        }
      }
    }
    stage('部署') {
      steps {
        echo '部署中...'
        script {
          // 声明服务器信息
          def remote = [:]
          remote.name = 'web-server'
          remote.allowAnyHosts = true
          remote.host = '106.54.86.239'
          remote.port = 22
          remote.user = 'ubuntu'

          // 把「CODING 凭据管理」中的「凭据 ID」填入 credentialsId,而 id_rsa 无需修改
          withCredentials([sshUserPrivateKey(credentialsId: "c4af855d-402a-4f38-9c83-f6226ae3441c", keyFileVariable: 'id_rsa')]) {
            remote.identityFile = id_rsa

            // SSH 登录到服务器,拉取 Docker 镜像
            // 请在持续集成的环境变量中配置 DOCKER_USER 和 DOCKER_PASSWORD
            sshCommand remote: remote, sudo: true, command: "apt-get install -y gnupg2 pass"
            sshCommand remote: remote, command: "docker login -u ${env.DOCKER_USER} -p ${env.DOCKER_PASSWORD} $DOCKER_SERVER"
            sshCommand remote: remote, command: "docker pull ${imageName}"
            sshCommand remote: remote, command: "docker stop web | true"
            sshCommand remote: remote, command: "docker rm web | true"
            sshCommand remote: remote, command: "docker run --name web -d ${imageName}"
          }
        }
      }
    }
  }
}

Docker Compose

Docker Compose 的代码和上述类似,仅有少许不同:

sshCommand remote: remote, sudo: true, command: "apt-get install -y gnupg2 pass"
sshCommand remote: remote, command: "docker login -u ${env.DOCKER_USER} -p ${env.DOCKER_PASSWORD} $DOCKER_SERVER"
sshCommand remote: remote, sudo: true, command: "mkdir -p /var/www/site/"
sshCommand remote: remote, sudo: true, command: "chmod 777 /var/www/site/"
sshPut remote: remote, from: 'docker-compose.yml', into: '/var/www/site/'
sshCommand remote: remote, command: "cd /var/www/site/ && echo IMAGE=${imageName} > .env && echo APP_KEY=${env.APP_KEY} >> .env && echo DB_CONNECTION=sqlite >> .env"
sshCommand remote: remote, command: "cd /var/www/site/ && docker-compose down --remove-orphans"
sshCommand remote: remote, command: "cd /var/www/site/ && docker-compose up -d --no-build"

docker-compose.yml 代码:

version: '2.1'
services:
  web:
    env_file: .env
    build: .
    image: ${IMAGE:-laravel-demo:dev}
    ports:
     - "80:80"
    links:
    - redis
  redis:
    image: "redis:5"
上一篇自动部署到 Linux Docker 服务器
文档是否对您有用?
感谢反馈有用
感谢反馈没用