1. 程式人生 > 實用技巧 >jenkins自動打包部署流水線

jenkins自動打包部署流水線

專案程式碼

流水線設計

#!groovy

//下載原始碼
String srcUrl = "${env.srcUrl}"
String branchName = "${env.branchName}"


//打包
String buildType = "${env.buildType}"
String buildShell = "${env.buildShell}"


pipeline {

    agent {
      node {
          label 'master'
      }
    }
    
    stages {
       stage('GetCode
') { steps { script { checkout([$class: 'GitSCM', branches: [[name: '${branchName}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'gitlab', url: '${srcUrl}']]]) } } } stage(
'Build') { steps { script { mvnhome = tool "M2" sh "${mvnhome}/bin/mvn ${buildShell}" jarName = sh returnStdout: true, script: "cd target && ls *.jar" jarName = jarName - "\n
" sh "cp target/${jarName} /root" } } } stage('Test') { steps { script { println("dan'yuan單元ce'shi") } } } stage('Deploy') { steps { script { println("ansible bu shu......") sh "java -jar /root/${jarName}" } } } } }
View Code

部署結果

Jenkins設定自己的全域性環境變數

maven打包問題處理

jenkins控制日誌內容的輸出

有些執行命令的內容不方便在日誌中打印出來

sh "/home/jenkins/a.sh 2>&1 >/dev/null" 顯示結果如下

把需要執行的命令提取到一個單獨的指令碼進行呼叫即可

[jenkins]$ vi a.sh

#!/bin/bash +x
/usr/local/sbin/kubectl --kubeconfig /home/jenkins/kubectl/k8s-cluster/siem-cluster/kube_config_rancher-cluster.yml replace -f /home/jenkins/kubectl/k8s-cluster/siem-cluster/config-center.yml --force
echo "a.sh 執行完畢"
a.sh

詳細的命令資訊不再進行顯示輸出