1. 程式人生 > >Java配置4-配置jenkins pipline

Java配置4-配置jenkins pipline

目錄

首先確保jenkins伺服器上的jenkins使用者可以使用JDK、git、maven,jenkins安裝了Open Blue Ocean外掛。

感謝南哥,文件大部分是他寫的,我只是做了排版。

2.建立pipeline專案

1)建立新的pipeline

登入jenkins,進入Open Blue Ocean

程式碼庫選擇git,將gitlab專案的ssh連線地址粘到倉庫url中。

然後下面會生成祕鑰,需要將祕鑰粘到gitlab中。

2)新增ssh祕鑰到gitlab

登入gitlab

選擇使用者設定中的ssh祕鑰,將祕鑰新增到右面的祕鑰框中,點選增加祕鑰即可。

新增成功ssh祕鑰後,點選pipeline建立介面的建立pipeline會生成專案的管道。點選進入專案管道,裡面就是專案中的各個分支。

3)配置定時掃描

選擇管道中的configure選項,進入管道配置。勾選上triggers即可實現隔一段時間掃描gitlab專案程式碼的各分支是否有變化。有變化就開始執行專案中的JenkinsFile。

4)配置filter by name

在專案源中可以add filter by name配置,直接篩選jenkins要管理的分支。

5)配置http連線

也可以將git源的地方改為http的連線,這是需要add認證。

點選Add,選擇Username with password,在username和password處填上gitlab的使用者和密碼即可。

6)新建任務建立

選擇新建任務,選擇pipeline型別即可。和blue ocean的configure的介面相同,只需配置git源路徑和掃描定時器即可。詳細見上一種方法。

Jenkinsfile是建立在專案的根目錄下。

pipeline {
  agent any
  parameters {//引數
    //jar包名
    string(name: 'AppName', defaultValue: 'access-antfin-application')
    string(name: 'AppHostName', defaultValue: 'access-antfin-application')
    string(name: 'PersistName', defaultValue: 'access-persist')
    //伺服器名稱
    string(name: 'PersistHostName', defaultValue: 'access-persist')
    string(name: 'DidiName', defaultValue: 'access-didi-application')
    string(name: 'DidiHostName', defaultValue: 'access-antfin-application')
		//應用埠號
    string(name: 'Port', defaultValue: '11000')
    string(name: 'PersistPort', defaultValue: '11001')
    string(name: 'DidiPort', defaultValue: '11002')
  }

  stages {
    stage('dev構建') { //stage是一個執行塊
				//判斷分支 以dev開頭繼續,不然跳過
        when {
            branch 'dev*'
        }

        steps {
          echo env.BRANCH_NAME //輸出分支名
          sh 'mvn -DskipTests=true clean install -T 5 -P dev' //跳過test,以dev的profile clean install
          //sh 'mvn -DskipTests=true clean install -T 5 -P dev,control'
          //sh 'mvn -DskipTests=true deploy -T 5 -P dev'
        }
    }

    stage('dev停止服務') {
        when {
            branch 'dev*'
        }
        steps {
            script {
                try{
                    sh '''echo  "開始停止$AppName服務 $(date '+%Y_%m_%d %H:%M:%S')"'''
                    sh "echo 目標伺服器:dev-$AppHostName, 目標應用:$AppName"
                    sh 'scp  ~/shutdown.sh 
[email protected]
$AppHostName:/home/admin/' //copyshutdown指令碼到應用伺服器的/home/admin sh 'ssh [email protected]$AppHostName "chmod +x /home/admin/shutdown.sh"' //賦予shutdown指令碼執行許可權 sh 'ssh [email protected]$AppHostName "chown admin:admin /home/admin/shutdown.sh "' //將shutdown指令碼的擁有者改為admin:admin sh '''ssh [email protected]$AppHostName "su - admin -c 'nohup ./shutdown.sh $AppName &'"''' //以admin使用者執行shutdown指令碼 }catch (exc) { echo '停止應用服務異常' } sh '''echo "停止$AppName服務結束 $(date '+%Y_%m_%d %H:%M:%S')"''' try{ sh '''echo "開始停止$PersistName服務 $(date '+%Y_%m_%d %H:%M:%S')"''' sh "echo 目標伺服器:dev-$PersistHostName, 目標應用:$PersistName" sh 'scp ~/shutdown.sh [email protected]$PersistHostName:/home/admin/' sh 'ssh [email protected]$PersistHostName "chmod +x /home/admin/shutdown.sh"' sh 'ssh [email protected]$PersistHostName "chown admin:admin /home/admin/shutdown.sh "' sh '''ssh [email protected]$PersistHostName "su - admin -c 'nohup ./shutdown.sh $PersistName &'"''' }catch (exc) { echo '停止應用服務異常' } sh '''echo "停止$PersistName服務結束 $(date '+%Y_%m_%d %H:%M:%S')"''' try{ sh '''echo "開始停止$DidiName服務 $(date '+%Y_%m_%d %H:%M:%S')"''' sh "echo 目標伺服器:dev-$DidiHostName, 目標應用:$DidiName" sh 'scp ~/shutdown.sh [email protected]$DidiHostName:/home/admin/' sh 'ssh [email protected]$DidiHostName "chmod +x /home/admin/shutdown.sh"' sh 'ssh [email protected]$DidiHostName "chown admin:admin /home/admin/shutdown.sh "' sh '''ssh [email protected]$DidiHostName "su - admin -c 'nohup ./shutdown.sh $DidiName &'"''' }catch (exc) { echo '停止應用服務異常' } sh '''echo "停止$DidiName服務結束 $(date '+%Y_%m_%d %H:%M:%S')"''' } } } stage('dev部署') { when { branch 'dev*' } steps { sh 'scp $AppName/target/$AppName.jar [email protected]$AppHostName:/home/admin/' //將jenkins編譯生成的jar包遠端拷貝到對應的應用伺服器的/home/admin目錄下 sh 'ssh [email protected]$AppHostName "chown admin:admin /home/admin/*.jar "' //將jar包的擁有者改為admin:admin sh 'scp $PersistName/target/$PersistName.jar [email protected]$PersistHostName:/home/admin/' sh 'ssh [email protected]$PersistHostName "chown admin:admin /home/admin/*.jar "' sh 'scp $DidiName/target/$DidiName.jar [email protected]$DidiHostName:/home/admin/' sh 'ssh [email protected]$DidiHostName "chown admin:admin /home/admin/*.jar "' } } stage('dev啟動服務') { when { branch 'dev*' } steps { sh '''ssh [email protected]$AppHostName "su - admin -c 'nohup java -jar $AppName.jar >> ./logs/$AppName.log &' "''' //後臺啟動jar包,並將日誌輸出到log中,不輸出控制檯日誌可寫為 >> /dev/null sh '''ssh [email protected]$PersistHostName "su - admin -c 'nohup java -jar $PersistName.jar >> ./logs/$PersistName.log &' "''' sh '''ssh [email protected]$DidiHostName "su - admin -c 'nohup java -jar $DidiName.jar >> ./logs/$DidiName.log &' "''' sh 'sleep 130' //啟動等待時間 sh '''echo 結束 date "+%Y_%m_%d %H:%M:%S" ''' } } stage('dev健康檢查') { when { branch 'dev*' } steps { sh ' curl -X GET http://dev-$AppHostName:$Port/health ' //健康檢查,訪問伺服器應用埠的 /health請求。又返回內容即檢查成功 sh ' curl -X GET http://dev-$PersistName:$PersistPort/health ' sh ' curl -X GET http://dev-$DidiHostName:$PersistPort/health ' } } stage('test構建') { when { branch 'test*' } steps { echo env.BRANCH_NAME sh 'mvn -DskipTests=true clean install -T 5 -P test' //sh 'mvn -DskipTests=true clean install -T 5 -P test,control' sh 'cp $AppName/target/$AppName.jar ~/test/test-$AppName.jar' sh 'cp $PersistName/target/$PersistName.jar ~/test/test-$PersistName.jar' sh 'cp $DidiName/target/$DidiName.jar ~/test/test-$DidiName.jar' } } stage('test停止服務') { when { branch 'test*' } steps { script { try{ sh '''echo "開始停止$AppName服務 $(date '+%Y_%m_%d %H:%M:%S')"''' sh "echo 目標伺服器:test-$AppHostName, 目標應用:$AppName" sh 'scp ~/shutdown.sh [email protected]$AppHostName:/home/admin/' sh 'ssh [email protected]$AppHostName "chmod +x /home/admin/shutdown.sh"' sh 'ssh [email protected]$AppHostName "chown admin:admin /home/admin/shutdown.sh "' sh '''ssh [email protected]$AppHostName "su - admin -c 'nohup ./shutdown.sh $AppName &'"''' }catch (exc) { echo '停止應用服務異常' } sh '''echo "停止$AppName服務結束 $(date '+%Y_%m_%d %H:%M:%S')"''' try{ sh '''echo "開始停止$PersistName服務 $(date '+%Y_%m_%d %H:%M:%S')"''' sh "echo 目標伺服器:test-$PersistHostName, 目標應用:$PersistName" sh 'scp ~/shutdown.sh [email protected]$PersistHostName:/home/admin/' sh 'ssh [email protected]$PersistHostName "chmod +x /home/admin/shutdown.sh"' sh 'ssh [email protected]$PersistHostName "chown admin:admin /home/admin/shutdown.sh "' sh '''ssh [email protected]$PersistHostName "su - admin -c 'nohup ./shutdown.sh $PersistName &'"''' }catch (exc) { echo '停止應用服務異常' } sh '''echo "停止$PersistName服務結束 $(date '+%Y_%m_%d %H:%M:%S')"''' try{ sh '''echo "開始停止$DidiName服務 $(date '+%Y_%m_%d %H:%M:%S')"''' sh "echo 目標伺服器:test-$DidiHostName, 目標應用:$DidiName" sh 'scp ~/shutdown.sh [email protected]$DidiHostName:/home/admin/' sh 'ssh [email protected]$DidiHostName "chmod +x /home/admin/shutdown.sh"' sh 'ssh [email protected]$DidiHostName "chown admin:admin /home/admin/shutdown.sh "' sh '''ssh [email protected]$DidiHostName "su - admin -c 'nohup ./shutdown.sh $DidiName &'"''' }catch (exc) { echo '停止應用服務異常' } sh '''echo "停止$DidiName服務結束 $(date '+%Y_%m_%d %H:%M:%S')"''' } } } stage('test部署') { when { branch 'test*' } steps { sh 'scp $AppName/target/$AppName.jar [email protected]$AppHostName:/home/admin/' sh 'ssh [email protected]$AppHostName "chown admin:admin /home/admin/*.jar "' sh 'scp $PersistName/target/$PersistName.jar [email protected]$PersistHostName:/home/admin/' sh 'ssh [email protected]$PersistHostName "chown admin:admin /home/admin/*.jar "' sh 'scp $DidiName/target/$DidiName.jar [email protected]$DidiHostName:/home/admin/' sh 'ssh [email protected]$DidiHostName "chown admin:admin /home/admin/*.jar "' } } stage('test啟動服務') { when { branch 'test*' } steps { sh '''ssh [email protected]$AppHostName "su - admin -c 'nohup java -jar $AppName.jar >> ./logs/$AppName.log &' "''' sh '''ssh [email protected]$PersistHostName "su - admin -c 'nohup java -jar $PersistName.jar >> ./logs/$PersistName.log &' "''' sh '''ssh [email protected]$DidiHostName "su - admin -c 'nohup java -jar $DidiName.jar >> ./logs/$DidiName.log &' "''' sh 'sleep 130' sh '''echo 結束 date "+%Y_%m_%d %H:%M:%S" ''' } } stage('test健康檢查') { when { branch 'test*' } steps { sh ' curl -X GET http://test-$AppHostName:$Port/health ' sh ' curl -X GET http://test-$PersistName:$PersistPort/health ' sh ' curl -X GET http://test-$DidiHostName:$PersistPort/health ' } } stage('release構建') { when { branch 'release*' } steps { echo env.BRANCH_NAME sh 'mvn -DskipTests=true clean install -T 5 -P product' //sh 'mvn -DskipTests=true clean install -T 5 -P product,control' //sh 'mkdir ~/release' sh 'cp $AppName/target/$AppName.jar ~/release/release-$AppName.jar' sh 'cp $PersistName/target/$PersistName.jar ~/release/release-$PersistName.jar' sh 'cp $DidiName/target/$DidiName.jar ~/release/release-$DidiName.jar' } } } }