腾讯云的devops自动化部署代替jenkins

起因

jenkins太耗内存了,经常导致服务器崩。
了解到devOps也是做类似的服务的,遂用之。
serverless framework也可以做这个,但是截至目前,只能够打包node项目。

devOps服务器运营商基本都提供,比如腾讯云、阿里云、亚马逊云等,使用方法大致相同

流程配置

devops核心内容就是pipeline 即流程配置。下边提供几个常用的配置

前端项目 推送到服务器

这里拿一个react项目,打包完 将内容推送到服务器的根目录

pipeline {
  agent {
    docker {
      reuseNode 'true'
      registryUrl 'https://coding-public-docker.pkg.coding.net'
      image 'public/docker/nodejs:19-2022'
    }

  }
  stages {
    stage('获取仓库代码') {
      steps {
        checkout([
          $class: 'GitSCM',
          branches: [[name: GIT_BUILD_REF]],
          userRemoteConfigs: [[
            url: GIT_REPO_URL,
            credentialsId: CREDENTIALS_ID
          ]]])
        }
      }

      stage('安装依赖') {
        steps {
          sh 'yarn'
        }
      }

      stage('打包产物') {
        steps {
          sh 'yarn build'
          sh "tar -zcvf tmp.tar.gz build"
        }
      }

      stage('部署') {
        steps {
          echo '部署中...'
          script {
            def remote = [:]
            remote.name = '腾讯云'
            remote.allowAnyHosts = true
            remote.host = '82.157.146.87'
            remote.port = 22
            remote.user = 'root'
            withCredentials([usernamePassword(credentialsId: '6de4c5ea-8436-4ed5-9e33-2b737f6f3032', passwordVariable: 'password', usernameVariable: 'userName')]) {
              remote.user = "${userName}"
              remote.password = "${password}"
              // SSH 上传文件到远端服务器
              sshCommand remote: remote, sudo: true,  command:'rm -rf /usr/share/nginx/html/*'
              sshPut remote: remote, sudo: true, from: './tmp.tar.gz', into: '/usr/share/nginx/html/tmp.tar.gz'
              // // 解压缩
              sshCommand remote: remote, sudo: true, command: "tar -zxf /usr/share/nginx/html/tmp.tar.gz --strip-components 1  -C /usr/share/nginx/html"
            }
          }
          echo '部署完成'
        }
      }

    }
  }

如果不想放到服务器根目录,放到别的目录也是可以的

pipeline {
  ...
  stages {
    ...

      stage('部署') {
        steps {
          echo '部署中...'
          script {
            ...
            withCredentials([usernamePassword(credentialsId: '6de4c5ea-8436-4ed5-9e33-2b737f6f3032', passwordVariable: 'password', usernameVariable: 'userName')]) {
              ...
              // SSH 上传文件到远端服务器
              sshCommand remote: remote, sudo: true,  command:'rm -rf /usr/share/nginx/html/*'
              sshPut remote: remote, sudo: true, from: './tmp.tar.gz', into: '/usr/share/nginx/html/tmp.tar.gz'
              // // 解压缩
              sshCommand remote: remote, sudo: true, command: "tar -zxf /usr/share/nginx/html/tmp.tar.gz --strip-components 1  -C /usr/share/nginx/html"
            }
          }
          echo '部署完成'
        }
      }

    }
  }
posted @ 2023-05-19 10:39  丁少华  阅读(56)  评论(0编辑  收藏  举报