腳本示例
pipeline {
agent any
options {
durabilityHint 'PERFORMANCE_OPTIMIZED'
timeout(time:5, unit: 'MINUTES')
timestamps()
skipStagesAfterUnstable()
// retry(2)
skipDefaultCheckout true
buildDiscarder logRotator(artifactDaysToKeepStr: '1', artifactNumToKeepStr: '1', daysToKeepStr: '10', numToKeepStr: '5')
}
stages {
stage('拉取代碼') {
steps {
echo '正在拉取代碼...'
script {
try {
checkout([$class: 'GitSCM', branches: [[name: 'v1-0-8-apix-20190531']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CloneOption', noTags: true, shallow: true, depth: 1, honorRefspec:true]], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '7e1f82d8-c808-4555-8c82-2a67f6cbcded',refspec: '+refs/heads/v1-0-8-apix-20190531:refs/remotes/origin/v1-0-8-apix-20190531', url: 'git@gitlab.test.cn:app/forseti.git']]])
} catch(Exception err) {
echo err.getMessage()
echo err.toString()
unstable '拉取代碼失敗'
warnError('拉取代碼失敗信息回調失敗') {
retry(5){
httpRequest acceptType: 'APPLICATION_JSON', consoleLogResponseBody: true, contentType: 'APPLICATION_JSON', httpMode: 'POST', ignoreSslErrors: true, requestBody: "{\"step\":\"pull\",\"id\":\"${JOB_NAME}\",\"build_number\":\"${BUILD_NUMBER}\"}", timeout: 5, url: 'http://127.0.0.1:8088/api/v1/job_finish', validResponseCodes: '200', validResponseContent: 'ok'
}
}
}
}
}
}
stage('構建') {
options {
timeout(time:3, unit: 'MINUTES')
}
steps {
echo '正在構建....'
script {
try {
sh 'touch forseti-api.properties'
sh 'mvn -B clean install -DskipTests -U'
} catch (Exception err) {
echo err.getMessage()
echo err.toString()
unstable '構建失敗'
warnError('構建失敗信息回調失敗') {
retry(5) {
httpRequest acceptType: 'APPLICATION_JSON', consoleLogResponseBody: true, contentType: 'APPLICATION_JSON', httpMode: 'POST', ignoreSslErrors: true, requestBody: "{\"step\":\"build\",\"id\":\"${JOB_NAME}\",\"build_number\":\"${BUILD_NUMBER}\"}", timeout: 5, url: 'http://127.0.0.1:8088/api/v1/job_finish', validResponseCodes: '200', validResponseContent: 'ok'
}
}
}
}
}
}
stage('依賴性檢查') {
steps {
echo '正在生成依賴性檢查信息...'
script {
try {
sh 'mvn -B dependency:tree > dependency.log'
} catch(Exception err) {
echo err.getMessage()
echo err.toString()
unstable '依賴性檢查失敗'
warnError('依賴性檢查失敗信息回調失敗') {
retry(5) {
httpRequest acceptType: 'APPLICATION_JSON', consoleLogResponseBody: true, contentType: 'APPLICATION_JSON', httpMode: 'POST', ignoreSslErrors: true, requestBody: "{\"step\":\"check\",\"id\":\"${JOB_NAME}\",\"build_number\":\"${BUILD_NUMBER}\"}", timeout: 5, url: 'http://127.0.0.1:8088/api/v1/job_finish', validResponseCodes: '200', validResponseContent: 'ok'
}
}
}
}
}
}
stage('返回依賴性檢查文件') {
steps {
echo '正在返回依賴性檢查文件給erebsu應用...'
script {
try {
retry(5) {
httpRequest acceptType: 'APPLICATION_JSON', consoleLogResponseBody: true, contentType: 'APPLICATION_OCTETSTREAM', customHeaders: [[maskValue: false, name: 'Content-Disposition', value: 'id=dependency.log']], httpMode: 'POST', ignoreSslErrors: true, multipartName: 'file', requestBody: "{\"id\":\"${JOB_NAME}\"}", timeout: 5, uploadFile: 'dependency.log', url: 'http://127.0.0.1:8088/api/v1/job_data_update', validResponseCodes: '200', validResponseContent: 'ok'
}
} catch(Exception err) {
echo err.getMessage()
echo err.toString()
unstable '依賴性檢查文件返回給erebus失敗'
warnError('依賴性檢查文件返回給erebus失敗信息回調失敗') {
retry(5) {
httpRequest acceptType: 'APPLICATION_JSON', consoleLogResponseBody: true, contentType: 'APPLICATION_JSON', httpMode: 'POST', ignoreSslErrors: true, requestBody: "{\"step\":\"callback\",\"id\":\"${JOB_NAME}\",\"build_number\":\"${BUILD_NUMBER}\"}", timeout: 5, url: 'http://127.0.0.1:8088/api/v1/job_finish', validResponseCodes: '200', validResponseContent: 'ok'
}
}
}
}
}
}
stage('完成') {
steps {
echo '依賴性檢查完成,正在返回完成信息...'
retry(5) {
httpRequest contentType: 'APPLICATION_OCTETSTREAM', customHeaders: [[maskValue: false, name: 'Content-type', value: 'application/json'], [maskValue: false, name: 'Accept', value: 'application/json']], httpMode: 'POST', ignoreSslErrors: true, requestBody: "{\"id\":\"${JOB_NAME}\",\"build_number\":\"${BUILD_NUMBER}\"}", responseHandle: 'NONE', timeout: 5, url: 'http://127.0.0.1:8088/api/v1/job_finish', validResponseContent: 'ok'
}
}
}
}
post {
always {
cleanWs()
}
}
}