1、阶段视图:
2、准备工作
所需工具与插件
jdk:可以存在多版本
maven:可以存在多版本
sonar-scanner
凭证令牌
gitlab:credentialsId
sonarqube:配置在sonarqube208服务中
3、jenkinsfile
pipeline {agent anystages {stage('从gitlab下载代码') {steps {script {git credentialsId: '977a0318-19c4-45f5-8f45-9e129d6753e3', url: 'http://git.xxxo.com.cn/officeweb/Officialweb.git'}}}stage('maven构建代码') {tools {jdk "jdk1.8"}steps {sh 'java -version'sh '/usr/local/maven/apache-maven-3.5.4/bin/mvn -version'sh ' /usr/local/maven/apache-maven-3.5.4/bin/mvn clean install -Dmaven.test.skip=true'}post {always {script {if (currentBuild.currentResult == 'FAILURE') {currentBuild.result = 'FAILURE'throw new Exception('构建失败,停止Pipeline执行')}}}}}stage('SonarQube分析代码') {steps {withSonarQubeEnv('sonarqube208') {sh """/usr/local/sonar-scanner-5/bin/sonar-scanner \-Dsonar.projectKey=guanwang-server \-Dsonar.projectName=官网后台 \-Dsonar.sources=src \-Dsonar.language=java \-Dsonar.java.binaries=target/classes \-Dsonar.junit.reportsPath=target/surefire-reports"""}}}stage('生成自定义镜像') { steps {script {sh """cp /opt/pipeline/java/Dockerfile .docker build -t ${DOCKER_REGISTRY}/${aliyunRepo}/${image_name}:latest ."""}}}stage('登录到镜像仓库') {steps {script{//sh 'docker logout ${DOCKER_REGISTRY}'//withCredentials([usernamePassword(credentialsId: 'b9bfab04-2a1f-4690-88ec-fb8c250d056a', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASSWORD')]) {sh """docker logout ${DOCKER_REGISTRY}docker login -u test-acr@1xxxxx5731673 -p xxxxzEWb ${DOCKER_REGISTRY}"""}}}stage('上传自定义镜像到阿里云') {steps {script {//withCredentials([usernamePassword(credentialsId: 'b9bfab04-2a1f-4690-88ec-fb8c250d056a', usernameVariable: 'aliyunUser', passwordVariable: 'aliyunPasswd')]) {// docker login -u ${aliyunUser} --password-stdin ${aliyunAddress} << EOF// ${aliyunPasswd}// EOF//docker tag ${image_name}:latest${aliyunAddress}/${aliyunRepo}/${image_name}:latestsh """docker push ${DOCKER_REGISTRY}/${aliyunRepo}/${image_name}:latest"""// }}}
}}
}
sonarqube 截图