Jenkins pipeline 纯代码第二波拿走即用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jenkins pipeline 纯代码第二波拿走即用相关的知识,希望对你有一定的参考价值。


前言、关于、介绍此处省略2K字...... ,全部都免了不用在废话过多介绍了,下面就直接分享干货,上 pipeline  纯代码,拿走适配自己的工程,小修改一下就能直接跑了。

##########################################################################

下面分享我的 Pipeline script 完整代码,按自己工程配置修改,即可直接使用。

// 输出日志用颜色标识
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("$ansi_color.green********$message********$ansi_color.res")
else if("$level" == "warnning")
println("$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://$user_name:$password_p@192.203.72.162:18068/root/mvnwebapp.git --depth 1 --branch master mvnwebapp
"""


// 执行 mvn 编译
def scprit_run()
msg = "echo scprit_run"
sh """
cd $env.WORKSPACE/$repo_name && mvn clean compile package -DskipTests=true
"""


// sonar 代码扫描质量分析
def scprit_sonar()
msg = "echo scprit_sonar"
sh """
cd $env.WORKSPACE/$repo_name && echo "sonarqube"
"""


// 构建镜像 push 到 docker
def scprit_docker()
msg = "echo scprit_docker"
sh """
cd $env.WORKSPACE/$repo_name && echo "docker"
"""


// 构建镜像 push 到 artifactory
def scprit_artifactory()
msg = "echo scprit_artifactory"
sh """
cd $env.WORKSPACE/$repo_name/out
jfrog rt upload --user scmadmin --access-token eiSHEkj4RJlji32o215AHek --url http://www.jfrogpro.encdate.cn/artifactory/ mvnwebapp/targer/ encdate-maven-repo/dev/
"""


// 发布到研发环境进行验证
def scprit_deploy()
msg = "echo scprit_deploy"

sh """
cd $env.WORKSPACE/$repo_name && cp -rf target/*.war scmadmin@192.23.17.105/data/$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 $JOB_NAME[$BUILD_NUMBER]
JOB URL: $BUILD URL
Project Name: $JOB_NAME
Project Compilation Times:$BUILD_NUMBER""",
to: "scmadmin@enc.com",
from: "scmadmin@enc.com"
)

failure
println("failed")
emailext (
subject: "The Project information is as follows:",
body: """Failed: Project $JOB_NAME[$BUILD_NUMBER]
JOB URL: $BUILD URL
Project Name: $JOB_NAME
Project Compilation Times:$BUILD_NUMBER""",
to: "scmadmin@enc.com",
from: "scmadmin@enc.com"
)




===============================================================================================================================
// 邮件通知
/*def send_mail()
def user = "wst021@zoho.com"
def email_subject = "FAILED: Job $env.JOB_NAME - Build #$env.BUILD_NUMBER]"
if ("$msg" == "")
email_subject = "SUCCESSED: Job [$env.JOB_NAME - Build #$env.BUILD_NUMBER - $params.PROJECT_NAME]"

emailext (
subject: "$email_subject",
mimeType: text/html,
attachLog: true,
compressLog: false,
body: """
<div style="font-family:黑体">
<ul>
<li>下载地址: http://192.203.71.124:18081/artifactory/devpackage/$BUILDDATE</li>
$msg
</ul>
</div>
""",
to: "$user",
from: "jenkins@admin.com"
)
*/

以上是关于Jenkins pipeline 纯代码第二波拿走即用的主要内容,如果未能解决你的问题,请参考以下文章

JAVA 基础知识 第二波~

让Jenkins执行GitHub上的pipeline脚本

jenkins 2 pipeline怎么获取各个阶段的log

Jenkins之Pipeline代码流水线配置

Jenkins之Pipeline代码流水线

Jenkins之Pipeline代码流水线配置