当前位置:   article > 正文

使用Coding部署项目_coding 服务重启

coding 服务重启

coding概述:提供一站式开发协作工具,帮助研发团队快速落地敏捷开发与 DevOps 开发方式,实现研发效能升级

一、创建项目

省略 详细文档:https://g-mnbk6665.coding.net/quickstart

二、SSH连接

关于ssh相关命令

重启SSH服务
systemctl restart sshd
开机自动启动ssh命令
sudo systemctl enable sshd
关闭ssh开机自动启动命令
sudo systemctl disable ssh
单次开启ssh
sudo systemctl start ssh
单次关闭ssh
sudo systemctl stop ssh
设置好后重启系统
查看ssh是否启动,看到Active: active (running)即表示成功
sudo systemctl status sshd
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
ssh-keygen -t rsa
  • 1

cd /root/.ssh
  • 1

生成了三个文件:
authorized_keys:存放远程免密登录的公钥,主要通过这个文件记录多台机器的公钥id_rsa : 生成的私钥文件id_rsa.pub : 生成的公钥文件
进入id_rsa.pub文件,并将里面的内容全部拷贝到authorized_keys文件中保存

cat id_rsa.pub
vim authorized_keys
  • 1
  • 2

再查看私钥 id_rsa 内容并全部复制:配置coding即可

cat id_rsa
  • 1

三、SpringBoot

持续集成

关联项目,结合自己项目进行关联

选择Spring+Docker模板


变量配置信息

构建方式有很多种 (分支,标签,版本号)

3.1 多环境配置

配置你的环境,可打包时选择相应环境

至此持续集成就完成了,但要考虑logs文件的保留问题,可通过脚本Copy到备份文件夹

3.2 Dockerfile

方式一
# 基础镜像
FROM  openjdk:8-jre
# author
MAINTAINER zxht
# 东八区
ENV TZ='Asia/Shanghai'
# 挂载目录
VOLUME /home/service
# 创建目录
RUN mkdir -p /home/service
# 指定路径
WORKDIR /home/service
# 复制jar文件到路径
COPY ./mes-web/target/mes.jar /home/service/mes.jar
# 启动系统服务
ENTRYPOINT ["java","-jar","mes.jar"]

方式二
FROM coding-public-docker.pkg.coding.net/public/docker/openjdk:8

COPY ./mes-web/target/mes.jar /root/workspace/mes.jar

CMD ["java", "-jar", "mes.jar"]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

3.3 静态Jenkinsfile

pipeline {
  agent any
  stages {
    stage('检出') {
      steps {
        checkout([$class: 'GitSCM',
        branches: [[name: GIT_BUILD_REF]],
        userRemoteConfigs: [[
          url: GIT_REPO_URL,
          credentialsId: CREDENTIALS_ID
        ]]])
      }
    }
    stage('编译') {
      steps {
        sh 'mvn clean install -e -U -P ${PROFILES}'
      }
    }
    stage('构建镜像并推送到 CODING Docker 制品库') {
      steps {
        script {
          docker.withRegistry(
            "${CCI_CURRENT_WEB_PROTOCOL}://${CODING_DOCKER_REG_HOST}",
            "${CODING_ARTIFACTS_CREDENTIALS_ID}"
          ) {
            def dockerImage = docker.build("${CODING_DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_VERSION}", "-f ${DOCKERFILE_PATH} ${DOCKER_BUILD_CONTEXT}")
            dockerImage.push()
          }
        }

      }
    }
    stage('部署到远端服务') {
      steps {
        script {
          def remoteConfig = [:]
          remoteConfig.name = "my-remote-server"
          remoteConfig.host = "${REMOTE_HOST}"
          remoteConfig.port = "${REMOTE_SSH_PORT}".toInteger()
          remoteConfig.allowAnyHosts = true

          withCredentials([
            sshUserPrivateKey(
              credentialsId: "${REMOTE_CRED}",
              keyFileVariable: "privateKeyFilePath"
            ),
            usernamePassword(
              credentialsId: "${CODING_ARTIFACTS_CREDENTIALS_ID}",
              usernameVariable: 'CODING_DOCKER_REG_USERNAME',
              passwordVariable: 'CODING_DOCKER_REG_PASSWORD'
            )
          ]) {
            // SSH 登录用户名
            remoteConfig.user = "${REMOTE_USER_NAME}"
            // SSH 私钥文件地址
            remoteConfig.identityFile = privateKeyFilePath

            // 请确保远端环境中有 Docker 环境
            sshCommand(
              remote: remoteConfig,
              command: "docker login -u ${CODING_DOCKER_REG_USERNAME} -p ${CODING_DOCKER_REG_PASSWORD} ${CODING_DOCKER_REG_HOST}",
              sudo: true,
            )

            sshCommand(
              remote: remoteConfig,
              command: "docker rm -f mes | true",
              sudo: true,
            )

            // DOCKER_IMAGE_VERSION 中涉及到 GIT_LOCAL_BRANCH / GIT_TAG / GIT_COMMIT 的环境变量的使用
            // 需要在本地完成拼接后,再传入到远端服务器中使用
            DOCKER_IMAGE_URL = sh(
              script: "echo ${CODING_DOCKER_REG_HOST}/${CODING_DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_VERSION}",
              returnStdout: true
            )

            sshCommand(
              remote: remoteConfig,
              command: "docker run -d -p 8999:8999 --name mes ${DOCKER_IMAGE_URL}",
              sudo: true,
            )

            echo "部署成功,请到 http://${REMOTE_HOST}:8999/mes/doc.html#/home"
          }
        }

      }
    }
  }
  environment {
    CODING_DOCKER_REG_HOST = "${CCI_CURRENT_TEAM}-docker.pkg.${CCI_CURRENT_DOMAIN}"
    CODING_DOCKER_IMAGE_NAME = "${PROJECT_NAME.toLowerCase()}/${DOCKER_REPO_NAME}/${DOCKER_IMAGE_NAME}"
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95

四、持续集成 Vue

1.关联仓库后选择模板
2.本次使用的是ssh连接当然也可以选择账户+密码
3.创建制品仓Generic类型存储dist压缩包

变量和缓存

流程配置信息的几个说明点
1.这是创建的制品名称

2.指定Node版本

3.注意你的存放Nginx和文件路径一致否则依赖加载不到

// 服务器远程地址
def remotePath = "/www/wwwroot/kblogs"
  • 1
  • 2
pipeline {
  agent any
  stages {
    stage('检出') {
      steps {
        checkout([$class: 'GitSCM',
        branches: [[name: GIT_BUILD_REF]],
        userRemoteConfigs: [[
          url: GIT_REPO_URL,
          credentialsId: CREDENTIALS_ID
        ]]])
      }
    }
    stage('安装依赖') {
      steps {
        sh 'npx -p node@16.20.2 npm install'
      }
    }
    stage('执行构建') {
      steps {
        echo '开始构建'
        sh 'npx -p node@16.20.2 npm run build'
        echo '构建完成'
      }
    }
    stage('压缩制品Dist') {
      steps {
        echo '压缩中...'
        sh 'tar -zcf dist.tar.gz -C ./src/.vuepress/ dist'
        echo '压缩完成.'
        sh 'ls'
      }
    }
    stage('上传制品') {
      steps {
        echo '开始上传'
        codingArtifactsGeneric(files: 'dist.tar.gz', repoName: 'blogs', version: '${env.GIT_BUILD_REF}')
      }
    }
    stage('部署至服务器') {
      steps {
        script {
          def remote= [:]
          remote.name = "my-remote-server"
          remote.host = "${REMOTE_HOST}"
          remote.allowAnyHosts = true
          // 服务器远程地址
          def remotePath = "/www/wwwroot/kblogs"
          withCredentials([sshUserPrivateKey(
            credentialsId: "${REMOTE_CRED}",
            keyFileVariable: "privateKeyFilePath"
          )]) {
            remote.user = "${REMOTE_USER_NAME}"
            // SSH 私钥文件地址
            remote.identityFile = privateKeyFilePath
            stage("执行ssh脚本") {
              echo '开始执行脚本'
              sshCommand remote: remote, sudo: false, command: "rm -rf ${remotePath}/*"
              sshCommand remote: remote, command: "mkdir -p /www/wwwroot/kblogs/ && touch /www/wwwroot/kblogs/dist.tar.gz"
              sshPut remote: remote, from: './dist.tar.gz', into:remotePath + "/dist.tar.gz"                // SSH 上传文件到远端服务器
              sshCommand remote: remote, command: "tar -zxf ${remotePath}/dist.tar.gz -C ${remotePath}"     // 解压缩
              sshCommand remote: remote, sudo: false, command: "rm -f ${remotePath}/*.tar.gz"              // 删除压缩文件
              sshCommand remote: remote, sudo: false, command: "mv ${remotePath}/dist/* ${remotePath}"    //将dist文件夹所有内容移动到上一级
              sshCommand remote: remote, sudo: false, command: "rm -rf ${remotePath}/dist"    //将dist文件夹所有内容移动到上一级
              echo '脚本执行结束'
            }
          }
        }

      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/698974
推荐阅读
相关标签
  

闽ICP备14008679号