ks-devops icon indicating copy to clipboard operation
ks-devops copied to clipboard

How to deploy applicatons to VM by ks devops?

Open ZengIan opened this issue 3 years ago • 0 comments
trafficstars

Installer Version

ks v3.2.1

1,Create ssh key credentials image You can view the private key in the path: /root/.ssh/id_rsa 2,Edit jenkinsfile, about 3 steps: Example: https://github.com/kubesphere/devops-maven-sample, You can fork to your own registry~

1)clone code 2)build & push to vm 3)run java app,It will kill the existing java process and restart it every time. For the VM path, you need to create it in advance, if there is no path.

pipeline {
  agent {
    node {
      label 'maven'
    }
  }
  stages {
    stage('clone code') {
      steps {
        container('base') {
          checkout([$class: 'GitSCM', branches: [[name: 'master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'https://gitee.com/ian_zeng/devops-java-sample.git']]])
        }
      }
    }
    stage('build & push to vm') {
      agent none
      steps {
        container('maven') {
          sh 'mvn -o -Dmaven.test.skip=true -gs `pwd`/configuration/settings.xml clean package'
          withCredentials([sshUserPrivateKey(credentialsId : 'host' ,keyFileVariable : 'SSH_KEY' ,usernameVariable : 'root' ,)]) {
            sh 'scp -i $SSH_KEY -o StrictHostKeyChecking=no target/*.jar "$root"@"$VM_IP":/jar/demo.jar'
          }
        }
      }
    }
    stage('run java app') {
      steps {
        container('maven') {
          withCredentials([sshUserPrivateKey(credentialsId : 'host' ,keyFileVariable : 'SSH_KEY' ,usernameVariable : 'root' ,)]) {
            sh 'ssh -i $SSH_KEY -o StrictHostKeyChecking=no "$root"@"$VM_IP" "killall java; nohup java -jar /jar/demo.jar > /var/log/demo-java.file 2>&1 &"'
          }
        }
      }
    }
  }
  environment {
    GITHUB_CREDENTIAL_ID = 'gitee'
    VM_IP = '172.20.100.7'
    VM_CREDENTIAL_ID = 'host'
    GITHUB_ACCOUNT = 'ian_zeng'
  }
}

3,Then you can access to vm_IP :8080 on web browser image

ZengIan avatar Jul 07 '22 05:07 ZengIan