当前位置:网站首页>Jenkins Pipeline 方法(函数)定义及调用
Jenkins Pipeline 方法(函数)定义及调用
2022-07-05 08:51:00 【云计算-Security】
网上查了一些文档资料,Jenkins Pipeline 方法调用
的文章还是比较少的,就算有也描述得不太清楚。于是根据自己的经验总结了如何在 Jenkinsfile 进行方法调用。如有不当之处,请予指正。
首先来说一下为什么要使用方法
?有这样一个场景,我有十几台远端服务器,这些服务器都是我的测试(或线上)服务器,我构建后的包需要部署到这些服务器上,于是我会为每台远程服务器定义相关登录信息(用户名、密码或密钥)等。这些配置对于每台服务器来说其实都是一样,无非就是 IP 不同而已(用户名、密钥或密码我暂且保持一致),于是我就可以将相同的部分定义为一个方法或叫函数,并在我的流水线阶段来调用这些方法或函数,当你定义的函数过多时,你还可以将这些函数写道共享库
中,然后流水线调用共享库即可。
本案例以声明式
Jenkinsfile 为例进行演示!
1、定义方法(函数)
方法定义于 pipeline {}
之外,并在 pipeline {}
之内进行调用。如下,我定义了一个远程服务器的远程登录信息,方便我能把构建的包发布到远程服务器上。
// 创建函数
def RemoteHost(Jarnname, Host, ProJect) {
withCredentials([usernamePassword(credentialsId: '131', passwordVariable: 'PassWord', usernameVariable: 'UserName')]){
def remote = [:]
remote.name = "$Jarnname"
remote.host = "$Host"
remote.user = "$UserName"
remote.password = "$PassWord"
remote.port = 22
remote.allowAnyHosts = true
sshPut remote: remote, from: "./target/$Jarnname", into: "./${ProJect}/", override: true
}
}
// 流水线步骤
pipeline {
stages {
// 阶段 + 步骤
}
}
2、调用方法(函数)
看下面代码块的项目发布
阶段,在这里调用了上面定义的函数,并进行参数传递。
// 创建函数
def RemoteHost(Jarnname, Host, ProJect) {
withCredentials([usernamePassword(credentialsId: '131', passwordVariable: 'PassWord', usernameVariable: 'UserName')]){
def remote = [:]
remote.name = "$Jarnname"
remote.host = "$Host"
remote.user = "$UserName"
remote.password = "$PassWord"
remote.port = 22
remote.allowAnyHosts = true
sshPut remote: remote, from: "./target/$Jarnname", into: "./${ProJect}/", override: true
}
}
// 流水线步骤
pipeline {
agent {
docker {
image 'maven:v2'
args '-v /var/lib/jenkins/workspace/$JOB_NAME:/root/.m2 -v /root/mavenRepository:/usr/repository'
}
}
...
...
stages {
// 拉取代码
stage('拉取代码') {
steps {
checkout([$class: 'GitSCM',
branches: [[name: "${params.BRANCH_TAG}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
gitTool: 'Default',
submoduleCfg: [],
userRemoteConfigs: [[url: 'https://codeup.aliyun.com/xx/java_code.git',credentialsId: 'yourid',]]
])
}
// 项目构建
stage("构建项目") {
steps {
sh "mvn clean package -Dmaven.test.skip=true -P test"
}
}
// 项目发布(在这里调用方法)
stage ("项目发布") {
steps {
script {
switch("$p_name"){
case "cms":
Jarnname = "demo.jar"
Host = "192.168.56.131"
ProJect = "cms"
// 调用方法并传递参数
RemoteHost("${Jarnname}", "${Host}", "${ProJect}")
break
}
}
}
}
...
...
}
}
这样的话,我们就不用每次都写一遍定义的方法内容了,只需调用其函数名即可。
注意:上面只是我 Jenkinsfile 的一部分!
至此,Jenkins Pipeline 方法定义及调用已经完成。
边栏推荐
- Redis implements a high-performance full-text search engine -- redisearch
- C#图像差异对比:图像相减(指针法、高速)
- MPSoC QSPI Flash 升级办法
- Use arm neon operation to improve memory copy speed
- 使用arm Neon操作,提高内存拷贝速度
- C [essential skills] use of configurationmanager class (use of file app.config)
- Codeworks round 639 (Div. 2) cute new problem solution
- Numpy pit: after the addition of dimension (n, 1) and dimension (n,) array, the dimension becomes (n, n)
- Classification of plastic surgery: short in long long long
- One dimensional vector transpose point multiplication np dot
猜你喜欢
Yolov4 target detection backbone
Programming implementation of ROS learning 6 -service node
Business modeling of software model | vision
Mathematical modeling: factor analysis
Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc
Halcon Chinese character recognition
Wechat H5 official account to get openid climbing account
2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition
Solution to the problems of the 17th Zhejiang University City College Program Design Competition (synchronized competition)
Count of C # LINQ source code analysis
随机推荐
C#绘制带控制点的Bezier曲线,用于点阵图像及矢量图形
Guess riddles (8)
Warning: retrying occurs during PIP installation
C#【必备技能篇】ConfigurationManager 类的使用(文件App.config的使用)
Program error record 1:valueerror: invalid literal for int() with base 10: '2.3‘
Beautiful soup parsing and extracting data
Dynamic dimensions required for input: input, but no shapes were provided. Automatically overriding
My experience from technology to product manager
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
Reasons for the insecurity of C language standard function scanf
【日常訓練--騰訊精選50】557. 反轉字符串中的單詞 III
Add discount recharge and discount shadow ticket plug-ins to the resource realization applet
RT thread kernel quick start, kernel implementation and application development learning with notes
location search 属性获取登录用户名
Halcon snap, get the area and position of coins
287. Looking for repeats - fast and slow pointer
Numpy 小坑:维度 (n, 1) 和 维度 (n, ) 数组相加运算后维度变为 (n, n)
Numpy pit: after the addition of dimension (n, 1) and dimension (n,) array, the dimension becomes (n, n)
Golang foundation -- map, array and slice store different types of data
696. Count binary substring