#!groovy
@Library('jenkinslib') _
def tools = new org.devops.tools()
def build = new org.devops.build()
def gitlab = new org.devops.gitlab()
def toemail = new org.devops.toemail()
def sonar = new org.devops.sonarqube()
def sonarapi = new org.devops.sonarapi()
def WebHookData = readJSON text : allData
String branch=WebHookData.ref
String userName=WebHookData.user_username
String projectId=WebHookData.project.id
String commitSha=WebHookData.checkout_sha
pipeline {
agent {
node {
label "built-in"
}
}
environment {
String GsrcUrl="${env.srcUrl}"
String GbranchName="${env.branchName}"
String GbuildType="${env.buildType}"
String GbuildShell="${env.buildShell}"
String qpName = "demo" //Sonar%20way
}
stages {
stage('CheckOut') {
steps {
script {
if ("$runOpts" == "GitlabPush"){
GbranchName = branch - 'refs/heads/'
currentBuild.description = "Trigger by user ${userName} \n branch: ${GbranchName}"
gitlab.ChangeCommitStatus(projectId,commitSha,"running")
}
tools.PrintMes("获取代码",'green')
checkout scmGit(branches: [[name: "${GbranchName}"]],
extensions: [cleanBeforeCheckout()],
userRemoteConfigs: [[credentialsId: '8c8a0774-7d4c-48a9-8f52-dd1a04f71320',
url: "${GsrcUrl}"]])
}
}
}
stage("Build") {
steps {
script {
tools.PrintMes("执行打包",'green')
build.Build(GbuildType,GbuildShell)
}
}
}
stage('QA') {
steps {
script {
tools.PrintMes("搜索项目",'green')
result = sonarapi.SerarchProject("${JOB_NAME}")
println(result)
if ( result == 'false' ){
println("${JOB_NAME}---项目不存在,准备创建项目---> ${JOB_NAME}!")
sonarapi.CreateProject("${JOB_NAME}")
} else {
println("${JOB_NAME}---项目已存在")
}
tools.PrintMes("配置项目质量规则","green")
sonarapi.ConfigQualityProfiles("${JOB_NAME}","web",qpName)
tools.PrintMes("配置质量阈","green")
sonarapi.ConfigQualityGates("${JOB_NAME}",qpName)
tools.PrintMes("代码扫描",'green')
sonar.SonarScan("dev","${JOB_NAME}","${JOB_NAME}","src")
sh "sleep 20"
tools.PrintMes("获取扫描结果",'green')
result = sonarapi.GetProjectStatus("${JOB_NAME}")
println(result)
if ( result.toString() == "ERROR" ){
toemail.Email("代码质量阈错误,请及时修复!",userEmail)
error "代码质量阈错误,请及时修复!"
}else {
println(result)
}
}
}
}
}
post {
always {
script {
println("always")
}
}
success {
script {
println("success")
gitlab.ChangeCommitStatus(projectId,commitSha,"success")
toemail.Email("流水线成功了",userEmail)
}
}
failure {
script {
println("failure")
gitlab.ChangeCommitStatus(projectId,commitSha,"failed")
toemail.Email("流水线失败了",userEmail)
}
}
aborted {
script {
println("aborted")
gitlab.ChangeCommitStatus(projectId,commitSha,"canceled")
toemail.Email("流水线被取消了",userEmail)
}
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.