1. 程式人生 > >jenkins-1、建立pipeline及相關指令碼編寫

jenkins-1、建立pipeline及相關指令碼編寫

新建立一個pipeline專案。

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

然後寫第一個指令碼,例如:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'echo "Hello World"'
                sh '''
                    echo "Multiline shell steps works too"
                    ls -lah
                '''
            }
        }
    }
}

這裡寫圖片描述

儲存

這裡寫圖片描述

構建,檢視結果。

這裡寫圖片描述

各個流程演示

pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
                echo 'Checkout'

            }
        }        
        stage('Build') {
            steps {
                echo 'Building'
                echo 'sh \'mvn clean install\' -- 可以用自己的 mvn clean deploy + 引數替代 '
} } stage('Test') { steps { echo 'Testing' echo ' sh \'mvn clean verify sonar:sonar\' -- 此處可以使用mvn test替代,筆者這步是檢測程式碼的質量同步到自己的程式碼質量檢測平臺。' } } stage('Deploy') { steps { echo
'Deploying' echo 'sh \'mvn clean deploy\' -- 此處呼叫指令碼或者ansible、saltstak,部署到遠端 ' } } } post { always { echo 'This will always run' } success { echo 'This will run only if successful' } failure { echo 'This will run only if failed' } unstable { echo 'This will run only if the run was marked as unstable' } changed { echo 'This will run only if the state of the Pipeline has changed' echo 'For example, if the Pipeline was previously failing but is now successful' } } }

構建結果。

這裡寫圖片描述

這裡寫圖片描述

demo1

演示pipeline引數化構建–(即新增輸入輸出介面)執行sh指令碼及從svn拉取原始碼

#!/usr/bin/env groovy
pipeline{
    agent none
    options{
        disableConcurrentBuilds()
        skipDefaultCheckout()
        timeout(time: 1, unit: 'HOURS')
        timestamps()
    }
    parameters{
        string(name: 'PERSON', defaultValue: 'among中文', description: '請輸入中文')
        booleanParam(name: 'YESORNO', defaultValue: true, description: '是否釋出')
        choice(name: 'Server_addr', choices: '10.114.24.192:7722\n10.129.14.147:7722\n10.114.4.208:6005\n127.0.0.1:6005', description: '被測系統ip及埠')
    }
    stages{
    stage('test stage')
    {
      agent
      {
          label 'master' 
      }
      steps
       {

    sh returnStatus: true, script: 'source /etc/profile && source ~/.bash_profile && env'


          echo 'Hello, stage1'
          echo "Hello ${params.PERSON}"
          echo "Hello ${env.PERSON}"
          echo "Hello ${params.choice}"
                script {
                    def browsers = ['chrome', 'firefox']
                    for (int i = 0; i < browsers.size(); ++i) {
                        echo "Testing the ${browsers[i]} browser"
                    }
                }  

checkout([$class: 'SubversionSCM', 
          additionalCredentials: [], 
          excludedCommitMessages: '', 
          excludedRegions: '', 
          excludedRevprop: '', 
          excludedUsers: '', 
          filterChangelog: false, 
          ignoreDirPropChanges: false, 
          includedRegions: '', 
          locations: [[credentialsId: 'e522721e-4a9a-467c-b154-acb803d8afb0', 
                       depthOption: 'infinity', 
                       ignoreExternalsOption: true, 
                       remote: 'svn://你的svn的地址/FunFunLeBaseLib']], 
          workspaceUpdater: [$class: 'UpdateUpdater']])


         }
     }
        }
}

注意:

credentialsId 是你在jenkins新增的一個憑據,新增以後會有一個唯一標識給你的。

連續執行三次看結果:

構建前:

這裡寫圖片描述

點選構建:

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

每次都會重新checkout一次原始碼,執行成功,

然而, 假如是大專案的話這樣一次構建估計光是checkout都要不少時間,所以本人不推薦每次都checkout的。

普通java app clean 打包 及釋出到nexus上

經過不斷除錯和試錯調整,終於將一份可以用的java的pipeline指令碼完成,注意,用checkout的原因是,假如直接svn同步更新到我們的指定目錄然後用該目錄編譯那麼就會有–第一,需要刪除jar,target檔案,有許可權的限制,第二,刪除後會有衝突的問題。

#!/usr/bin/env groovy
pipeline{
    agent any
  environment {
    REVISION = "0.0.${env.BUILD_ID}"
  }
    options{
        disableConcurrentBuilds()
        skipDefaultCheckout()
        timeout(time: 1, unit: 'HOURS')
        timestamps()
    }
    parameters{
        string(name: 'PERSON', defaultValue: '測試-欄位輸入', description: '請輸入中文')
        booleanParam(name: 'YESORNO', defaultValue: true, description: '測試-布林值')
        choice(name: 'Server_addr', choices: '10.114.24.192:7722\n10.129.14.147:7722\n10.114.4.208:6005\n127.0.0.1:6005', description: '被測系統ip及埠')
    }
    stages{
        stage ('Initialize') {
          steps {
              sh '''
              echo "任務初始化..."
              echo "構建版本revision:${REVISION}"
              '''
              sh '''
              echo "專案檢出...."
              '''

checkout([$class: 'SubversionSCM',
          additionalCredentials: [],
          excludedCommitMessages: '',
          excludedRegions: '',
          excludedRevprop: '',
          excludedUsers: '',
          filterChangelog: false,
          ignoreDirPropChanges: false,
          includedRegions: '',
          locations: [[credentialsId: 'e522721e-4a9a-467c-b154-acb803d8afb0',
                       depthOption: 'infinity',
                       ignoreExternalsOption: true,
                       remote: 'svn://您的svn/FunFunLeBaseLib']],
          workspaceUpdater: [$class: 'UpdateUpdater']])

          }
        }

    stage ('Build') {
      steps {
        echo '構建階段....'
        sh 'mvn clean package -e -f FunFunLeBaseLib/pom.xml'
      }
    }
    stage ('Deploy') {
      steps {
      echo '釋出階段....'
        script {
          currentBuild.displayName = "${REVISION}"
        }
        sh 'mvn deploy  -Drevision=${REVISION} -e -f FunFunLeBaseLib/pom.xml'
      }
    }
    }

post {
        always {
            echo '構建結束...'
        }
        success {
            echo '恭喜您,構建成功!!!'
        }
        failure {
            echo '抱歉,構建失敗!!!'
        }
        unstable {
            echo '該任務已經被標記為不穩定任務....'
        }
        changed {
            echo ''
        }
    }

}

這裡寫圖片描述