使用Coding.net實現自動化編譯部署SpringBoot項目

構建計劃:Jenkinsfile

pipeline {
  agent any
  environment {
    APP_Jar = 'spider-flow-web.jar'
    TAR_Dir = '/root/workspace/spider-flow-web/target'
    RUN_IP = 'x0.0.19x.14'
    RUN_Dir = '/usr/www'
  }
  stages {
    stage('檢出Git') {
      steps {
        checkout([
          $class: 'GitSCM',
          branches: [[name: GIT_BUILD_REF]],
          userRemoteConfigs: [[
            url: GIT_REPO_URL,
            credentialsId: CREDENTIALS_ID
          ]]])
        }
      }
      stage('編譯構建Maven') {
        steps {
          echo '構建中...'
          sh 'java -version'
          sh 'mvn clean install package'
          archiveArtifacts(artifacts: '**/target/*.jar', fingerprint: true, onlyIfSuccessful: true)
          echo '構建完成.'
          sh "cd $TAR_Dir "
          sh 'ls -all'
        }
      }
      stage('上線發佈申請') {
        steps {
          input(message: '本次發佈上線記錄', submitter: 'VddBbpfEo', parameters: [ string(name:'Version',description:'上線版本號')
                              , string(name:'Description',description:'本次上線版本內容說明')
                            ])
        }
      }
      stage('部署SSH') {
        steps {
          script {
            def remote = [:]
            remote.name = "matols"
            remote.host = "$RUN_IP"
            remote.port = 22
            remote.allowAnyHosts = true

            // 把「CODING 憑據管理」中的「憑據 ID」填入 credentialsId,而 id_rsa 無需修改
            //withCredentials([sshUserPrivateKey(credentialsId: "b87exx-47ed-9c7f-x71ea",keyFileVariable: "id_rsa")]) {
              //remote.identityFile = id_rsa
              // 使用ssh用戶名密碼進行遠程操作
              withCredentials([usernamePassword(credentialsId:"${env.SSH_server01}",usernameVariable:'USERNAME',passwordVariable:'PASSWORD')]){
                remote.user = USERNAME
                remote.password = PASSWORD
                echo "SSH with ${APP_Jar}"
                echo '終止進程...'
                try {
                  sshCommand remote: remote, command: "ps -ef|grep $APP_Jar|awk '{print \$2}'|xargs kill -9"
                } catch (t) {
                  echo " ${APP_Jar} 沒有運行! "
                }
                echo '刪除程序...'
                sshCommand remote: remote, command: "rm -rf $RUN_Dir/$APP_Jar"
                echo '傳輸程序...'
                sshPut remote: remote, from: "$TAR_Dir/$APP_Jar", into: "$RUN_Dir"
                echo '啓動程序...'
                //取消後臺常駐運行nohup,否則該執行計劃無法結束,持續掛起
                sshCommand remote: remote, command: "nohup java -jar $RUN_Dir/$APP_Jar > $RUN_Dir/nohup.out 2>&1 &"
                echo " ${APP_Jar} 已經部署成功,並運行! "
              }
            }

          }
        }
      }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章