0
点赞
收藏
分享

微信扫一扫

Coding持续集成Jenkins-pipeline自动化部署

把一个SpringBoot源码在DevOps一站式研发平台编译成jar远程部署到服务器分为几步?答:3步。

  • 1、打成压缩包;
  • 2、SCP 上传到服务器;
  • 3、SSH 解压;

如果使用持续集成,则可以实现提交代码时自动上线,原理如下图:

DevOps自动上线原理


常见的持续集成有:商业化的 CircleCI、开源的 Jenkins,本文以小编公司使用的 Jenkins 云服务—Coding为例
coding持续集成说明https://coding.net/products/ci?cps_source=PIevZ6Jr


实战示例:

  • 注册CODING,创建一个 Git 仓库,提交代码(该平台5人以下团队免费这个已经很不错了);
  • 服务器创建SSH公私钥
    命令如下:
ssh-keygen -m PEM -t rsa -b 4096 -C "your.email@example.com"
  • 创建好后会在目录/root/.ssh生成两个文件(id_rsa是私钥|id_rsa.pub是公钥)
[root@VM_0_3_centos .ssh]# pwd
/root/.ssh
[root@VM_0_3_centos .ssh]# ll
total 16
-rw------- 1 root root  743 Apr 10 14:32 authorized_keys
-rw------- 1 root root 3243 Apr 10 13:20 id_rsa
-rw-r--r-- 1 root root  742 Apr 10 13:20 id_rsa.pub
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 {
          def remote = [:]
          remote.name = 'web-server'
          remote.allowAnyHosts = true
          remote.host = '106.54.18.228'
          remote.user = 'root'
          // 需要先创建一对 SSH 密钥,把私钥放在 CODING 凭据管理,把公钥放在服务器的 `.ssh/authorized_keys`,实现免密码登录
          withCredentials([sshUserPrivateKey(credentialsId: "d770d752-6d95-4936-9936-xxxxxxxxx", keyFileVariable: 'id_rsa')]) {
            remote.identityFile = id_rsa
            // SSH 上传文件到远端服务器
            sshCommand remote: remote, command: "sh /opt/test.sh"
            sshCommand remote: remote, command: "for i in {1..5}; do echo -n \"Loop \$i \"; date ; sleep 1; done"
          }
        }
        echo '部署完成'
      }
    }
  }
}

-最终构建成功打印结果(和jenkins控制台日志输出很像)



通过上图可以看到构建日志已经成功打印出远程执行服务的shell脚本信息。

  • 附jenkins控制台日志输出对比


    原创不易,转载请注明出处

举报

相关推荐

0 条评论