我 Jenkins pipeline 工程运行后的界面如下
下面就直接分享干货,上 pipeline 代码,需要的可以参考,拿走适配自己的工程,修改一下就能直接跑运行了,希望对各位有所帮助。
//===============================================================================================================================//
// 输出日志用颜色标识
def log(def level, def message) {
// 定义日志输出颜色
def ansi_color = ["red": "\033[31m", "green": "\033[32m", "yellow": "\033[33m", "blue": "\033[34m", "res": "\033[0m"]
if("${level}" == "info") {
println("ansicolor.green∗∗∗∗∗∗∗∗{ansi_color.green}********{message}********${ansi_color.res}")
} else if("${level}" == "warnning") {
println("ansicolor.red∗∗∗∗∗∗∗∗{ansi_color.red}********{message}********${ansi_color.res}")
}
}
// 清理工作空间
def clean_dir() {
log("info", "start clean workspace")
deleteDir()
log("info", "end clean workspace")
// params.each {
// my_params.add(string(name: it.key, value: it.value))
// }
}
// check 仓库代码
def scprit_check() {
msg = "echo scprit_check"
/*sh """
git clone http://username:{user_name}:{password_p}@192.203.172.62:8068/root/mvnwebapp.git --depth 1 --branch master mvnwebapp
"""*/
}
// 执行 mvn 编译
def scprit_run() {
msg = "echo scprit_run"
/*sh """
cd env.WORKSPACE/{env.WORKSPACE}/{repo_name} && mvn clean compile package -DskipTests=true
"""*/
}
// sonar 代码扫描质量分析
def scprit_sonar() {
msg = "echo scprit_sonar"
/*sh """
cd env.WORKSPACE/{env.WORKSPACE}/{repo_name} && echo "sonarqube"
"""*/
}
// 构建镜像 push 到 docker
def scprit_docker() {
msg = "echo scprit_docker"
/*sh """
cd env.WORKSPACE/{env.WORKSPACE}/{repo_name} && echo "docker"
"""*/
}
// 构建镜像 push 到 artifactory
def scprit_artifactory() {
msg = "echo scprit_artifactory"
/*sh """
cd env.WORKSPACE/{env.WORKSPACE}/{repo_name}/out
jfrog rt upload --user scmroot --access-token x8jYt43Ekj4RJljigIET05A2MMhq --url http://www.jfrogpro.itdev.cn/artifactory/ mvnwebapp/targer/ encdate-maven-repo/dev/
"""*/
}
// 发布到研发环境进行验证
def scprit_deploy() {
msg = "echo scprit_deploy"
/*sh """
cd env.WORKSPACE/{env.WORKSPACE}/{repo_name} && cp -rf target/*.war scmroot@192.13.117.15/data/{repo_name}_{repo_name}_{repo_name}_{BUILDDATE}
"""*/
}
// ****************************************** Start ************************************
pipeline {
agent {
node {
label "master"
//label "SH_Ubuntu22"
}
}
environment {
BUILDDATE = sh(script: "date +%Y%m%d", returnStdout: true).trim()
BUILDTIME = sh(script: "date +%Y%m%d%H%M%S", returnStdout: true).trim()
LC_ALL="C.UTF-8"
LANG="C.UTF-8"
}
parameters {
string(name: "REPO_NAME", defaultValue: "mvnwebapp", description: "gitlab中的git仓库名称")
string(name: "BRANCH_NAME", defaultValue: "master", description: "source仓库主干分支")
string(name: "USER_NAME", defaultValue: "", description: "用户名")
string(name: "PASSWORD_P", defaultValue: "", description: "用户密码")
}
options {
ansiColor('xterm')
disableConcurrentBuilds()
timestamps()
}
stages {
stage("Clean") {
steps {
dir("${env.workspace}") {
script {
clean_dir()
}
}
}
}
stage("CheckOut") {
steps {
dir("${env.workspace}") {
script {
scprit_check()
println env.JOB_NAME
println env.BUILD_NUMBER
}
}
}
}
stage("Build") {
steps {
dir("${env.workspace}") {
script {
scprit_run()
}
}
}
}
stage("SonarQube") {
steps {
dir("${env.workspace}") {
script {
scprit_sonar()
}
}
}
}
stage("Push Docker") {
steps {
dir("${env.workspace}") {
script {
scprit_docker()
}
}
}
}
stage("Push Artifactory") {
steps {
dir("${env.workspace}") {
script {
scprit_artifactory()
}
}
}
}
stage("Deploy") {
steps {
dir("${env.workspace}") {
script {
scprit_deploy()
}
}
}
}
}
post {
success {
println("success")
emailext (
subject: "The Project information is as follows:",
body: """Successful: Project JOBNAME[{JOB_NAME} [{BUILD_NUMBER}]'
Build Status: ${JOB_NAME} job Build works normally
JOB URL: ${BUILD_URL}
Project Name: ${JOB_NAME}
Project Compilation Times:${BUILD_NUMBER}""",
to: "cicdadmin@sysdev.com",
from: "cicdadmin@sysdev.com"
)
}
failure {
println("failed")
emailext (
subject: "The Project information is as follows:",
body: """Failed: Project JOBNAME[{JOB_NAME} [{BUILD_NUMBER}]'
Build Status: ${JOB_NAME} job Build works normally
JOB URL: ${BUILD_URL}
Project Name: ${JOB_NAME}
Project Compilation Times:${BUILD_NUMBER}""",
to: "
cicdadmin@sysdev.com
",
from: "
cicdadmin@sysdev.com
"
)
}
}
}
//===============================================================================================================================//
// 邮件通知
/*def send_mail() {
def user = "wst021@zoho.com"
def email_subject = "FAILED: Job '{env.JOB_NAME} - Build #{env.JOB_NAME} - Build #{env.JOB_NAME} - Build #{env.BUILD_NUMBER}]'"
if ("${msg}" == "") {
email_subject = "SUCCESSED: Job '[{env.JOB_NAME} - Build #{env.JOB_NAME} - Build #{env.JOB_NAME} - Build #{env.BUILD_NUMBER} - ${params.PROJECT_NAME}]'"
}
emailext (
subject: "${email_subject}",
mimeType: 'text/html',
attachLog: true,
compressLog: false,
body: """
下载地址:
http://192.203.71.124:18081/artifactory/devpackage/${BUILDDATE}
${msg}
""",
to: "${user}",
from: "jenkins@admin.com"
)
}*/