pipeline {
//agent any
agent none
environment {
HARBOR_CREDS = credentials('harbor-admin-passwd')
//BUILD_BAK = $[ ${BUILD_ID} -2 ]
}
options {
//日志会有时间,删除隐形checkout scm语句,禁止并行,流水线超时设置1h
timestamps()
//shkipDefaultCheckout()
//disableConcurrentBuilds()
timeout(time: 1, unit: 'HOURS')
}
parameters{
string(name: 'BRANCH', defaultValue: 'master', description: '仓库分支')
string(name: 'HARBOR_HOST', defaultValue: '106.13.23.226:31007', description: 'harbor仓库地址')
//string(name: 'DOCKER_IMAGE', defaultValue: 'pinhui001/java-demo', description: 'docker镜像名')
//choice(name: 'DOCKER_UPDATE', choices: 'False\nTrue', description: '是否更新基础镜像')
//string(name: 'APP_NAME', defaultValue: 'pipeline-demo', description: 'k8s中标签名')
//string(name: 'K8S_NAMESPACE', defaultValue: 'demo', description: 'k8s的namespace名称')
}
stages{
stage('拉取代码'){
agent { node { label 'master'} }
steps{
timeout (time:20, unit:"MINUTES"){
git branch: '${BRANCH}',credentialsId: 'gitlab-root-passwd', url: 'http://106.13.23.226:31001/rocilife/xxl-job-admin.git'
}
}
}
stage('静态扫描'){
agent { node { label 'master'} }
steps {
echo "Code Scanning"
}
}
stage('maven编译'){
agent { node { label 'master'} }
steps {
timeout (time:20, unit:"MINUTES"){
sh "/usr/local/maven/bin/mvn -Dmaven.test.skip=true clean install"
}
}
}
stage('代码检测'){
agent { node { label 'master'} }
steps {
timeout (time:20, unit:"MINUTES"){
sh "echo 代码质量检测"
//以下注释
//withSonarQubeEnv('SonarQube'){
// sh '''
// /usr/local/sonar-scanner/bin-scanner \
// && -Dsonar.projectKey=${JOB_NAME} \
// && -Dsonar.projectName=${JOB_NAME} \
// && -Dsonar.projectVersion=1.0 \
// && -Dsonar.sourceEncoding=UTF-8 \
// && -Dsonar.language=java \
// && -Dsonar.sources=. \
// && -Dsonar.java.binaries=.
// '''
//}
//script {
// //一分钟
// timeout(1) {
// //利用sonar webhook功能通知pipeline代码检测结果,未通过质量阈,pipeline将会fail
// def qg = waitForQualityGate()
// if (qg.status != 'OK') {
// error "未通过Sonarqube的代码质量阈检查,请及时修改!failure: ${qg.status}"
// }
// }
//}
}
}
}
stage('镜像制作'){
agent { node { label 'master'} }
steps {
timeout (time:20, unit:"MINUTES"){
dir(env.WORKSPACE) {
sh 'find ./xxl-job-admin/target/ -name "*.jar" -type f -exec scp -P 31000 {} 172.16.16.5:/data/wwwroot/rocilife.dev \\;'
//sh '''
//find ./xxl-job-admin/target/ -name "*.jar" -type f -exec mv {} {}_${BUILD_NUMBER} \\;
//find ./xxl-job-admin/target/ -name "*.jar_*" -type f -exec scp -P 31000 {} 172.16.16.5:/data/wwwroot/rocilife.dev \\;
//'''
}
}
}
}
//stage ('镜像忽略') {
// agent { node { label 'master'} }
// steps {
// echo "不需要更新镜像"
// }
//}
//stage('是否部署'){
// agent { node { label 'master'} }
// steps {
// input "是否部署?"
// }
//}
//stage('部署镜像'){
// agent { node { label 'master'} }
// steps{
// timeout (time:20, unit:"MINUTES"){
// sh "ssh -p 31000 172.16.16.5 'docker login -u ${HARBOR_CREDS_USR} -p ${HARBOR_CREDS_PSW} ${params.HARBOR_HOST} \
// && docker pull ${params.HARBOR_HOST}/${params.DOCKER_IMAGE}:${BUILD_NUMBER} \
// && docker rm -f pinhui \
// && docker run -d --name pinhui ${params.HARBOR_HOST}/${params.DOCKER_IMAGE}:${BUILD_NUMBER}'"
// }
// }
//}
}
post {
always {
script{
println("always")
}
}
success {
script {
currentBuild.description = "构建成功!"
}
}
failure {
script{
currentBuild.description = "构建失败!"
}
}
aborted {
script {
currentBuild.description ="构建取消!"
}
}
}
}