2 Star 3 Fork 3

coolops / jenkins-shareLibrary

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ci.Jenkinsfile 4.66 KB
一键复制 编辑 原始数据 按行查看 历史
coolops 提交于 2020-05-22 17:45 . update
//String workspace "/opt/jenkins/workspace"
// 配置共享库
@Library('myLib')
// 引用共享库中的方法
def tools = new org.devops.tools()
def build = new org.devops.build()
def deploy = new org.devops.deploy()
def sendEmail = new org.devops.sendEmail()
def sonar = new org.devops.sonarqube()
def sonarapi = new org.devops.sonarApi()
def nexus = new org.devops.nexus()
def dingmes = new org.devops.sendDingTalk()
def runOpts
def toEmailUser = "coolops@163.com"
// 定义变量
String buildType = env.buildType
String buildShell = env.buildShell
String deployHosts = env.deployHosts
String dingTalkHook = env.dingTalkHook
String branchName = "${env.branchName}"
String srcUrl = "${env.srcUrl}"
// 上传nexus的方式
String uploadNexusType = env.uploadNexusType
// 获取分支
if ("${runOpts}" == "GiteePush"){
branchName = branch - "refs/heads/"
currentBuild.description = "构建者${userName} 分支${branchName}"
}else{
currentBuild.description = "构建者${env.BUILD_USER_ID} 分支${branchName}"
}
// Pipeline
pipeline {
// 指定在哪个节点上执行pipeline
agent any
// 获取自动安装或者手动安装的环境变量
//tools {
// maven "M2"
//}
// 指定运行的选项(可选)
options {
timestamps() // 日志会有时间
skipDefaultCheckout() // 删除隐式checkout scm语句
disableConcurrentBuilds() //禁止并行
timeout(time:1,unit:'HOURS') //设置流水线超时时间
}
// 构建阶段
stages {
// 下载代码
stage("GetCode"){
// 步骤
steps{
// 设置步骤超时时间
timeout(time:5,unit:'MINUTES'){
script{
tools.PrintMes(branchName, "blue")
// println("获取代码")
tools.PrintMes("获取代码",'green')
checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'gitee', url: "${srcUrl}"]]])
}
}
}
}
stage("Build"){
steps{
timeout(time:20,unit:'MINUTES'){
script{
// println("代码打包")
tools.PrintMes("代码打包",'green')
build.Build(buildType,buildShell)
// 上传制品库
println(uploadNexusType)
nexus.main(uploadNexusType)
}
}
}
}
stage("CodeScanner"){
steps{
timeout(time:30,unit:'MINUTES'){
script{
// 在扫描之前判断sonar项目是否存在
res = sonarapi.SearchProject("${JOB_NAME}")
if (res == "false"){
// 表示项目不存在,需要新增项目
tools.PrintMes("新增Sonar项目${JOB_NAME}",'green')
sonarapi.CreateProject("${JOB_NAME}")
// 配置项目质量规则,这里是写死的,后期再修改
tools.PrintMes("配置项目${JOB_NAME}质量规则",'green')
sonarapi.ConfigQualityProfiles("java","java-test","${JOB_NAME}")
// 配置质量阈
tools.PrintMes("配置项目${JOB_NAME}质量阈",'green')
sonarapi.ConfigQualityGate("${JOB_NAME}","demo")
}
tools.PrintMes("代码扫描",'green')
sonar.SonarScan("pre","${JOB_NAME}","${JOB_NAME}","src")
}
}
}
}
stage("ScanStatus"){
steps{
timeout(time:30,unit:"MINUTES"){
script{
// 获取扫描状态
tools.PrintMes("获取扫描结果状态","green")
res = sonarapi.GetSonarStatus("${JOB_NAME}")
// 对结果进行判断
if (res.toString() == "ERROR"){
// 发邮件通知
sendEmail.SendEmail("代码扫描失败",toEmailUser)
error "代码扫描失败."
}
}
}
}
}
}
// 构建后的操作
post {
always {
script{
println("always:不论构建成功与否都会执行")
}
}
success {
script{
println("success:只有构建成功才会执行")
currentBuild.description += "\n构建成功!"
// deploy.AnsibleDeploy("${deployHosts}","-m ping")
// sendEmail.SendEmail("构建成功",toEmailUser)
dingmes.SendDingTalk("构建成功 ✅")
}
}
failure {
script{
println("failure:只有构建失败才会执行")
currentBuild.description += "\n构建失败!"
//sendEmail.SendEmail("构建失败",toEmailUser)
dingmes.SendDingTalk("构建失败 ❌")
}
}
aborted {
script{
println("aborted:只有取消构建才会执行")
currentBuild.description += "\n构建取消!"
//sendEmail.SendEmail("取消构建",toEmailUser)
dingmes.SendDingTalk("构建失败 ❌","暂停或中断")
}
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/coolops/jenkins-shareLibrary.git
git@gitee.com:coolops/jenkins-shareLibrary.git
coolops
jenkins-shareLibrary
jenkins-shareLibrary
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891