【2、持續集成】【AmberNAS】在CODING上建立持續繼承環境

在CODING上建立持續繼承環境

CI工具

CODING 團隊版 注意,不是我的版

前期準備

添加密鑰

爲項目添加密鑰,以便讓集成環境能夠訪問咱們的遠程服務器,發佈程序。經過ssh-keygen生成密鑰,最好能夠重命名,防止覆蓋本地密鑰。html

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/amber/.ssh/id_rsa): demo  #重命名demo
Enter passphrase (empty for no passphrase):  # 設置密碼 這裏直接回車略過過
Enter same passphrase again: 
Your identification has been saved in demo. 
Your public key has been saved in demo.pub. 
The key fingerprint is:
SHA256:+ie8nyDsn3V+gh1BQBmH6a3p8FVvfZKxZ/UsoxAbECk amber@amber-ubuntu

Linux密鑰默認在/home/{user}/.ssh/目錄下,Windows密鑰默認在C:\Users\{user}\.ssh在。在這個文件夾下demo就是密鑰,demo.pub就是公鑰。前端

將公鑰添加至服務器的信任列表中

將經過ssh-keygen生成的密鑰保存至coding中,留意ID,這裏之後會用到。
圖片.pngjava

Spring Boot 的持續繼承

建立團隊,與項目後,須要初始化一個倉儲,用來管理後臺代碼。web

參考文檔

CODING- 持續繼承快速入門
CODING-配置文件DEMO

爲Spring Boot建立服務

在目標服務器上建立服務,以即可以經過systemctl快速管理服務。在/etc/systemd/system目錄下建立一個spring.service文件。spring

[Unit]
Description=spring
After=syslog.target
 
[Service]
ExecStart= /usr/jdk1.8.0_191/bin/java -jar /data/www/target/server-0.0.1.jar  # 在這裏啓動spring項目
 
[Install]
WantedBy=multi-user.target

設置緩存

經過設置緩存,加速構建速度
圖片.pngshell

配置持續繼承

能夠經過如下文件進行配置,注意看註釋npm

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 '構建中...'   // 這裏開始下載依賴,打包程序
        sh 'mvn install'
        sh 'mvn package'
        sh 'ls'
        echo '構建完成.'
      }
    }
    stage('部署') {
      steps {
        echo '部署中...'
        script {
          def remote = [:]   // 這裏開始登錄目標主機發布程序
          remote.name = 'web-server'
          remote.allowAnyHosts = true
          remote.host = 'xxxxx.com'  //這裏填寫服務器地址
          remote.user = 'root'  // 登錄用戶名
          // credentialsId: 密鑰存儲在coding上,返回的id
          withCredentials([sshUserPrivateKey(credentialsId: 'c93aac1c-ba76-4149-87e6-45fb5590e613', keyFileVariable: 'id_rsa')]) {
            remote.identityFile = id_rsa
            // 將jar包上傳至服務器
            sshPut remote: remote, from: 'target/server-0.0.1.jar', into: '/data/www/target'
            // 重啓服務
            sshCommand remote: remote, command: "systemctl stop spring"
            sshCommand remote: remote, command: "systemctl start spring"
          }
        }
      }
    }
  }
}

Angular的持續繼承

前端的持續繼承與後端基本保持一致。值得注意的是須要把npm的緩存打開。Angular的配置文件以下所示ubuntu

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 '構建中...'
        sh 'npm i'
        sh 'npm run build:prod'
      }
    }
    stage('部署') {
      steps {
        echo '部署中...'
        script {
          def remote = [:]
          remote.name = 'web-server'
          remote.allowAnyHosts = true
          remote.host = 'xxxx.xxx'
          remote.user = 'root'
          withCredentials([sshUserPrivateKey(credentialsId: 'c93aac1c-ba76-4149-87e6-45fb5590e613', keyFileVariable: 'id_rsa')]) {
            // 私鑰文件地址
            remote.identityFile = id_rsa
            sshPut remote: remote, from: 'dist/nas-web/', into: '/data/www/target/dist'
          }
        }

      }
    }
  }
}
相關文章
相關標籤/搜索